In the following code I am calculating the area of triangle. Once I declare the object tri1 the width and height are initialized twice.
First: The default constructor of the base class is called and values width = 10.9; height = 8.0; are automatically assigned to the triangle.
Then: In triangle construct width = a; and height = b; happens.
But, my question is: Is there any way not to call any constructor from a base class?
class polygon {
protected:
float width, height;
public:
polygon () {width = 10.9; height = 8.0;}
void set_val (float a, float b) {width = a; height = b;}
polygon (float a, float b) : width(a), height(b) {cout<<"I am the polygon"<<endl;}
};
class triangle: public polygon {
public:
triangle (float a, float b) {cout<<"passed to polygon"<<endl; width = a; height = b;}
float area () {return width*height/2;}
};
int main () {
triangle tri1 {10, 5};
cout<<tri1.area()<<endl;
}