Area and Volume using multiple inheritance
Write a C++ Program to find Area and Volume using Multiple inheritance. Here’s a Simple C++ Program to find Area and Volume using Multiple inheritance in C++ Programming Language.
Multiple Inheritance : :
When a class is inherited from more than one base class, it is known as multiple inheritance.
- The syntax for defining a subclass, which is inheriting more than one classes is:
class Subclass : access_specifier Baseclass1,
access_specifier Baseclass2, ………
…….. access_specifier Baseclass_n
{
members of the derived class ;
};
- The following figure example shows the use of multiple inheritance.
- Circle and Rectangle are two base classes from which the class Cylinder is being inherited.
- The data members of both the base classes are declared in protected mode. Thus, the class Cylinder can access the data member radius of class Circle and data member length, breadth of the class Rectangle, but the objects of the class Cylinder cannot access these protected data members.
- The volume of the cylinder is equal to 22/7*(radius*radius*length). Thus, instead of defining these data again, they can be inherited from the base classes Circle and Rectangle ( radius from class Circle and length from class Rectangle ).
Below is the source code for C++ Program to demonstrate an Example of Multilevel Inheritance which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C++ Program to demonstrate an Example of Multilevel Inheritance */ #include<iostream> using namespace std; class Circle // First base class { protected: float radius ; public: void Enter_r(void) { cout << "\n\t Enter the radius: "; cin >> radius ; } void Display_ca(void) { cout << "\t The area = " << (22/7 * radius*radius) ; } }; class Rectangle // Second base class { protected: float length, breadth ; public: void Enter_lb(void) { cout << "\t Enter the length : "; cin >> length ; cout << “\t Enter the breadth : ” ; cin >> breadth ; } void Display_ar(void) { cout << "\t The area = " << (length * breadth); } }; class Cylinder : public Circle, public Rectangle // Derived class, inherited { // from classes Circle & Rectangle public: void volume_cy(void) { cout << "\t The volume of the cylinder is: " << (22/7* radius*radius*length) ; } }; int main() { Circle c ; cout << "\n Getting the radius of the circle\n" ; c.Enter_r( ); c.Display_ca( ); Rectangle r ; cout << "\n\n Getting the length and breadth of the rectangle\n\n"; r.Enter_lb( ); r.Display_ar( ); Cylinder cy ; // Object cy of the class cylinder which can access all the // public members of the class circle as well as of the class rectangle cout << "\n\n Getting the height and radius of the cylinder\n"; cy.Enter_r( ); cy.Enter_lb( ); cy.volume_cy( ); return 0; }
OUTPUT : :
/* C++ Program to demonstrate an Example of Multilevel Inheritance */ Getting the radius of the circle Enter the radius: 4 The area = 48 Getting the length and breadth of the rectangle Enter the length : 6 Enter the breadth : 7 The area = 42 Getting the height and radius of the cylinder Enter the radius: 3 Enter the length : 4 Enter the breadth : 5 The volume of the cylinder is: 108 Process returned 0
Above is the source code and output for C++ Program to demonstrate an Example of Multilevel Inheritance which is successfully compiled and run on Windows System to produce desired output.
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.
Thanks for reading the post….