0% found this document useful (0 votes)
15 views16 pages

Lab - 07

lab 7 of oop

Uploaded by

Atta Ur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

Lab - 07

lab 7 of oop

Uploaded by

Atta Ur Rahman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

CL217 - Object Oriented Programming Lab Lab Manual - 07

National University of Computer & Emerging Sciences,


Karachi
Computer Science Department
Spring 2021, Lab Manual - 07
Course Code: CL- Course : Object Oriented Programming
217 Lab
Instructor(s) : Nida Munawar, Abeer Gauher,
Romasha Khurshid, M. Fahim, Sohail
Afzal, Qaiser Abbas, Ali Fatmi

Contents:
1. Introduction to Inheritance
2. Types of Inheritance based on Base Class Access Control
3. Types of Inheritance based on Derived Classes
4. UML Representation for Inheritance
5. Lab Tasks

INTRODUCTION TO INHERITANCE
Inheritance is one of the key features of Object-oriented programming in C++. It allows
us to create a new class (derived class) from an existing class (base class).

Base Class:
 A base class is the class from which features are to be inherited into another
class.

Derived Class:
 A derived class is the one which inherits features from the base class. It can
have additional properties and methods that are not present in the parent class
that distinguishes it and provides additional functionality.

Real World Example:


 A real world example of inheritance constitutes the concept that children inherit
certain features and traits from their parents. In addition, children also have
their unique features and traits that distinguishes them from their parents.

Basic syntax for Inheritance:


class derived-class-name : access base-class-name {
// body of class
};

1 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

TYPES OF INHERITANCE BASED ON BASE CLASS


ACCESS CONTROL
There are three types of inheritance with respect to base class access control:
 Public
 Private
 Protected

Public Inheritance:
 With public inheritance, every object of a derived class is also an object of that
derived class’s base class. However, base class objects are not objects of their
derived classes.

Is – A Relationship:
 Inheritance is represented by an is-a relationship which means that an object of
a derived class also can be treated as an object of its base class for example, a
Car is a Vehicle, so any attributes and behaviors of a Vehicle are also attributes
and behaviors of a Car.

Syntax for public Inheritance:

Class (name of the derived class) : public (name of the base class)
Class Car : public Vehicle

Base Class Access Control for Public, Private and Protected:

Visibility of Types of Inheritance


Base Class
Members
Public Private Protected
Inheritance Inheritance Inheritance
Public Public in Private in Protected in
derived class derived class derived class
Private Hidden in Hidden in Hidden in derived
derived class derived class class
Protected Protected in Hidden in Protected in
derived class derived class derived class

2 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

TYPES OF INHERITANCE BASED ON DERIVED


CLASSES
Inheritance based on derived classes can be categorized as follows:
 Single Inheritance
 Multiple Inheritance
 Multilevel Inheritance
 Hierarchical Inheritance
 Hybrid Inheritance

Single Inheritance:
 In this type of inheritance there is one base class and one derived class.
 As shown in the figure below, in single inheritance only one class can be
derived from the base class. Based on the visibility mode used or access
specifier used while deriving, the properties of the base class are derived.

Syntax for single Inheritance:

3 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

C class A // base class


{ {
X // body of the class
}; };
class B : acess_specifier A // derived class
{ {
// body of the class
};
};

Example code for single Inheritance:


#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()
{
cout << "Enter the value of x = "; cin >> x;
}
};
class derive : public base //single derived class
{
private:
int y;
public:
void readdata()
{
cout << "Enter the value of y = "; cin >> y;
}
void product()
{
cout << "Product = " << x * y;
}
};

int main()
{
derive a; //object of derived class
a.getdata();
a.readdata();

4 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

a.product();
return 0;
} //end of program

Sample Run
Enter the value of x = 3
Enter the value of y = 4
Product = 12

Multiple Inheritance:
 In multiple inheritance, a class is derived from two or more base classes. In
multiple inheritance a derived class has more than one base class.
 As shown in the figure below, class C is derived from two base classes A and
