0% found this document useful (1 vote)
139 views

Inheritance and Polymorphism: (Multiple Inheritance, Diamond Problem)

This document discusses inheritance and polymorphism in C++, specifically multiple inheritance and the diamond problem. It provides 4 tasks to implement classes with inheritance and polymorphism: 1. Implement Shape, Rectangle, and Triangle classes to demonstrate inheritance and polymorphism with virtual functions. 2. Implement Person, Employee, Manager, and Clerk classes to demonstrate inheritance and composition. 3. Implement classes to demonstrate multiple inheritance and solutions to the diamond problem. 4. Implement Teacher, Classroom, and Course classes with various constructors and overloaded operators to allow dynamic object creation based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
139 views

Inheritance and Polymorphism: (Multiple Inheritance, Diamond Problem)

This document discusses inheritance and polymorphism in C++, specifically multiple inheritance and the diamond problem. It provides 4 tasks to implement classes with inheritance and polymorphism: 1. Implement Shape, Rectangle, and Triangle classes to demonstrate inheritance and polymorphism with virtual functions. 2. Implement Person, Employee, Manager, and Clerk classes to demonstrate inheritance and composition. 3. Implement classes to demonstrate multiple inheritance and solutions to the diamond problem. 4. Implement Teacher, Classroom, and Course classes with various constructors and overloaded operators to allow dynamic object creation based on user input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 4

Inheritance and Polymorphism

(Multiple inheritance, Diamond Problem)

Task 1: Implement this class carefully by understanding the nature


of the problem. Write main to depict its functionality.
Shape
public:
int getArea(); //virtual function (return multiplication of
width and height)
//Setters, Getters
protected:
int width, height;

Rectangle (Child Class) : public Shape


public:
int getArea();
Triangle (Child Class)
public:
int getArea();

Task 2:
Implement this class and check requirements (at Task 3)
Person
//content of Person
Employee // Employee is a person
Private:
string fName, lName;
double salary;

public: Employee(string fName, string lName, double salary);


void show();
void addBonus(double bonus);
//salary + bonus
//getters,settes
Manager //Manager is an Employeee
public: Manager(string fName, string lName, double salary, double
commission)
//setters,getters

Clerk (Every clerk has one manager) //composition


public:
Clerk(string fName, string lName, double sal, Manager man)
Manager manager;
//setters
Manager getManager();

void congratulate(Employee emp) //Global Function


{
cout << "Happy Birthday!!!" << endl;
emp.addBonus(200);
emp.show();
};

int main()
{
//pointer to base class object
Employee* emp;

//object of derived class


Manager m1("Steve", "Kent", 3000, 0.2);
Clerk c1("Kevin","Jones", 1000, &m1);

Task 3:
Requirements:
Implement the class by using some basic private members according
to the class type, default and parametrized constructors and call each
constructor.
Submit one cpp file with simple multiple inheritance and the diamond
problem.
Submit second with multiple inheritance and the solution of the
diamond problem.

-Task3DiamondProblem.cpp
-Task3DiamondProblemSolution.cpp
Task 4:

Write a C++ program which should perform the following tasks:


i. Create a class named Teacher. It contains the name of the teacher
(string) and the teacher’s office extension number (an integer) as
private member variables. Its only constructor requires the values of
above two member variables as arguments.
ii. Create a class named Classroom. It contains a room number (an
integer) and the capacity (an integer) as private member variables,
and its only constructor requires values of both as arguments.
iii. Create a class named Course. A Course contains a course title (a
string), a Teacher, and a Classroom. Its constructor should require
these 5 parameters: course title, the name and office extension
number of the Teacher, and the room number and capacity of the
Classroom.
iv. Each of the above three classes should have a member function
display which should display all attributes of a given object on
screen.
v. Implement the Copy constructor and Overloaded assignment
operator for each of the above three classes.
vi. Write a main() function that creates at two Course objects
containing different values. Then, the main() function should ask the
user about all details of a course and create a Course object using
the user-given input. Then, the main() function should ask the user
about all details of another course and dynamically create a Course
object using the user-given input. Finally, the details of all created
Course should be displayed on screen.
Now, implement another overloaded constructor for the Course
class. This constructor should take these 3 parameters: course title,
a constantant reference to a Teacher object, and a constant
reference to a ClassRoom object. Then, write a main() function to
test the working of this 3-parameter overloaded constructor as you
did in step (vi)

You might also like