Multiple inheritance

Multiple inheritance:

In multiple inheritance a single derived class may inherit from two or more base classes.

multiple

Example 1
include<iostream.h>

// this is a Base class Shape

class Shape

{

public: void setWidth(int w)

{

width = w;

}

void setHeightt(int h)

{

heightt = h;

}

protected: int width;

int heightt;

};

class PaintCost

{

public: int getCost(int area)

{

return area * 70;

}

};

// this is a Derived class

class Rectangle: public Shape, public PaintCost

{

public: int getArea()

{

return (width * heightt);

}

};

int main(void)

{

Rectangle Rect1;

int area;

Rect1.setWidth(5);

Rect1.setHeightt(7);

area = Rect1.getArea();

cout << “Total area is: ” << Rect1.getArea() << endl;

cout << “Total paint cost is: $” << Rect1.getCost(area) << endl; return 0;

}

Output
Total area is:35

Total paint cost is:$ 2450

Example 2

#include<iostream.h>

#include <conio.h>

class student

{

protected: int rno,m1,m2;

public: void get()

{

cout<<“Enter the Roll no :”;

cin>>rno;

cout<<“Enter the two marks :”;

cin>>m1>>m2;

}

};

class sports

{

protected: int sm;

// sm = Sports mark public: void getsm()

{

cout<<“\nEnter the sports mark :”;

cin>>sm;

}

};

class statement:public student,public sports

{

int tot,avg;

public: void display()

{

tot=(m1+m2+sm);

avg=tot/3;

cout<<“\n\n\tRoll No : “<<rno<<“\n\tTotal : “<<tot; cout<<“\n\tAverage : “<<avg;

}

};

void main()

{

clrscr();

statement obj;

obj.get();

obj.getsm();

obj.display();

getch();

}

Output
Enter the Roll no : 12

Enter the two marks :89

90

Enter the sports mark :4

Roll No :12

Total :183

Average :61

 

Leave a Reply

Your email address will not be published. Required fields are marked *