#include <iostream>
usingnamespace std;
class Polygon {
protected:
int x;
int y;
public:
Polygon(int, int);
};
Polygon::Polygon(int x, int y) {
this->x = x;
this->y = y;
}
class Square : public Polygon {
public:
Square(int);
int area();
};
Square::Square(int x) {
Polygon::Polygon(x, x);
}
int Square::area() {
returnthis->x * this->y;
}
int main() {
Square s(5);
cout << s.area() << endl;
}
As you see, the Square class inherits properties from the Polygon class. However, I am getting the following compilation errors.
1 2 3 4
basicInheritance.cpp: In constructor ‘Square::Square(int)’:
basicInheritance.cpp:24: error: no matching function for call to ‘Polygon::Polygon()’
basicInheritance.cpp:13: note: candidates are: Polygon::Polygon(int, int)
basicInheritance.cpp:5: note: Polygon::Polygon(const Polygon&)
Why is the default constructor for Polygon being called? I thought that I had explicitly called a custom constructor for Polygon in the constructor for Square.