B.

5 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

Syntax for multiple Inheritance:

C class A // base class


{
X // body of the class
};
class B // base class
{ {
X // body of the class
}; };
class C : acess_specifier A, acess_specifier B // derived class
{ {
// body of the class
};
};

Example code for multiple Inheritance:

#include

6 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

using namespace std;


class A // base class
{
public:
int x;
void getx()
{
cout << "enter value of x: "; cin >> x;
}
};
class B // base class
{
public:
int y;
void gety()
{
cout << "enter value of y: "; cin >> y;
}
};
class C : public A, public B //C is derived from class A and
class B
{
public:
void sum()
{
cout << "Sum = " << x + y;
}
};
int main()
{
C obj1; //object of derived class C
obj1.getx();
obj1.gety();
obj1.sum();
return 0;
} //end of program

Sample Run
enter value of x: 5
enter value of y: 4
Sum = 9

Multilevel Inheritance:
 If a class is derived from another derived class then it is called multilevel
inheritance, so in multilevel inheritance, a class has more than one parent class.
 As shown in the figure below, class C has class B and class A as parent classes.
 As in other inheritance, based on the visibility mode used or access specifier
used. while deriving, the properties of the base class are derived. Access
specifier can be private, protected or public.

7 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

Syntax for multilevel Inheritance:


C class A // base class
{
X // body of the class
};
class B : acess_specifier A // derived class
{ {
X // body of the class
}; };
class C : acess_specifier B // derived from class B
{ {
// body of the class
};
};

Example code for multilevel Inheritance:


#include <iostream>
using namespace std;
class base //single base class
{
public:
int x;
void getdata()

8 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

{
cout << "Enter value of x= "; cin >> x;
}
};
class derive1 : public base // derived class from base class
{
public:
int y;
void readdata()
{
cout << "\nEnter value of y= "; cin >> y;
}
};
class derive2 : public derive1 // derived from class derive1
{
private:
int z;
public:
void indata()
{
cout << "\nEnter value of z= "; cin >> z;
}
void product()
{
cout << "\nProduct= " << x * y * z;
}
};
int main()
{
derive2 a; //object of derived class
a.getdata();
a.readdata();
a.indata();
a.product();
return 0;
} //end of program

Sample Run
Enter value of x= 2
Enter value of y= 3
Enter value of z= 3
Product= 18

Hierarchical Inheritance:
 When several classes are derived from a common base class it is called as
hierarchical inheritance.

9 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

 In C++ hierarchical inheritance, the feature of the base class is inherited onto
more than one sub-class.
 For example, a car is a common class from which Audi, Ferrari, Maruti etc can
be derived.
 As shown in the figure below, in C++ hierarchical inheritance all the derived
classes have a common base class. The base class includes all the features
that are common to derived classes.

Syntax for hierarchical Inheritance:


