c++ - display the values of area for inherited classes -
in code below how can display areas of rectangle , triangle. can print strings area returned. how can print returned value functions. should change here in code, please help.
class shape { protected: int width, height; public: shape( int = 0, int b = 0) { width = a; height = b; } virtual int area() { cout << "parent class area :" <<endl; return 0; } }; class rectangle: public shape { public: rectangle( int = 0, int b = 0):shape(a, b) { } int area () { cout << "rectangle class area :" <<endl; return (width * height); } }; class triangle: public shape{ public: triangle( int = 0, int b = 0):shape(a, b) { } int area () { cout << "triangle class area :" <<endl; return (width * height / 2); } }; // main function program int main( ) { shape *shape; rectangle rec(10,7); triangle tri(10,5); // store address of rectangle shape = &rec; // call rectangle area. shape->area(); // store address of triangle shape = &tri; // call triangle area. shape->area(); return 0; }
change area functions below:
int rectangle::area() { int ret = width * height; cout << "rectangle class area : " << ret << endl; return ret; } int triangle::area() { int ret = width * height / 2; cout << "triangle class area :" << ret << endl; return ret; }
Comments
Post a Comment