[go: up one dir, main page]

0% found this document useful (0 votes)
101 views54 pages

Unit 5 - Inheritance

The document discusses inheritance in C++. It defines inheritance as a mechanism where objects of one class acquire properties of another class. There are different types of inheritance like single, multiple, multilevel and hierarchical inheritance. The key advantages of inheritance are reusability of code and rapid extension of applications. Inheritance allows defining data members and functions in base classes that can be inherited in derived classes.

Uploaded by

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

Unit 5 - Inheritance

The document discusses inheritance in C++. It defines inheritance as a mechanism where objects of one class acquire properties of another class. There are different types of inheritance like single, multiple, multilevel and hierarchical inheritance. The key advantages of inheritance are reusability of code and rapid extension of applications. Inheritance allows defining data members and functions in base classes that can be inherited in derived classes.

Uploaded by

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

INHERITANCE

INTRODUCTION
▪ Inheritance is the feature by which objects of one class
acquire the properties of objects of another class
▪ It is the mechanism of deriving a new class from an old one
▪ It supports the concept of hierarchical classification
▪ The concept of inheritance provides the idea of reusability
▪ This means we can add additional features to an existing
class without modifying it

1
Prepared by Sherin Joshi
INTRODUCTION
▪ This is possible by deriving a new class from an existing
one
▪ The new derived class will have the features of the existing
class as well as its own unique features
▪ Inheritance allows the implementation of data members or
member functions that were previously defined

2
Prepared by Sherin Joshi
ADVANTAGES
▪ The application can be built over previously available
module
▪ Extension of any application is rapid
▪ The codes that were previously tested can be used as the
foundation for newer modules
▪ Time and cost of development is controlled

3
Prepared by Sherin Joshi
4
Prepared by Sherin Joshi
5
Prepared by Sherin Joshi
6
Prepared by Sherin Joshi
SYNTAX
class derived_class_name : visibility_mode class base_class_name
{
//members of derived class
<optional>
private:
………
protected:
………
public:
………
};
7
Prepared by Sherin Joshi
SYNTAX
▪ base_class_name – The name of the base/parent/super
class from which the new class will inherit its features
▪ derived_class_name – The name of the derived/child/sub
class which acquires the properties of the old existing class
▪ visibility_mode
▪ Determines the mode of derivation of features into the
child class
▪ Can be private, protected or public
▪ The default visibility mode is private(optional, if not
written, it is considered private)
8
Prepared by Sherin Joshi
TYPES OF INHERITANCE
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

9
Prepared by Sherin Joshi
SINGLE INHERITANCE
▪ A derived class with only one base class

10
Prepared by Sherin Joshi
SINGLE INHERITANCE
▪ Syntax:
class ClassA
{ Can be any among
private/public/protected
………..
};
class ClassB : public class ClassA
{
optional
………..
}; 11
Prepared by Sherin Joshi
MULTIPLE INHERITANCE
▪ A derived class with several base classes

12
Prepared by Sherin Joshi
MULTIPLE INHERITANCE
▪ Syntax:
class ClassB class ClassA : public ClassB, public ClassC
{ {
……….. ………..
}; };
class ClassC
{
………..
}; 13
Prepared by Sherin Joshi
MULTILEVEL INHERITANCE
▪ Deriving a class from another derived class

14
Prepared by Sherin Joshi
MULTILEVEL INHERITANCE
▪ Syntax:
class ClassC class ClassA : public ClassB
{ {
……….. ………..
}; };
class ClassB : public ClassC
{
………..
};
15
Prepared by Sherin Joshi
HIERARCHICAL INHERITANCE
▪ Traits of one class inherited by more than one class

16
Prepared by Sherin Joshi
HIERARCHICAL INHERITANCE
▪ Syntax:
class A class C : public A
{ {
……….. ………..
}; };
class B : public A class D : public A
{ {
……….. ………..
}; };
17
Prepared by Sherin Joshi
HYBRID INHERITANCE
▪ Combination of two or more types of inheritance

18
Prepared by Sherin Joshi
MODES OF INHERITANCE

