5. (A) Design a class for single level inheritance using public and private type derivation
Single level inheritance using public
| ||
| class Shape | ||
| { | ||
| protected: | ||
| float width, height; | ||
| public: | ||
| void set_data (float a, float b) | ||
| { | ||
| width = a; | ||
| height = b; | ||
| } | ||
| }; | ||
| class Rectangle: public Shape | ||
| { | ||
| public: | ||
| float area () | ||
| { | ||
| return (width * height); | ||
| } | ||
| }; | ||
| class Triangle: public Shape | ||
| { | ||
| public: | ||
| float area () | ||
| { | ||
| return (width * height / 2); | ||
| } | ||
| }; | ||
| void main () | ||
| { | ||
| clrscr(); | ||
| Rectangle rect; | ||
| Triangle tri; | ||
| rect.set_data (5,3); | ||
| tri.set_data (2,5); | ||
| cout <<"\narea of reactangle= "<< rect.area() << endl; | ||
| cout <<"\narea of triangle= "<< tri.area() << endl; | ||
| getch(); | ||
| } |
Single level inheritance using private
| ||
| class Shape | ||
| { | ||
| protected: | ||
| float width, height; | ||
| public: | ||
| void set_data (float a, float b) | ||
| { | ||
| width = a; | ||
| height = b; | ||
| } | ||
| }; | ||
| class Rectangle: private Shape | ||
| { | ||
| public: | ||
| float area () | ||
| { | ||
| set_data (5,3); | ||
| return (width * height); | ||
| } | ||
| }; | ||
| class Triangle: private Shape | ||
| { | ||
| public: | ||
| float area () | ||
| { | ||
| set_data (2,5); | ||
| return (width * height / 2); | ||
| } | ||
| }; | ||
| void main () | ||
| { | ||
| clrscr(); | ||
| Rectangle rect; | ||
| Triangle tri; | ||
| cout <<"\narea of reactangle= "<< rect.area() << endl; | ||
| cout <<"\narea of triangle= "<< tri.area() << endl; | ||
| getch(); | ||
| } |
5. (A) Design a class for single level inheritance using public and private type derivation
Reviewed by admin
on
December 23, 2019
Rating:
Reviewed by admin
on
December 23, 2019
Rating:

No comments: