Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

HITEC UNIVERSITY, TAXILA

FACULTY OF ELECTRICAL ENGINEERING

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:

The basic syntax of inheritance is:

class DerivedClass : accessSpecifier BaseClass

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

Inheritance Visibility Mode


Depending on Access modifier used while inheritance, the availability of class members of Super
class in the sub class changes. It can either be private, protected or public.

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.

There are three Modes of inheritance in C++

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.

class DerivedClass : public BaseClas

Accessing Base class members public protected private


From Base class Yes Yes Yes
From object of a Base class Yes No No
From Derived classes Yes (As Public) Yes (As Protected) No
From object of a Derived class Yes No No
From Derived class of Derived Classes Yes (As Public) Yes (As Protected) No

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

Then public and protected members of class A will be accessible in class C as public and
protected respectively.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

Protected Inheritance:

In protected mode, the public and protected members of Base class becomes protected members
of Derived class.

class DerivedClass : protected BaseClass

Accessing Base class members Public protected private


From Base class Yes Yes Yes
From object of a Base class Yes No No
From Derived classes Yes (As Protected) Yes (As Protected) No
From object of a Derived class No No No
From Derived class of Derived Classes Yes (As Protected) Yes (As Protected) No

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.

class DerivedClass : private BaseClass


class DerivedClass : BaseClass // By default inheritance is private

Accessing Base class members public protected private


From Base class Yes Yes Yes
From object of a Base class Yes No No
From Derived classes Yes (As Private) Yes (As Private) No
From object of a Derived class No No No
From Derived class of Derived Classes No No No
Object Oriented Programming 2nd Semester-EE HITEC University Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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.

Multiple Inheritance
In this type of inheritance a single derived class may inherit from two or more than two base
classes.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

Hierarchical Inheritance
In this type of inheritance, multiple derived classes inherit from a single base class.

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.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

Hybrid (Virtual) Inheritance


Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

Example#1:
Single Inheritance:

class derived-class: access baseA

#include <iostream>
Object Oriented Programming 2nd Semester-EE HITEC University Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

using namespace std;


// Base class
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);
}
};
int main(void)

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

{
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:

class derived-class: access baseA, access baseB....

#include <iostream>
using namespace std;
// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

}
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);
}
};
int main(void)
{
Rectangle Rect;
int area;

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

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

#include <iostream>
using namespace std;
// Base class Shape
class Shape
{
public:
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

height = h;
}
protected:
int width;
int height;
};
// Derived class
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
// Sub class of Derived class PaintCost
class Square: public Rectangle
{
public:
Square() {
cout<<”rectangle has a square”;
}
};

int main(void)
{
Square Sq;

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

int area;
Sq.setWidth(5);
Sq.setHeight(7);
area = Sq.getArea();

// Print the area of the object.


cout << "Total area: " << Sq.getArea() << endl;
return 0;
}

Example # 4:
Inheritance with Constructor and Destructor

#include<iostream>
using namespace std;

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;

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

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) {
c = myc;
cout << "C:" << c << endl;
}
};

void main()
{
cout << "Allocating a B object" << endl;
B b1;
//B b2(4);

cout << "Allocating 1st C object" << endl;


C* c1 = new C;

cout << "Allocating 2nd C object" << endl;


C c2(4, 5);

cout << "Deleting c1 object" << endl;


delete c1;
Object Oriented Programming 2nd Semester-EE HITEC University Taxila
HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

cout << "Quitting" << endl;

system("pause");

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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).

Write the following functions to Cell Phone class


 displayInfo(): to show the cell phone details
 setInfo(): to set the cell phone details
 makeCall(int number): to make a call to the number entered by the user.
 sendSMS(int number, string message): to send sms to number entered by user.

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.

3. Write a code of the following diagrams


I.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

II.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila

You might also like