简体   繁体   中英

C++ polygon rectangle and triangle inheritance

I am trying to make a Polygon class and a Rectangle and Triangle that inherit the first. Polygon class has height and width variables that I want them to be be given values within the constructor. Then, Rectangle and Triangle have area calculation methods. Then, I use a main() to give some examples. I use:

#include <iostream>
using namespace std;

class Polygon {
    public:
        Polygon(int, int);
    protected:
        int height;
        int width;
};

class Rectangle: public Polygon {
    public:
        void calc_area();
};

class Triangle: public Polygon {
    public:
        void calc_area();
};

Polygon::Polygon(int a, int b) {
    height = a;
    width = b;
}

void Rectangle::calc_area() {
    cout << "Rectangle area: " << (height*width) << endl;
}

void Triangle::calc_area() {
    cout << "Triangle area: " << (height*width/2) << endl;
}

int main() {
    Rectangle s1(5, 2);
    Triangle s2(5, 2);
    s1.calc_area();
    s2.calc_area();
}

But while everything looks ok to my newbie eyes, I get a series of errors:

12 base Polygon' with only non-default constructor in class without a constructor `

36 no matching function for call to `Rectangle::Rectangle(int, int)

37 no matching function for call to `Triangle::Triangle(int, int)'

Can someone give me some tips? As seen, I am very new to C++...

You shouldn't call constructor to use . , such as:

Rectangle s1;
Triangle s2;
s1.Polygon(5, 2);
s2.Polygon(5, 2);

try this way:

Rectangle s1(5, 2);
Triangle  s2(5, 2);

and you should add constructor for Rectangle and Triangle respectively:

class Rectangle: public Polygon {
public:
    Rectangle(int height, int width):Polygon(height, width){}
    void calc_area();
};

class Triangle: public Polygon {
public:
    Triangle(int height, int width):Polygon(height, width){}
    void calc_area();
};

To construct Triangle and Rectangle you are calling their invisible default constructors.

So the Triangle() and Rectangle() methods. These are implemented as empty methods, but they must also call the default constructors of any class they inherit from. So effectively this is the code that's being generated:

Triangle() : Polygon() {}
Rectangle() : Polygon() {}

Polygon will not get a default constructor because you created your own constructor. So the only way to get that back is to define it:

Polygon() {}

Or in C++11:

Polygon() = default;

The default constructor Triangle() or Rectangle() are called when you instantiate the member variable. So these lines are calling constructors:

Rectangle s1;
Triangle s2;

Note that once they are constructed they cannot be reconstructed. So these calls are illegal:

s1.Polygon(5, 2);
s2.Polygon(5, 2);

Now if you want to accomplish having a setting constructor, you can do that, but you'll need to define it for Triangle and Rectangle :

Triangle(int width, int height) : Polygon(width, height) {}
Triangle(int width, int height) : Polygon(width, height) {}

Again recall that defining these methods will cause your default constructor not to be generated for Triangle or Rectangle . So your s1 and s2 declarations will now be illegal, but that's OK cause you want to declare them with your new constructors anyway:

Rectangle s1(5, 2);
Triangle s2(5, 2);

You must use this way... Rectangle *s1 = new Rectangle; s1->ab(5,2); s1->calc_area();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM