//Skip to content
Inheritance in C++
Single Inheritance in C++:
- #include<iostream>
- using namespace std;
-
- class Shape{
- private:
- int l,h;
- public:
- void setDetails(int a,int b);
- int getDetails(){
- return l*h;
- }
- };
- void Shape::setDetails(int a,int b){
- l=a;
- h=b;
- }
-
- class Rectangle:public Shape{
- public:
- void showArea(){
- cout<<"Rectangle Area: "<<getDetails()<<endl;
- }
- };
-
- int main()
- {
- Rectangle aRectangle;
- aRectangle.setDetails(3,6);
- aRectangle.showArea();
- return 0;
- }
Multiple Inheritance in C++:
- #include<iostream>
- using namespace std;
-
- class Area{
- private:
- int length;
- int width;
- public:
- void setArea(int x,int y){
- length=x;
- width=y;
- }
- int getArea(){
- return length*width;
- }
- };
-
- class Height{
- private:
- int h;
- public:
- void setHeight(int x){
- h=x;
- }
- int getHeight(){
- return h;
- }
- };
-
- class Volume:public Area,public Height{
- public:
- void getVolume(){
- cout<<"Volume: "<<getArea()*getHeight()<<endl;
- }
- };
-
- int main()
- {
- Volume vol;
- vol.setArea(4,3);
- vol.setHeight(7);
- vol.getVolume();
- return 0;
- }
I am Md Abdullah Al Hasan. I have completed my graduation in Computer Science and Engineering from Jashore University of Science and Technology.
0 Comments:
Post a Comment