0% found this document useful (0 votes)
332 views

OOP Lab-6

This document is a lab manual on object oriented programming in C++ authored by Marriam Nawaz. It discusses topics like inheritance, levels of inheritance, and multiple inheritance. It provides examples of deriving new classes like a foreman class from a laborer class. It also gives an example of multiple inheritance where manager and scientist classes inherit from both employee and student classes. The document assigns class tasks to create inheritance hierarchies and home tasks to add new features like compensation and publication dates using inheritance.

Uploaded by

Saba Ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
332 views

OOP Lab-6

This document is a lab manual on object oriented programming in C++ authored by Marriam Nawaz. It discusses topics like inheritance, levels of inheritance, and multiple inheritance. It provides examples of deriving new classes like a foreman class from a laborer class. It also gives an example of multiple inheritance where manager and scientist classes inherit from both employee and student classes. The document assigns class tasks to create inheritance hierarchies and home tasks to add new features like compensation and publication dates using inheritance.

Uploaded by

Saba Ch
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

DEPARTMENT OF COMPUTER SCIENCE


http://web.uettaxila.edu.pk

CS-103 | Object Oriented Programming (Lab)

Classes and Objects in C++

Instructor
Marriam Nawaz
[email protected]

Lab Manual: CS-103 (Object Orient Programming) Marriam Nawaz


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
http://web.uettaxila.edu.pk

Topics: Inheritance
a. Inheritance
Inheritance is a way of creating a new class by starting with an existing class and
adding new members. The new class can replace or extend the functionality of the
existing class. The existing class is called the base class and the new class is called the
derived class.
b. Levels of Inheritance
Classes can be derived from classes that are themselves derived. Here’s a mini-program that
shows the idea:
class A
{ };
class B : public A
{ };
class C : public B
{ };
Here B is derived from A, and C is derived from B. The process can be extended to an arbitrary
number of levels—D could be derived from C, and so on.
c. Multiple Inheritance
A class can be derived from more than one base class. This is called multiple inheritance.
Figure shows how this looks when a class C is derived from base classes A and B.

The syntax for multiple inheritance is similar to that for single inheritance. In the
situation shown in Figure, the relationship is expressed like this:
class A // base class A
{
};
class B // base class B
{
};

Lab Manual: CS-103 (Object Orient Programming) Marriam Nawaz


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
http://web.uettaxila.edu.pk

class C : public A, public B // C is derived from A and B


{
};
The base classes from which C is derived are listed following the colon in C’s
specification; they are separated by commas.
Class Tasks:
1. As a more concrete example, suppose that we decided to add a special kind of laborer called a
foreman to the EMPLOY program. We’ll create a new program, EMPLOY2 that incorporates
objects of class foreman. Since a foreman is a kind of laborer, the foreman class is derived from
the laborer class, as shown in Figure:

2. Member Functions in Multiple Inheritance


As an example of multiple inheritance, suppose that we need to record the educational
experience of some of the employees in the EMPLOY program. Let’s also suppose that, perhaps
in a different project, we’ve already developed a class called student that models students with
different educational backgrounds. We decide that instead of modifying the employee class to
incorporate educational data, we will add this data by multiple inheritance from the student
class. The student class stores the name of the school or university last attended and the
highest degree received. Both these data items are stored as strings. Two member functions,
getedu() and putedu(), ask the user for this information and display it. Educational information
is not relevant to every class of employee. Let’s suppose, somewhat undemocratically, that we
don’t need to record the educational experience of laborers; it’s only relevant for managers
and scientists. We therefore modify manager and scientist so that they inherit from both the
employee and student classes, as shown in Figure:

Lab Manual: CS-103 (Object Orient Programming) Marriam Nawaz


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
DEPARTMENT OF COMPUTER SCIENCE
http://web.uettaxila.edu.pk

Home Tasks:
1. Derive a class called employee2 from the employee class in the EMPLOY program in this
chapter. This new class should add a type double data item called compensation, and also
an enum type called period to indicate whether the employee is paid hourly, weekly, or
monthly. For simplicity you can change the manager, scientist, and laborer classes so they
are derived from employee2 instead of employee. However, note that in many
circumstances it might be more in the spirit of OOP to create a separate base class called
compensation and three new classes manager2, scientist2, and laborer2, and use multiple
inheritance to derive these three classes from the original manager, scientist, and laborer
classes and from compensation. This way none of the original classes needs to be
modified. Write a main() program to test the book and tape classes by creating instances
of them, asking the user to fill in data with getdata(), and then displaying the data with
putdata().
2. Start with the publication, book, and tape classes of Exercise 1. Suppose you want to add
the date of publication for both books and tapes. From the publication class, derive a new
class called publication2 that includes this member data. Then change book and tape so
they are derived from publication2 instead of publication. Make all the necessary changes
in member functions so the user can input and output dates along with the other data.
For the dates, you can use the date class from Exercise 5 in Chapter 6, which stores a date
as three ints, for month, day, and year.

Lab Manual: CS-103 (Object Orient Programming) Marriam Nawaz

You might also like