19
Prepared by Sherin Joshi
//Inheritance Demo
//Single Inheritance EMPLOYEE
#include <iostream>
using namespace std;
class Employee{
private: MANAGER
int eno;
string ename;
public:
void getEmp( );
void setEmp( );
}; 20
Prepared by Sherin Joshi
class Manager : public Employee {
private:
string dept;
public:
void getMan( );
void setMan( );
};
void Employee :: setEmp( ) {
cout << endl << "Enter employee ID: ";
cin >> eno;
cout << endl << "Enter name: ";
cin.ignore( );
21
getline(cin,ename); } Prepared by Sherin Joshi
void Employee :: getEmp( )
{
cout << endl << "ID: " << eno;
cout << endl << "Name: " << ename;
}
void Manager :: setMan( )
{
cout << endl << "Enter department: ";
getline(cin,dept);
}
void Manager :: getMan( )
{
cout << endl << "Department: " << dept;
} 22
Prepared by Sherin Joshi
int main( )
{
Manager obj; //Creating object of derived class
obj.setEmp( );
obj.setMan( );
obj.getEmp( );
obj.getMan( );
return 0;
}

23
Prepared by Sherin Joshi
#include <iostream>
using namespace std; FRUIT VEGETABLE

class Vegetable {
public:
TOMATO
void makeCurry( ) {
cout << endl << "Steps to make curry."; }
};
class Fruit {
public:
void makeSalad( ) {
cout << endl << "Steps to make salad."; }
}; 24
Prepared by Sherin Joshi
class Tomato : public Vegetable, public Fruit {
private:
string color; float weight; string type;
public:
void getInfo( ) {
cout << endl << "Enter color: ";
getline(cin,color);
cout << endl << "Enter weight: ";
cin >> weight;
cout << endl << "Enter type: ";
cin.ignore( );
getline(cin,type); } 25
Prepared by Sherin Joshi
void showInfo( ) {
cout << endl << "Color: " << color;
cout << endl << "Weight: " << weight;
cout << endl << "Type: " << type; }
};
int main( ) {
Tomato t;
t.getInfo( );
t.showInfo( );
t.makeCurry( );
t.makeSalad( );
return 0; } 26
Prepared by Sherin Joshi
Classwork
Write a program for the following scenario of multilevel
inheritance:
LIVING BEING

ANIMAL

CARNIVORE 27
Prepared by Sherin Joshi
CONSTRUCTORS & DESTRUCTORS
IN DERIVED CLASSES
▪ When we create an object of a derived class, the constructor of the
base class gets automatically called first and only then the
constructor of the derived class gets called
▪ eg: class A{ class B : public A{
public: public:
A( ){ B( ){
//some code //some code
} }
}; };
28
Prepared by Sherin Joshi
▪ Here, when we create an object of B in main function:
B obj;
▪ Order of constructor call will be A( )→B( )

▪ In case of multilevel inheritance A→B→C, when we create


an object of C, the order of constructor call will be A( )→B( )
→C( )
▪ That is, the constructors will be executed in the order of
inheritance
▪ In multiple inheritance, the base classes are constructed in
the order in which they appear in the declaration of the
derived class

29
Prepared by Sherin Joshi
▪ eg: class A { class B {
public: public:
A( ) { B( ) {
……. …….
} }
}; };

class C : public B, public A {


public:
C( ){
…….
}
};
▪ Here, order of constructor call will be B( )→A( )→C( ) 30
Prepared by Sherin Joshi
CONSTRUCTORS AND
DESTRUCTORS IN DERIVED
CLASSES
▪ The order of call for destructors is exactly the reverse order
of the order of constructor call
▪ This is because the top most base class is the last one to be
removed from the memory heap whereas the derived class is
the first
▪ For instance, in the above scenario of multiple inheritance,
the order of call for destructors would be ~C( )→~A( )→~B( )

31
Prepared by Sherin Joshi
CONSTRUCTORS AND
DESTRUCTORS IN DERIVED
CLASSES
▪ As long as no base class constructor takes any arguments,
the derived class need not have a constructor function
▪ However, if any base class contains a constructor with one
or more arguments, then it is mandatory for the derived
class to have a constructor and pass the arguments to the
base class constructors

32
Prepared by Sherin Joshi
//Constructors in derived classes
#include <iostream>
using namespace std;

