Object Oriented Programming Lab 9
Object Oriented Programming Lab 9
EE DEPARTMENT
Experiment 09
Implementation of Inheritance
Objective
Objective of this lab session is to understand inheritance, different types of inheritance and how
to implement inheritance in C++.
THEORY
Inheritance
Inheritance is one of the key features of object-oriented programming including C++ which
allows user to create a new class (derived class) from an existing class (base class). The derived
class inherits all features from a base class and it can have additional features of its own.
Inheritance Syntax:
EE DEPARTMENT
Access specifier can be public, protected and private. The default access specifier
is private. Access specifiers affect accessibility of data members of base class from the derived
class. In addition, it determines the accessibility of data members of base class outside the derived
class.
Public Inheritance:
This inheritance mode is used mostly. In this the protected member of Base class becomes
protected members of Derived class and public becomes public.
Derived class of Derived Classes: If we are inheriting a derived class using a public inheritance
as shown below
class B : public A
class C : public B
EE DEPARTMENT
Then public and protected members of class A will be accessible in class C as public and
protected respectively.
Protected Inheritance:
In protected mode, the public and protected members of Base class becomes protected members
of Derived class.
Derived class of Derived Classes: If we are inheriting a derived class using a protected
inheritance as shown below
class B : protected A
class C : protected B
then public and protected members of class A will be accessible in class C as protected
Private Inheritance:
In private mode the public and protected members of Base class become private members of
Derived class.
EE DEPARTMENT
Derived class of Derived Classes: If we are inheriting a derived class using a private inheritance
as shown below
class B : private A
class C : private B
Then public and protected members of class A will not be accessible in class C.
Types of Inheritance:
In C++, we have different types of Inheritance. Namely,
Single Inheritance
Multiple Inheritance
Hierarchical Inheritance
Multilevel Inheritance
Hybrid Inheritance (also known as Virtual Inheritance)
Single Inheritance
In this type of inheritance one derived class inherits from only one base class. It is the simplest
form of Inheritance.
EE DEPARTMENT
Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two base
classes.
Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherit from a single base class.
EE DEPARTMENT
Multilevel Inheritance
In this type of inheritance the derived class inherits from a class, which in turn inherits from some
other class. The Super class for one, is sub class for the other.
EE DEPARTMENT
Example#1:
Single Inheritance:
#include <iostream>
using namespace std;
// Base class
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
Object Oriented Programming 2nd Semester-EE HITEC University Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING
EE DEPARTMENT
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
int main(void)
{
Rectangle Rect;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total area: " << Rect.getArea() << endl;
return 0;
}
Example#2:
Multiple Inheritances:
A C++ class can inherit members from more than one class and here is the extended syntax:
EE DEPARTMENT
#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;
}
};
EE DEPARTMENT
// Derived class
class Rectangle: public Shape, public PaintCost
{
public:
int getArea()
{
return (width * height);
}
};
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;
}
Example#3:
Multilevel Inheritances:
class derivedclass: access baseA
class derived-derivedclass: access derivedclass
EE DEPARTMENT
#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;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
EE DEPARTMENT
};
// Sub class of Derived class PaintCost
class Square: public Rectangle
{
public:
Square() {
cout<<”rectangle has a square”;
}
};
int main(void)
{
Square Sq;
int area;
Sq.setWidth(5);
Sq.setHeight(7);
area = Sq.getArea();
Example # 4:
Inheritance with Constructor and Destructor
#include<iostream>
using namespace std;
EE DEPARTMENT
class A {
int a;
public:
A()
{
a = 0; cout << "A:" << a << endl;
}
~A()
{
cout << "~A" << endl;
}
A(int mya) {
a = mya;
cout << "A:" << a << endl;
}
};
class B : public A {
int b;
public:
B() {
b = 0; cout << "B:" << b << endl;
}
~B() {
cout << "~B ";
}
B(int myb) {
b = myb;
cout << "B:" << b << endl;
}
};
class C : public B {
int c;
public:
C() {
c = 0; cout << "C:" << c << endl; }
~C() {
cout << "~C "; }
C(int myb, int myc) : B(myb) {
Object Oriented Programming 2nd Semester-EE HITEC University Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING
EE DEPARTMENT
c = myc;
cout << "C:" << c << endl;
}
};
void main()
{
cout << "Allocating a B object" << endl;
B b1;
//B b2(4);
system("pause");
EE DEPARTMENT
Lab Tasks
1. Amend the above given program for calculating:
Area of Circle
Circumference of Circle
Color of Circle
2. Define a parent class Cell Phone which has the following features Model Number,
Manufacturer, Screen size, Touch Phone, 3GEnabled, and Camera.(choose data type
according to the field).
Define the child/derived class of Nokia, Samsung and Sony which has custom attributes other
than defined in parent class. For example a simple cell phone cannot send MMS .make an
attribute in child class of any(Nokia, Samsung and Sony)names has MMS Support and write a
function to send MMS if it is capable of sending MMS (has MMS Support=true).Override all the
functions of Cell Phone in all child classes.
EE DEPARTMENT
II.