Point class, which stores coordinates of points, and Rectangle class that inherits Shape.
#include <iostream>
#include <vector>
#include <string>
using std::cout;
using std::endl;
class Point
{
int x, y;
public:
Point(int a, int b)
{
this->x = a;
this->y = b;
}
Point(Point &A)
{
this->x = A.x;
this->y = A.y;
}
};
class Shape
{
public:
};
class Rectangle : public Shape
{
Point p[4];
public:
Rectangle(const Point p1, const Point p2, const Point p3, const Point p4)
{
p[0] = p1;
p[1] = p2;
p[2] = p3;
p[3] = p4;
}
Rectangle(const Point p1, const Point p2, const Point p3, const Point p4)
In this part, there is an error that there is no default generator when transferring parameters, which generator should I create?
c++ class constructor
This error is caused by the lack of a default constructor in the Point class.
class Rectangle : public Shape
{
Point p[4];
// blah blah blah
You declared a point-type array, p, as a member variable of Rectangle. No default constructor can generate this p.
Change the Point class as follows.
class Point
{
int x, y;
public:
Point() {}
Point(int a, int b)
{
this->x;
this->y;
}
Point(Point &A)
{
this->x = A.x;
this->y = A.y;
}
};
© 2024 OneMinuteCode. All rights reserved.