Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
61 views

Object Oriented Programming Lab 9

This document discusses inheritance in C++. It defines inheritance as a key feature of object-oriented programming that allows a new class to inherit properties from an existing class. The document outlines different types of inheritance like single, multiple, hierarchical and multilevel inheritance. It also describes public, protected and private inheritance modes and how they affect accessibility of members of the base class. Code examples of single and multiple inheritance in C++ are provided.

Uploaded by

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

Object Oriented Programming Lab 9

This document discusses inheritance in C++. It defines inheritance as a key feature of object-oriented programming that allows a new class to inherit properties from an existing class. The document outlines different types of inheritance like single, multiple, hierarchical and multilevel inheritance. It also describes public, protected and private inheritance modes and how they affect accessibility of members of the base class. Code examples of single and multiple inheritance in C++ are provided.

Uploaded by

Haristic 01
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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
 Yes (As
 From Derived classes  Yes (As Protected)  No
Public)
 From object of a Derived class  Yes  No  No
 From Derived class of Derived  Yes (As
 Yes (As Protected)  No
Classes 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

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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.

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
 Yes (As  Yes (As
 From Derived classes  No
Protected) Protected)
 From object of a Derived class  No  No  No
 From Derived class of Derived  Yes (As  Yes (As
 No
Classes Protected) Protected)

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

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

 Accessing Base class members  public  protected  private


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

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.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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.

Hybrid (Virtual) Inheritance


Hybrid Inheritance is combination of Hierarchical and Multilevel Inheritance.

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

EE DEPARTMENT

Example#1:
Single Inheritance:

class derived-class: access baseA

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

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

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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;
}
};

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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);
}

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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();

// 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;

Object Oriented Programming 2nd Semester-EE HITEC University Taxila


HITEC UNIVERSITY, TAXILA
FACULTY OF ELECTRICAL ENGINEERING

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

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;

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