class Alpha {
private:
int x;
public:
Alpha(int i) {
x = i;
cout << "Alpha Initialized" << endl;
} 33
Prepared by Sherin Joshi
void showX( ) {
cout << "x = " << x << endl;
}
};
class Beta {
private:
float y;
public:
Beta(float j) {
y = j;
cout << "Beta Initialized" << endl;
} 34
Prepared by Sherin Joshi
void showY( ) {
cout << "y = " << y << endl;
}
};
class Gamma : public Beta,public Alpha {
private:
int m,n;
public:
Gamma(int a, float b, int c, int d):Alpha(a),Beta(b) {
m = c, n = d;
cout << "Gamma Initialized" << endl;
} 35
Prepared by Sherin Joshi
void showMN( ) {
cout << "m = " << m << endl << "n = " << n << endl;
}
};
int main( ) {
Gamma g(5,10.75,20,30);
g.showX( );
g.showY( );
g.showMN( );
return 0;
}
36
Prepared by Sherin Joshi
AMBIGUITY IN MULTIPLE
INHERITANCE
(1) If two base classes have functions with the same name,
there will be ambiguity when a derived class object tries to
access any one of these functions normally

37
Prepared by Sherin Joshi
#include <iostream>
using namespace std;

class A
{
public:
void show( )
{
cout << "Class A" << endl;
}
};
38
Prepared by Sherin Joshi
class B
{
public:
void show( )
{
cout << "Class B" << endl;
}
};

class C:public A,public B


{ };
39
Prepared by Sherin Joshi
int main( )
{
C objC;
objC.show( ); //ambiguous, will not compile
objC.A::show( );
objC.B::show( );
return 0;
}

40
Prepared by Sherin Joshi
AMBIGUITY IN MULTIPLE
INHERITANCE
(2) There will also be ambiguity in cases of diamond shaped
inheritance like:
show( )

show( ) show( )

41
Prepared by Sherin Joshi
this POINTER
▪ C++ uses a unique keyword called this to represent an
object that invokes a member function
▪ this is a pointer that points to the object for which this
function was called
▪ It is a unique pointer that is automatically passed to a
member function when it is called
▪ For example, the function call obj.max( ) will set the
pointer this to the address of the object obj

42
Prepared by Sherin Joshi
this POINTER
▪ The pointer this acts as an implicit argument to all member
functions
▪ eg: class ABC
{
private:
int a;
……
};
43
Prepared by Sherin Joshi
this POINTER
▪ The private variable ‘a’ can be used directly inside a
member function like:
a = 123;
▪ We can also use the following statement to do the same job:
this -> a = 123;
▪ Another important use of this is to return the object it
points to
▪ For eg, the statement “return *this;” will return the object
that invoked the function
44
Prepared by Sherin Joshi
//this Pointer Demo
#include <iostream>
using namespace std;

class Check {
private:
int var;
public:
Check( ) { }
Check(int var) {
this -> var = var;
} 45
Prepared by Sherin Joshi
Check send( ) {
return *this;
}
void show( ) {
cout << "var = " << var; }
};
int main( ) {
Check var(44);
Check ans;
ans = var.send( );
ans.show( );
return 0;
} 46
Prepared by Sherin Joshi
AGGREGATION (CLASSES
WITHIN CLASSES)
▪ Aggregation is called a “has a” relationship
▪ eg: LIBRARY has a BOOK

▪ Also called a “part-whole” relationship


▪ eg: This book is a part of the library

▪ In OOP, aggregation occurs when one object is an attribute


of another
47
Prepared by Sherin Joshi
//Aggregation demo
#include <iostream>
using namespace std;
class Address {
private:
string country, city, zone;
public:
Address(string city, string zone, string country) {
this->city = city;
this->zone = zone;
this->country = country;
} 48
Prepared by Sherin Joshi
void disp( ) {
cout << city << " " << zone << " " << country << endl;
}
};

class Employee
{
private:
int id;
string name;
Address *address; //Employee HAS-A Address
49
Prepared by Sherin Joshi
public:
Employee(int id, string name, Address* address) {
this->id = id;
this->name = name;
this->address = address;
}
void display( ) {
cout << id << " " << name << " ";
address->disp( );
}
};
50
Prepared by Sherin Joshi
int main( )
{
Address a1 = Address("Kathmandu","Bagmati","Nepal");
Employee e1 = Employee(101,"Alisson", &a1);
e1.display( );
return 0;
}

51
Prepared by Sherin Joshi
CLASSWORK
Write a program for the following aggregation scenario:

LIBRARY has a BOOK

52
Prepared by Sherin Joshi
CONTAINERSHIP/CONTAINER
CLASSES
▪ Self Study

53
Prepared by Sherin Joshi

You might also like