C class A // base class
{
X // body of the class
};
class B : acess_specifier A // derived class from A
{ {
X // body of the class
}; };
class C : acess_specifier A // derived class from A
{ {
// body of the class
};
} ; class D : acess_specifier A // derived class from A
{ {
// body of the class
};
Example code for hierarchical Inheritance:
#include <iostream>
using namespace std;

class A //single base class

10 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

{
public:
int x, y;
void getdata()
{
cout << "\n Enter value of x and y:\n"; cin >> x >> y;
}
};
class B : public A //B is derived from class base
{
public:
void product()
{
cout << "\n Product= " << x * y;
}
};
class C : public A //C is also derived from class base
{
public:
void sum()
{
cout << "\n Sum= " << x + y;
}
};
int main()
{
B obj1; //object of derived class B
C obj2; //object of derived class C
obj1.getdata();
obj1.product();
obj2.getdata();
obj2.sum();
return 0;
} //end of program

Sample Run
Enter value of x and y:
2
3
Product= 6
Enter value of x and y:
2
3
Sum= 5

Hybrid Inheritance:
 The inheritance in which the derivation of a class involves more than one
form of any inheritance is called hybrid inheritance.

11 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

 Basically C++ hybrid inheritance is combination of two or more types of


inheritance. It can also be called multi path inheritance.
 The figure below shows the hybrid combination of single inheritance and
multiple inheritance. Hybrid inheritance is used in a situation where we need
to apply more than one inheritance in a program.

Syntax for
hybrid Inheritance:
C class A // base class
{
X // body of the class
};
class B : public A
{ {
X // body of the class
}; };
class C
{ {
// body of the class
};
} ; class D : public B, public C
{ {
// body of the class
};

Example code for hybrid Inheritance:


#include <iostream>
using namespace std;

class A
{
public:
int x;
};
class B : public A

12 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

{
public:
B() //constructor to initialize x in base class A
{
x = 10;
}
};
class C
{
public:
int y;
C() //constructor to initialize y
{
y = 4;
}
};
class D : public B, public C //D is derived from class B and class C
{
public:
void sum()
{
cout << "Sum= " << x + y;
}
};

int main()
{
D obj1; //object of derived class D
obj1.sum();
return 0;
} //end of program
Sample Run
Sum= 14

13 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

UML REPRESENTATION FOR INHERITANCE


An association line tipped with an open arrowhead is drawn from the subclass to the
base class. The figure below shows a class diagram for two classes named BaseClass
and DerivedClassOne.

BaseClass is referred
to as the base class and DerivedClassOne is referred to as the derived class or

BaseClass contains two private attributes. One is a static, class-wide variable named
count, and the other is an integer variable named its_count. BaseClass contains four
public functions and one protected function.

14 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

LAB TASKS:

Task - 01:
A school library wants to organize its library system by categorizing books according to
their genre. They need an automated system that will allow them to input the details of
the books that are in their library. To do this, you need to implement a program that
contains a base class called Books that will contain a data member to store the genre
of the book. Derive two other classes from the base class and name them accordingly.
Each of these two classes will hold details about a book from a specific genre of your
choice such as Novel, Narrative, Mystery and so on. The derived class will contain data
members to store the title and the author of the book. Display the details of each book
along with their genre.

Task - 02:
A vehicle company is deciding to hire a programmer to develop a system that will allow
the company to enter the details of the vehicles sold by them. As a programmer, you
need to implement a program that contains a base class called Vehicles that contains
a data member to store the price of the vehicles. Derive two other classes named as
Car and Motorcycle.
 The Car class will contain data members to store details that include seating
capacity, number of doors and fuel type (diesel or petrol).
 The Motorcycle class will contain data members to store details such as the
number of cylinders, the number of gears and the number of wheels.
Derive another subclass named as Audi of Car and Yamaha of Motorcycle.
 The Audi class will contain a data member to store the model type.
 The Yamaha class will contain a data member to store the make – type.
Display the details of an Audi car (price, seating capacity, number of doors, fuel type,
model type) and the details of the Yamaha motorcycle (price, number of cylinders,
number of gears, number of wheels, make – type).

Task - 03:
A university is deciding to upgrade its system. In order to upgrade, you need to
implement the following scenario as shown in the figure:
Note the following:
 The class student has a function that displays all the information about the
student.
 Class marks is derived from class student and has a function that displays all the
marks obtained in the courses by the students.
 Class result is derived from class marks. This class has a function that calculates
the total marks and then calculates the average marks. It then displays both the
total and the average marks.
 In the main function you are required to do the following:
 Create an object of the result class.
 Then display the student details, the marks obtained in each courses and
the total and the average marks.

15 OF 15
CL217 - Object Oriented Programming Lab Lab Manual - 07

C class student{
int id;
string name;
public:
void
getstudentdetails(){
}
};

C class marks : public


student{
protected:
int marks_oop,
marks_pf, marks_ds,
marks_db;
public:
void getmarks(){
}

C class result : public


marks{
protected:
int total_marks;
double avg_marks;
public:
void display(){
}
};

16 OF 15

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy