You are on page 1of 5

Object Oriented Programming

Lab Manual

Topic: Multiple Inheritance

Lab Instructor:Ahmad Abduhu

Session: Spring 2020

School of Systems and Technology


UMT Lahore Pakistan

1
Objectives:
In this Lab we’ll discuss that how multiple inheritance enables a derived class to inherit members from
more than one parent, which symbol is used to perform multiple inheritance. Multiple Inheritance can make code
harder to tackle.

Multiple Inheritance:

Multiple inheritance occurs when the relationship involves multiple parents and a child. In other words, the
child inherits from more than one parent.

The following diagram depicts this inheritance:

2
Sample Code:

#include <iostream>

using namespace std;

// Base class Shape


class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};

// Base class PaintCost


class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};

// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};

3
int main(void)
{
Rectangle Rect;
int area;

Rect.setWidth(5);
Rect.setHeight(7);

area = Rect.getArea();

// Print the area of the object.


cout<< "Total area: " <<Rect.getArea() <<endl;

// Print the total cost of painting


cout<< "Total paint cost: $" <<Rect.getCost(area) <<endl;

return 0;
}

4
Lab Tasks
Task 1:
Create Class Person with variables weight,height, gender. Create another Class Employee with variables
designation, HoursPerDay. Now create another class Teacher and inherit it from Person and Employee and
add function display() which should show all the details related to teacher.

Task 2:
Create Class Person with variables weight,height, gender and functions walk() sit(). Ceate another Class
Sudent with variable ID,First name, Last name and function PrintDetail() and Write(). Now create a class
GraduationStudent with variables UniversityName, YearGraduation and functions Display().
Note: In both parent classes simply add a statement about the task. In child display function show all data

Task 3:
Create Class Date with variables day,month,year and a function display() and another class Time with
variables hour,minutes,seconds and a function display(). Now create a third class named Time_Date it will
inherit from both Classes Date and Time it should have a function display() which will display time and
date.

Task 4:
Create class named shape with variables height and width. Create another class named color with variable
color_name. Now create third class named Rectangle with variable area. Inherit rectangle class from shape
class and color class. Now calculate area of rectangle and define color of rectangle as well.

You might also like