#include<iostream>
#include<string>
#include<cmath>
using namespace std;
class Polygon {
public:
Polygon() {}
Polygon(int point, float length) {
mPoint = point;
mLength = length;
}
~Polygon() {}
virtual void calcPerimeter() {
cout << "Perimeter: " << mLength * mPoint;
}
virtual void calcArea() {
cout << "Area: " << sqrt(3) * pow(mLength, 2) / 4;
}
protected:
int mPoint;
double mLength;
};
class Rectangle : public Polygon {
public:
Rectangle() {}
Rectangle(int point, float length):mPoint(4), mLength(4 * length){}
~Rectangle() {}
void calcPerimeter() override {
cout << "Perimeter: " << mLength;
}
void calcArea() override {
cout << "Area: " << mLength * mLength;
}
};
int main() {
Polygon pol;
Rectangle rec(4, 10);
cout << "--- Polygon class---" << endl;
pol.calcPerimeter();
pol.calcArea();
cout << "---Rectangle class---" << endl;
rec.calcPerimeter();
rec.calcArea();
return 0;
}
I learned that if the protected part of the parent class is inherited as public, it is used like a private in the child class. By the way
Rectangle (int point, float length): mPoint (4), mLength (4 * length) {}
In this part, I get the error that mPoint and mLength are not non-static data members or the base class of Reactangle. If it's private, can't I use it like that in a class ??
If I'm misunderstanding, I hope you can tell me what's wrong. Thanks for reading.