SlideShare a Scribd company logo
Inheritance : Extending classesInheritance : Extending classes
By
Nilesh Dalvi
Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College.
http://www.slideshare.net/nileshdalvi01
Object oriented ProgrammingObject oriented Programming
with C++with C++
Introduction
• Inheritance is one of the most useful and
essential characteristics of oops.
• Existing classes are main components of
inheritance.
• New classes are created from existing one.
• Properties of existing classes are simply
extended to the new classes.
• New classes are called as derived classes
and existing one are base classes.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Introduction
Fig. Inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
1. Single Inheritance
2. Multiple Inheritance
3. Hierarchical Inheritance
4. Multilevel Inheritance
5. Hybrid Inheritance
Types of Inheritance
Single inheritance:
A derived class with only one base class is
called as single inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Multiple inheritance:
A derived class with several base classes is
called as Multiple inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Multilevel inheritance:
The mechanism of deriving a class from
another ‘derived class’ is known as multilevel
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Hierarchical inheritance:
One class may be inherited by more than one
class. This process is known as hierarchical
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Types of Inheritance
Hybrid inheritance:
It is combination of Hierarchical and Multilevel
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Defining derived class
• A derived class can be defined by specifying its
relationship with the base class in addition to its
own details.
class derived-class-name : visibility-mode base-class-name
{
//members of derived class.
};
• The colon (:) indicates that derived-class-name is
derived from the base-class-name.
• The visibility-mode is optional and if present may be
either private or public. The default visibility-mode
is private
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
• Visibility-mode specifies whether the features of the base
class are privately derived or publicly derived.
• For Example:
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Defining derived class
class base
{
private:
public:
//members of base class
}
class derived : private base
{
//members of base class
}
class derived : public base
{
//members of base class
}
class derived : base
{
//members of base class
}
• public members of the base class become
private members of the derived class.
• Therefore, the public members of the base
class can only be accessed by the member
functions of the derived class.
• They are not accessible to the object of the
derived class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Privately Inherited
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Privately Inherited
#include <iostream>
using namespace std;
//Public Derivation
class A //Base class
{
public:
int x;
};
class B : private A // Derived class
{
public:
int y;
B()
{
x = 10;
y = 20;
}
void show ()
{
cout << "X : " << x <<endl;
cout << "Y : " << y <<endl;
}
};
int main()
{
B b;
b.show ();
return 0;
}
The object b of derived class
cannot directly access the
variable x.
b.x = 10; // cannot access
class B is privately inherited.
Hence, its access is
restricted.
The object b of derived class
cannot directly access the
variable x.
b.x = 10; // cannot access
class B is privately inherited.
Hence, its access is
restricted.
• public members of the base class remains
public member in derived class.
• Therefore, they are accessible to the objects
of the derived class.
• In both cases, the private members are not
inherited and therefore, the private
members of base class will never become
the members of its derived class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Publicly Inherited
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Publicly Inherited
#include <iostream>
using namespace std;
//Public Derivation
class A //Base class
{
public:
int x;
};
class B : public A // Derived class
{
public:
int y;
};
int main()
{
B b;
b.x = 10;
b.y = 20;
cout << "Member of A :" << b.x <<endl;
cout << "Member of B :" << b.y <<endl;
return 0;
}
In main() function, b is an
object of class B. The object
b can access the members
of class A as well as of B
through the statements:
b.x = 10;
b.y = 20;
In main() function, b is an
object of class B. The object
b can access the members
of class A as well as of B
through the statements:
b.x = 10;
b.y = 20;
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Privately Inherited
#include<iostream>
using namespace std;
class geometry
{
int length;
int breadth;
public:
void get()
{
cout << "Enter values: "<<endl;
cin >> length >> breadth;
}
int area();
};
int geometry :: area ()
{
return length * breadth;
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Privately Inherited
class Rect : private geometry
{
int height;
public:
void getHeight (int h)
{
get();
height = h;
}
void volume();
};
void Rect ::volume()
{
cout << "Area is: " << area ()<< endl;
cout <<"Volume is : "<< area() * height;
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Privately Inherited
int main()
{
Rect r;
r.getHeight (2);
//r.area();
r.volume();
return 0;
}
Output:
Enter values:
2
3
Area is: 6
Volume is: 12
• The member functions of derived class cannot
access the private member variables of base class.
• The private members of base class can be accessed
using public member functions of the same class.
• This approach makes a program lengthy.
• To overcome the problem associated with private
data, the creator of C++ introduced another access
specifier called protected.
• protected is same as private , but it allows the
derived class to access the private members
directly.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Protected data with private inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Protected data with private inheritance
class ABC
{
private: // optional
.... //visible to member functions within class
protected: //visible to member functions of its own
.... // and of its derived class
public: //visible to all functions in the program
.... //
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
#include <iostream>
using namespace std;
// PROTECTED DATA //
class A // BASE CLASS
{
protected: // protected declaration
int x;
};
class B : private A // DERIVED CLASS
{
int y;
public:
B ()
{
x = 30;
y = 40;
}
void show()
{
cout <<"n x = "<<x;
cout <<"n y = "<<y;
}
};
Protected data with private inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Protected data with private inheritance
int main()
{
B b; // DECLARATION OF OBJECT
b.show();
return 0;
}
Output :
x = 30
y = 40
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Access specifies with scopes
Sr.
No. Base class
access mode
Derived class access mode
private
derivation
public
derivation
protected
derivation
A public private public protected
B private Not inherited Not inherited Not inherited
C protected private protected protected
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Access controls of functions
Sr.
No Types of functions Access mode
private public protected
A. Class member function √ √ √
B. Derived class member x √ √
C. Friend function √ √ √
D. Friend class member √ √ √
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Single Inheritance
class ABC
{
protected:
char name [20];
int age;
};
class abc : public ABC // public derivation
{
float height;
float weight;
public:
void getdata()
{
cout << "nEnter name and age: " ;
cin >>name>>age;
cout << "nEnter height and weight: " << endl;
cin >>height>>weight;
}
void show ()
{
cout << "nName :" <<name<< "nAge :" <<age;
cout << "nHeight :" <<height << "nWeight :" <<weight;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
int main()
{
abc x;
x.getdata(); // reads data through keyboard
x.show(); // displays data on the screen
return 0;
}
Single Inheritance
class ABC has two protected
data members name and age.
class abc with two data
members inherit class ABC
publically. In main function, x
is an object of derived class
invokes member function
getdata() and show().
class ABC has two protected
data members name and age.
class abc with two data
members inherit class ABC
publically. In main function, x
is an object of derived class
invokes member function
getdata() and show().
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multilevel Inheritance
#include<iostream>
using namespace std;
class top //base class
{
protected:
int a;
public :
void getdata()
{
cout<<"nnEnter first Number ::t";
cin>>a;
putdata();
}
void putdata()
{
cout<<"nFirst Number is ::t"<<a;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multilevel Inheritance
//First level inheritance
class middle :public top // class middle is derived_1
{
protected:
int b;
public:
void square()
{
getdata();
b=a*a;
cout<<"nnSquare is ::t"<<b;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multilevel Inheritance
//Second level inheritance
class bottom :public middle // class bottom is derived_2
{
int c;
public:
void cube()
{
square();
c=b*a;
cout<<"nnCube is ::t"<<c;
}
};
int main()
{
bottom b1;
b1.cube();
return 0;
}
Output :
Enter first Number :: 2
First Number is :: 2
Square is :: 4
Cube is :: 8
• When a class is derived from more than one class
then this type of inheritance is called multiple
inheritance.
• Multiple inheritance allows us to combine the
features of several existing classes as a starting
point for deriving new classes.
• Syntax:
class D : visibility-mode B, visibility-mode A
{
//members of D.
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multiple Inheritance
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multiple Inheritance
#include<iostream>
using namespace std;
class M
{
protected:
int m;
public:
void get_m(int);
};
class N
{
protected:
int n;
public:
void get_n(int);
};
class P: public M, public N
{
public:
void display();
};
void M :: get_m(int x)
{
m = x;
}
void N :: get_n(int y)
{
n = y;
}
void P :: display()
{
cout << "M = "<< m << endl;
cout << "N = "<< n << endl;
cout << "M*N = "<< m*n << endl;
}
int main()
{
P x;
x.get_m(10);
x.get_n(20);
x.display();
return 0;
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multiple Inheritance
For example: A class Rectangle is derived
from base classes Area and Perimeter.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multiple Inheritance
#include <iostream>
using namespace std;
class Area
{
public:
float area_calc(float l,float b)
{
return l*b;
}
};
class Perimeter
{
public:
float peri_calc(float l,float b)
{
return 2*(l+b);
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multiple Inheritance
/* Rectangle class is derived from classes Area and Perimeter. */
class Rectangle : private Area, private Perimeter
{
private:
float length, breadth;
public:
Rectangle() : length(0.0), breadth(0.0) { }
void get_data( )
{
cout<<"Enter length: ";
cin>>length;
cout<<"Enter breadth: ";
cin>>breadth;
}
float area_calc()
{
/* Calls area_calc() of class Area and returns it. */
return Area::area_calc(length,breadth);
}
float peri_calc()
{
/* Calls peri_calc() function of class
Perimeter and returns it. */
return Perimeter::peri_calc(length,breadth);
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multiple Inheritance
int main()
{
Rectangle r;
r.get_data();
cout<<"Area = "<<r.area_calc();
cout<<"nPerimeter = "<<r.peri_calc();
return 0;
}
Output:
Enter length: 5.1
Enter breadth: 2.3
Area = 11.73
Perimeter = 14.8
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Ambiguity Resolution in Inheritance
class M
{
public:
void display()
{
cout << "class M" << endl;
}
};
class N
{
public:
void display()
{
cout << "class N" << endl;
}
};
class P: public M, public N
{
public:
void display()
{
M :: display();
N :: display();
}
};
int main()
{
P x;
x.display();
return 0;
}
When a function
name with the same
name appears in
more than one base
class, which function
is used by the derived
class when we inherit
these two classes??
When a function
name with the same
name appears in
more than one base
class, which function
is used by the derived
class when we inherit
these two classes??
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Hierarchical Inheritance
• One class may be inherited by more than
one classes.
• Hierarchical unit shows top down style
through splitting a compound class into
several simple subclasses.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Hierarchical Inheritance
#include<iostream>
using namespace std;
class Polygon
{
protected:
int width, height;
public:
void input(int x, int y)
{
width = x;
height = y;
}
};
class Rectangle : public Polygon
{
public:
int areaR()
{
return (width * height);
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Hierarchical Inheritance
class Triangle : public Polygon
{
public:
int areaT()
{
return (width * height)/2;
}
};
int main()
{
Rectangle r;
r.input(6, 8);
cout << "Area of Rectangle ::" <<r.areaR() <<endl;
Triangle t;
t.input(6, 10);
cout << "Area of Triangle ::" <<r.areaT() <<endl;
return 0;
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Hybrid Inheritance
• Combination of one or more types of inheritance.
• In fig., GAME is derived from two base classes i.e.
LOCATION and PHYSIQUE.
• Class PHYSIQUE is also derived from class
PLAYER.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Hybrid Inheritance
#include<iostream>
using namespace std;
class Player
{
protected:
char name [15];
char gender;
int age;
};
class Physique : public Player
{
protected:
float height;
float weight;
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Hybrid Inheritance
class Location
{
protected:
char city [10];
char pin [10];
};
class Game : public Physique, public Player
{
protected:
char game [15];
public:
void getdata();
void show();
};
int main()
{
Game g;
g.getdata();
g.show();
return 0;
}
• When a class is derived from two or more classes, which
are derived from the same base class such type of
inheritance is known as multipath inheritance.
• Multipath inheritance consists multiple, multilevel and
hierarchical as shown in Figure.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Multipath Inheritance
• There is an ambiguity problem. When you run
program with such type inheritance. It gives a
compile time error [Ambiguity].
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Problem in Multipath Inheritance
• To overcome the ambiguity occurred due to
multipath inheritance, C++ provides the keyword
virtual.
• The keyword virtual declares the specified
classes virtual.
• When classes are declared as virtual, the
compiler takes necessary precaution to avoid
duplication of member variables.
• Thus, we make a class virtual if it is a base
class that has been used by more than one derived
class as their base class.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
#include<iostream>
using namespace std;
class student
{
protected:
int rno;
public:
void getnumber()
{
cout<<"Enter Roll No:";
cin>>rno;
}
void putnumber()
{
cout<<"nntRoll No:"<<rno<<"n";
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
class test:virtual public student
{
protected:
int part1,part2;
public:
void getmarks()
{
cout<<"Enter Marksn";
cout<<"Part1:";
cin>>part1;
cout<<"Part2:";
cin>>part2;
}
void putmarks()
{
cout<<"tMarks Obtainedn";
cout<<"ntPart1:"<<part1;
cout<<"ntPart2:"<<part2;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
class sports:public virtual student
{
protected:
int score;
public:
void getscore()
{
cout<<"Enter Sports Score:";
cin>>score;
}
void putscore()
{
cout<<"ntSports Score is:"<<score;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
class result:public test,public sports
{
int total;
public:
void display()
{
total=part1+part2+score;
putnumber();
putmarks();
putscore();
cout<<"ntTotal Score:"<<total;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
int main()
{
result r;
r.getnumber();
r.getmarks();
r.getscore();
r.display();
return 0;
}
Output:
Enter Roll No:111
Enter Marks
Part1:11
Part2:22
Enter Sports Score:33
Roll No:111
Marks Obtained
Part1:11
Part2:22
Sports Score is:33
Total Score:66
Problem statement:
 Define a multipath inheritance structure in which
the class master deserves information from both
account and admin classes which in turn derive
information from the class person.
 Define all four classes and write a program to
create, update, and display the information
contained in master objects.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
#include<iostream>
using namespace std;
class person
{
protected:
char name[20];
int code;
public:
void getcode()
{
cout<<"n Enter the code ";
cin>>code;
}
void getname()
{
cout<<"n Enter the name ";
cin>>name;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
class account:virtual public person
{
protected:
int pay;
public:
void getpay()
{
cout<<"n Enter the payment ";
cin>>pay;
}
};
class admin:virtual public person
{
protected:
int exp;
public:
void getexp()
{
cout<<"n Enter the experiance ";
cin>>exp;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
class master:public account,public admin
{
public:
void getdata()
{
getcode();
getname();
getpay();
getexp();
}
void update()
{
int c;
cout<<"n You want 2 updaten1.coden
2.namen3.paymentn4.experiance";
cout<<"nEnter ur choice ";
cin>>c;
switch(c)
{
case 1: getcode();
break;
case 2: getname();
break;
case 3: getpay();
break;
case 4: getexp();
break;
default:
cout<<"n Invalid choice";
}
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Virtual base classes
void putdata()
{
system("cls");
cout<<"nDetails";
cout<<"n Code "<<code<<"n Name "<<name;
cout<<"n Payment "<<pay<<"n Experiance "<<exp;
cout<<"nPress any key 2 continue ";
getchar();
}
};
int main()
{
int ch;
master m;
while(1)
{
cout<<"nMENUn1.Createn2.Updaten3.Displayn4.Exit";
cout<<"nEnter ur choice ";
cin>>ch;
switch(ch)
{
case 1: m.getdata();
break;
case 2: m.update();
break;
case 3: m.putdata();
break;
case 4: exit(0);
default:cout<<"n Invalid choice";
}
}
return 0;
}
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Abstract classes
• An abstract class is one that is not used to
create objects.
• An abstract class is designed only to act as
a base class(to be inherited by other
classes).
• Constructors play an important role in initializing
objects.
• 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 arguments to the base class constructor.
• In inheritance we make a objects of derived class.
Thus it make sense to pass arguments to the base
class constructor.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in Derived classes
• When both derived and base classes contain
constructors, the base class constructor is
executed first and then the constructor in the
derived class is executed.
• In multiple inheritance, the base classes are
constructed in the order in which they appear
in the declaration of the derived class.
• Similarly in multilevel inheritance, the
constructors will be executed in the order of
inheritance.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in derived class
• The general form of defining the Derived
constructor is :
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in derived class
Derived-constructor(arg1, arg2,..,argN) : base-class1(arg1),..,base-
classN(argN)
{
//body of constructor of derived class.
};
Derived-constructor(arg1, arg2,..,argN) : base-class1(arg1),..,base-
classN(argN)
{
//body of constructor of derived class.
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors and Destructor in inheritance
Base-class
Constructor
Base-class
Constructor
Derived-class
constructor1
Derived-class
constructor1
Derived-class
constructorN
Derived-class
constructorN
Derived-class
DestructorN
Derived-class
DestructorN
Derived-class
Destructor1
Derived-class
Destructor1
Base-class
DestructorN
Base-class
DestructorN
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in derived class
class alpha
{
private:
int x;
public:
alpha(int i)
{
x = i;
cout << "n alpha initialized n";
}
void show_x()
{
cout << "n x = "<<x;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in derived class
class beta
{
private:
float y;
public:
beta(float j)
{
y = j;
cout << "n beta initialized n";
}
void show_y()
{
cout << "n y = "<<y;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in derived class
class gamma : public beta, public alpha
{
private:
int n,m;
public:
gamma(int a, float b, int c, int d): alpha(a), beta(b)
{
m = c;
n = d;
cout << "n gamma initialized n";
}
void show_mn()
{
cout << "n m = "<<m;
cout << "n n = "<<n;
}
};
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Constructors in derived class
int main()
{
gamma g(5, 7.65, 30, 100);
cout << "n";
g.show_x();
g.show_y();
g.show_mn();
return 0;
}
Output:
beta initialized
alpha initialized
gamma initialized
x = 5
y = 7.65
m = 30
n = 100
• The most frequent use of inheritance is for
deriving classes using existing classes, which
provides reusability. The existing classes
remains unchanged. By reusability, the
development time of software is reduced.
• The derived classes extend the properties of
base classes to generate more dominant object.
• The same base class can be used by a number of
derived classes in class hierarchy.
• When a class is derived from more than one
class, all the derived classes have the same
properties as that of base classes.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Advantages of Inheritance
• The increased time/effort it takes the program to
jump through all the levels of overloaded classes.
– If a given class has ten levels of abstraction above it, then
it will essentially take ten jumps to run through a
function defined in each of those classes
• Two classes (base and inherited class) get tightly
coupled.
– This means one cannot be used independent of each
other.
• Also with time, during maintenance adding new
features both base as well as derived classes are
required to be changed.
– If a method signature is changed then we will be affected
in both cases (inheritance & composition)
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Disadvantages of Inheritance
• If a method is deleted in the "super class" or
aggregate, then we will have to re-factor in case
of using that method.
• Here things can get a bit complicated in case of
inheritance because our programs will still
compile, but the methods of the subclass will
no longer be overriding superclass methods.
These methods will become independent
methods in their own right.
Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
Disadvantages of Inheritance
To beTo be
continued…..continued…..

More Related Content

PPT
Polymorphism
PPT
Constructor,destructors cpp
PPTX
Polymorphism In c++
PPTX
Interface in java
PDF
Managing I/O in c++
PPTX
virtual function
PPTX
Chapter 07 inheritance
PPTX
07. Virtual Functions
Polymorphism
Constructor,destructors cpp
Polymorphism In c++
Interface in java
Managing I/O in c++
virtual function
Chapter 07 inheritance
07. Virtual Functions

What's hot (20)

PPTX
Inheritance In Java
PPTX
Multi level inheritence
PPTX
Inheritance in java
PPTX
Inheritance
PPTX
Inheritance in JAVA PPT
PPTX
Oop c++class(final).ppt
PPTX
Linked list
PPTX
polymorphism
PPTX
Inner class
PDF
Constructors and destructors
PPT
Friends function and_classes
PPTX
inheritance c++
PDF
Constructor and Destructor
PPTX
Templates in c++
PDF
Classes and objects
PPTX
Virtual function in C++ Pure Virtual Function
PPTX
Friend function & friend class
PPTX
Inheritance in c++
PPT
Class and object in C++
Inheritance In Java
Multi level inheritence
Inheritance in java
Inheritance
Inheritance in JAVA PPT
Oop c++class(final).ppt
Linked list
polymorphism
Inner class
Constructors and destructors
Friends function and_classes
inheritance c++
Constructor and Destructor
Templates in c++
Classes and objects
Virtual function in C++ Pure Virtual Function
Friend function & friend class
Inheritance in c++
Class and object in C++
Ad

Viewers also liked (20)

PPT
Inheritance
PPT
10 inheritance
PDF
Introduction to cpp
PPTX
Interoduction to c++
PPT
14. Linked List
PDF
Chapter27 polymorphism-virtual-function-abstract-class
PPTX
Inheritance
PPT
Inheritance
PPT
Inheritance
PPT
Input and output in C++
PPTX
Inline function in C++
PDF
Introduction to oops concepts
PPTX
Inline function in C++
PPT
friend function(c++)
PPTX
Constructors & destructors
PPT
C++ Function
PPTX
operator overloading & type conversion in cpp over view || c++
PPT
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
PPTX
Inheritance in C++
Inheritance
10 inheritance
Introduction to cpp
Interoduction to c++
14. Linked List
Chapter27 polymorphism-virtual-function-abstract-class
Inheritance
Inheritance
Inheritance
Input and output in C++
Inline function in C++
Introduction to oops concepts
Inline function in C++
friend function(c++)
Constructors & destructors
C++ Function
operator overloading & type conversion in cpp over view || c++
Inheritance in c++ ppt (Powerpoint) | inheritance in c++ ppt presentation | i...
Inheritance in C++
Ad

Similar to Inheritance : Extending Classes (20)

PPT
5. Inheritances, Packages and Intefaces
PPT
Inheritance OOP Concept in C++.
PPT
Inheritance in C++
PPT
11 Inheritance.ppt
PPT
inheritance
PPT
Inheritance
PPT
Overview of Object Oriented Programming using C++
PPTX
Inheritance
PDF
Inheritance
PPTX
Inheritance
PDF
lecture 6.pdf
PPT
week14 (1).ppt
PPTX
INHERITANCE.pptx
DOCX
oop database doc for studevsgdy fdsyn hdf
PPT
Inheritance, Object Oriented Programming
PPTX
Inheritance
PPTX
Inheritance
PPTX
Inheritance
PPTX
Inheritance in c++theory
PPTX
Aryan's pres. entation.pptx
5. Inheritances, Packages and Intefaces
Inheritance OOP Concept in C++.
Inheritance in C++
11 Inheritance.ppt
inheritance
Inheritance
Overview of Object Oriented Programming using C++
Inheritance
Inheritance
Inheritance
lecture 6.pdf
week14 (1).ppt
INHERITANCE.pptx
oop database doc for studevsgdy fdsyn hdf
Inheritance, Object Oriented Programming
Inheritance
Inheritance
Inheritance
Inheritance in c++theory
Aryan's pres. entation.pptx

More from Nilesh Dalvi (17)

PPT
13. Queue
PPT
12. Stack
PPT
11. Arrays
PPT
10. Introduction to Datastructure
PPT
9. Input Output in java
PPT
8. String
PPT
7. Multithreading
PPT
6. Exception Handling
PPT
4. Classes and Methods
PPT
3. Data types and Variables
PPT
2. Basics of Java
PPT
1. Overview of Java
PPT
Standard Template Library
PPT
Templates
PPT
File handling
PPT
Strings
PPT
Operator Overloading
13. Queue
12. Stack
11. Arrays
10. Introduction to Datastructure
9. Input Output in java
8. String
7. Multithreading
6. Exception Handling
4. Classes and Methods
3. Data types and Variables
2. Basics of Java
1. Overview of Java
Standard Template Library
Templates
File handling
Strings
Operator Overloading

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Cell Structure & Organelles in detailed.
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
master seminar digital applications in india
PDF
Basic Mud Logging Guide for educational purpose
PDF
01-Introduction-to-Information-Management.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Business Ethics Teaching Materials for college
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
RMMM.pdf make it easy to upload and study
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Cell Structure & Organelles in detailed.
human mycosis Human fungal infections are called human mycosis..pptx
Pharma ospi slides which help in ospi learning
Final Presentation General Medicine 03-08-2024.pptx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Microbial diseases, their pathogenesis and prophylaxis
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPH.pptx obstetrics and gynecology in nursing
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
STATICS OF THE RIGID BODIES Hibbelers.pdf
master seminar digital applications in india
Basic Mud Logging Guide for educational purpose
01-Introduction-to-Information-Management.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
Business Ethics Teaching Materials for college
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O5-L3 Freight Transport Ops (International) V1.pdf

Inheritance : Extending Classes

  • 1. Inheritance : Extending classesInheritance : Extending classes By Nilesh Dalvi Lecturer, Patkar-Varde College.Lecturer, Patkar-Varde College. http://www.slideshare.net/nileshdalvi01 Object oriented ProgrammingObject oriented Programming with C++with C++
  • 2. Introduction • Inheritance is one of the most useful and essential characteristics of oops. • Existing classes are main components of inheritance. • New classes are created from existing one. • Properties of existing classes are simply extended to the new classes. • New classes are called as derived classes and existing one are base classes. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 3. Introduction Fig. Inheritance Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 4. Types of Inheritance Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). 1. Single Inheritance 2. Multiple Inheritance 3. Hierarchical Inheritance 4. Multilevel Inheritance 5. Hybrid Inheritance
  • 5. Types of Inheritance Single inheritance: A derived class with only one base class is called as single inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 6. Types of Inheritance Multiple inheritance: A derived class with several base classes is called as Multiple inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 7. Types of Inheritance Multilevel inheritance: The mechanism of deriving a class from another ‘derived class’ is known as multilevel inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 8. Types of Inheritance Hierarchical inheritance: One class may be inherited by more than one class. This process is known as hierarchical inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 9. Types of Inheritance Hybrid inheritance: It is combination of Hierarchical and Multilevel inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 10. Defining derived class • A derived class can be defined by specifying its relationship with the base class in addition to its own details. class derived-class-name : visibility-mode base-class-name { //members of derived class. }; • The colon (:) indicates that derived-class-name is derived from the base-class-name. • The visibility-mode is optional and if present may be either private or public. The default visibility-mode is private Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W).
  • 11. • Visibility-mode specifies whether the features of the base class are privately derived or publicly derived. • For Example: Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Defining derived class class base { private: public: //members of base class } class derived : private base { //members of base class } class derived : public base { //members of base class } class derived : base { //members of base class }
  • 12. • public members of the base class become private members of the derived class. • Therefore, the public members of the base class can only be accessed by the member functions of the derived class. • They are not accessible to the object of the derived class. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Privately Inherited
  • 13. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Privately Inherited #include <iostream> using namespace std; //Public Derivation class A //Base class { public: int x; }; class B : private A // Derived class { public: int y; B() { x = 10; y = 20; } void show () { cout << "X : " << x <<endl; cout << "Y : " << y <<endl; } }; int main() { B b; b.show (); return 0; } The object b of derived class cannot directly access the variable x. b.x = 10; // cannot access class B is privately inherited. Hence, its access is restricted. The object b of derived class cannot directly access the variable x. b.x = 10; // cannot access class B is privately inherited. Hence, its access is restricted.
  • 14. • public members of the base class remains public member in derived class. • Therefore, they are accessible to the objects of the derived class. • In both cases, the private members are not inherited and therefore, the private members of base class will never become the members of its derived class. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Publicly Inherited
  • 15. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Publicly Inherited #include <iostream> using namespace std; //Public Derivation class A //Base class { public: int x; }; class B : public A // Derived class { public: int y; }; int main() { B b; b.x = 10; b.y = 20; cout << "Member of A :" << b.x <<endl; cout << "Member of B :" << b.y <<endl; return 0; } In main() function, b is an object of class B. The object b can access the members of class A as well as of B through the statements: b.x = 10; b.y = 20; In main() function, b is an object of class B. The object b can access the members of class A as well as of B through the statements: b.x = 10; b.y = 20;
  • 16. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Privately Inherited #include<iostream> using namespace std; class geometry { int length; int breadth; public: void get() { cout << "Enter values: "<<endl; cin >> length >> breadth; } int area(); }; int geometry :: area () { return length * breadth; }
  • 17. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Privately Inherited class Rect : private geometry { int height; public: void getHeight (int h) { get(); height = h; } void volume(); }; void Rect ::volume() { cout << "Area is: " << area ()<< endl; cout <<"Volume is : "<< area() * height; }
  • 18. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Privately Inherited int main() { Rect r; r.getHeight (2); //r.area(); r.volume(); return 0; } Output: Enter values: 2 3 Area is: 6 Volume is: 12
  • 19. • The member functions of derived class cannot access the private member variables of base class. • The private members of base class can be accessed using public member functions of the same class. • This approach makes a program lengthy. • To overcome the problem associated with private data, the creator of C++ introduced another access specifier called protected. • protected is same as private , but it allows the derived class to access the private members directly. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Protected data with private inheritance
  • 20. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Protected data with private inheritance class ABC { private: // optional .... //visible to member functions within class protected: //visible to member functions of its own .... // and of its derived class public: //visible to all functions in the program .... // };
  • 21. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). #include <iostream> using namespace std; // PROTECTED DATA // class A // BASE CLASS { protected: // protected declaration int x; }; class B : private A // DERIVED CLASS { int y; public: B () { x = 30; y = 40; } void show() { cout <<"n x = "<<x; cout <<"n y = "<<y; } }; Protected data with private inheritance
  • 22. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Protected data with private inheritance int main() { B b; // DECLARATION OF OBJECT b.show(); return 0; } Output : x = 30 y = 40
  • 23. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Access specifies with scopes Sr. No. Base class access mode Derived class access mode private derivation public derivation protected derivation A public private public protected B private Not inherited Not inherited Not inherited C protected private protected protected
  • 24. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Access controls of functions Sr. No Types of functions Access mode private public protected A. Class member function √ √ √ B. Derived class member x √ √ C. Friend function √ √ √ D. Friend class member √ √ √
  • 25. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Single Inheritance class ABC { protected: char name [20]; int age; }; class abc : public ABC // public derivation { float height; float weight; public: void getdata() { cout << "nEnter name and age: " ; cin >>name>>age; cout << "nEnter height and weight: " << endl; cin >>height>>weight; } void show () { cout << "nName :" <<name<< "nAge :" <<age; cout << "nHeight :" <<height << "nWeight :" <<weight; } };
  • 26. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). int main() { abc x; x.getdata(); // reads data through keyboard x.show(); // displays data on the screen return 0; } Single Inheritance class ABC has two protected data members name and age. class abc with two data members inherit class ABC publically. In main function, x is an object of derived class invokes member function getdata() and show(). class ABC has two protected data members name and age. class abc with two data members inherit class ABC publically. In main function, x is an object of derived class invokes member function getdata() and show().
  • 27. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multilevel Inheritance #include<iostream> using namespace std; class top //base class { protected: int a; public : void getdata() { cout<<"nnEnter first Number ::t"; cin>>a; putdata(); } void putdata() { cout<<"nFirst Number is ::t"<<a; } };
  • 28. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multilevel Inheritance //First level inheritance class middle :public top // class middle is derived_1 { protected: int b; public: void square() { getdata(); b=a*a; cout<<"nnSquare is ::t"<<b; } };
  • 29. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multilevel Inheritance //Second level inheritance class bottom :public middle // class bottom is derived_2 { int c; public: void cube() { square(); c=b*a; cout<<"nnCube is ::t"<<c; } }; int main() { bottom b1; b1.cube(); return 0; } Output : Enter first Number :: 2 First Number is :: 2 Square is :: 4 Cube is :: 8
  • 30. • When a class is derived from more than one class then this type of inheritance is called multiple inheritance. • Multiple inheritance allows us to combine the features of several existing classes as a starting point for deriving new classes. • Syntax: class D : visibility-mode B, visibility-mode A { //members of D. }; Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multiple Inheritance
  • 31. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multiple Inheritance #include<iostream> using namespace std; class M { protected: int m; public: void get_m(int); }; class N { protected: int n; public: void get_n(int); }; class P: public M, public N { public: void display(); }; void M :: get_m(int x) { m = x; } void N :: get_n(int y) { n = y; } void P :: display() { cout << "M = "<< m << endl; cout << "N = "<< n << endl; cout << "M*N = "<< m*n << endl; } int main() { P x; x.get_m(10); x.get_n(20); x.display(); return 0; }
  • 32. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multiple Inheritance For example: A class Rectangle is derived from base classes Area and Perimeter.
  • 33. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multiple Inheritance #include <iostream> using namespace std; class Area { public: float area_calc(float l,float b) { return l*b; } }; class Perimeter { public: float peri_calc(float l,float b) { return 2*(l+b); } };
  • 34. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multiple Inheritance /* Rectangle class is derived from classes Area and Perimeter. */ class Rectangle : private Area, private Perimeter { private: float length, breadth; public: Rectangle() : length(0.0), breadth(0.0) { } void get_data( ) { cout<<"Enter length: "; cin>>length; cout<<"Enter breadth: "; cin>>breadth; } float area_calc() { /* Calls area_calc() of class Area and returns it. */ return Area::area_calc(length,breadth); } float peri_calc() { /* Calls peri_calc() function of class Perimeter and returns it. */ return Perimeter::peri_calc(length,breadth); } };
  • 35. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multiple Inheritance int main() { Rectangle r; r.get_data(); cout<<"Area = "<<r.area_calc(); cout<<"nPerimeter = "<<r.peri_calc(); return 0; } Output: Enter length: 5.1 Enter breadth: 2.3 Area = 11.73 Perimeter = 14.8
  • 36. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Ambiguity Resolution in Inheritance class M { public: void display() { cout << "class M" << endl; } }; class N { public: void display() { cout << "class N" << endl; } }; class P: public M, public N { public: void display() { M :: display(); N :: display(); } }; int main() { P x; x.display(); return 0; } When a function name with the same name appears in more than one base class, which function is used by the derived class when we inherit these two classes?? When a function name with the same name appears in more than one base class, which function is used by the derived class when we inherit these two classes??
  • 37. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Hierarchical Inheritance • One class may be inherited by more than one classes. • Hierarchical unit shows top down style through splitting a compound class into several simple subclasses.
  • 38. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Hierarchical Inheritance #include<iostream> using namespace std; class Polygon { protected: int width, height; public: void input(int x, int y) { width = x; height = y; } }; class Rectangle : public Polygon { public: int areaR() { return (width * height); } };
  • 39. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Hierarchical Inheritance class Triangle : public Polygon { public: int areaT() { return (width * height)/2; } }; int main() { Rectangle r; r.input(6, 8); cout << "Area of Rectangle ::" <<r.areaR() <<endl; Triangle t; t.input(6, 10); cout << "Area of Triangle ::" <<r.areaT() <<endl; return 0; }
  • 40. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Hybrid Inheritance • Combination of one or more types of inheritance. • In fig., GAME is derived from two base classes i.e. LOCATION and PHYSIQUE. • Class PHYSIQUE is also derived from class PLAYER.
  • 41. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Hybrid Inheritance #include<iostream> using namespace std; class Player { protected: char name [15]; char gender; int age; }; class Physique : public Player { protected: float height; float weight; };
  • 42. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Hybrid Inheritance class Location { protected: char city [10]; char pin [10]; }; class Game : public Physique, public Player { protected: char game [15]; public: void getdata(); void show(); }; int main() { Game g; g.getdata(); g.show(); return 0; }
  • 43. • When a class is derived from two or more classes, which are derived from the same base class such type of inheritance is known as multipath inheritance. • Multipath inheritance consists multiple, multilevel and hierarchical as shown in Figure. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Multipath Inheritance
  • 44. • There is an ambiguity problem. When you run program with such type inheritance. It gives a compile time error [Ambiguity]. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Problem in Multipath Inheritance
  • 45. • To overcome the ambiguity occurred due to multipath inheritance, C++ provides the keyword virtual. • The keyword virtual declares the specified classes virtual. • When classes are declared as virtual, the compiler takes necessary precaution to avoid duplication of member variables. • Thus, we make a class virtual if it is a base class that has been used by more than one derived class as their base class. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes
  • 46. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes #include<iostream> using namespace std; class student { protected: int rno; public: void getnumber() { cout<<"Enter Roll No:"; cin>>rno; } void putnumber() { cout<<"nntRoll No:"<<rno<<"n"; } };
  • 47. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes class test:virtual public student { protected: int part1,part2; public: void getmarks() { cout<<"Enter Marksn"; cout<<"Part1:"; cin>>part1; cout<<"Part2:"; cin>>part2; } void putmarks() { cout<<"tMarks Obtainedn"; cout<<"ntPart1:"<<part1; cout<<"ntPart2:"<<part2; } };
  • 48. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes class sports:public virtual student { protected: int score; public: void getscore() { cout<<"Enter Sports Score:"; cin>>score; } void putscore() { cout<<"ntSports Score is:"<<score; } };
  • 49. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes class result:public test,public sports { int total; public: void display() { total=part1+part2+score; putnumber(); putmarks(); putscore(); cout<<"ntTotal Score:"<<total; } };
  • 50. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes int main() { result r; r.getnumber(); r.getmarks(); r.getscore(); r.display(); return 0; } Output: Enter Roll No:111 Enter Marks Part1:11 Part2:22 Enter Sports Score:33 Roll No:111 Marks Obtained Part1:11 Part2:22 Sports Score is:33 Total Score:66
  • 51. Problem statement:  Define a multipath inheritance structure in which the class master deserves information from both account and admin classes which in turn derive information from the class person.  Define all four classes and write a program to create, update, and display the information contained in master objects. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes
  • 52. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes #include<iostream> using namespace std; class person { protected: char name[20]; int code; public: void getcode() { cout<<"n Enter the code "; cin>>code; } void getname() { cout<<"n Enter the name "; cin>>name; } };
  • 53. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes class account:virtual public person { protected: int pay; public: void getpay() { cout<<"n Enter the payment "; cin>>pay; } }; class admin:virtual public person { protected: int exp; public: void getexp() { cout<<"n Enter the experiance "; cin>>exp; } };
  • 54. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes class master:public account,public admin { public: void getdata() { getcode(); getname(); getpay(); getexp(); } void update() { int c; cout<<"n You want 2 updaten1.coden 2.namen3.paymentn4.experiance"; cout<<"nEnter ur choice "; cin>>c; switch(c) { case 1: getcode(); break; case 2: getname(); break; case 3: getpay(); break; case 4: getexp(); break; default: cout<<"n Invalid choice"; } }
  • 55. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Virtual base classes void putdata() { system("cls"); cout<<"nDetails"; cout<<"n Code "<<code<<"n Name "<<name; cout<<"n Payment "<<pay<<"n Experiance "<<exp; cout<<"nPress any key 2 continue "; getchar(); } }; int main() { int ch; master m; while(1) { cout<<"nMENUn1.Createn2.Updaten3.Displayn4.Exit"; cout<<"nEnter ur choice "; cin>>ch; switch(ch) { case 1: m.getdata(); break; case 2: m.update(); break; case 3: m.putdata(); break; case 4: exit(0); default:cout<<"n Invalid choice"; } } return 0; }
  • 56. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Abstract classes • An abstract class is one that is not used to create objects. • An abstract class is designed only to act as a base class(to be inherited by other classes).
  • 57. • Constructors play an important role in initializing objects. • 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 arguments to the base class constructor. • In inheritance we make a objects of derived class. Thus it make sense to pass arguments to the base class constructor. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in Derived classes
  • 58. • When both derived and base classes contain constructors, the base class constructor is executed first and then the constructor in the derived class is executed. • In multiple inheritance, the base classes are constructed in the order in which they appear in the declaration of the derived class. • Similarly in multilevel inheritance, the constructors will be executed in the order of inheritance. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in derived class
  • 59. • The general form of defining the Derived constructor is : Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in derived class Derived-constructor(arg1, arg2,..,argN) : base-class1(arg1),..,base- classN(argN) { //body of constructor of derived class. }; Derived-constructor(arg1, arg2,..,argN) : base-class1(arg1),..,base- classN(argN) { //body of constructor of derived class. };
  • 60. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors and Destructor in inheritance Base-class Constructor Base-class Constructor Derived-class constructor1 Derived-class constructor1 Derived-class constructorN Derived-class constructorN Derived-class DestructorN Derived-class DestructorN Derived-class Destructor1 Derived-class Destructor1 Base-class DestructorN Base-class DestructorN
  • 61. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in derived class class alpha { private: int x; public: alpha(int i) { x = i; cout << "n alpha initialized n"; } void show_x() { cout << "n x = "<<x; } };
  • 62. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in derived class class beta { private: float y; public: beta(float j) { y = j; cout << "n beta initialized n"; } void show_y() { cout << "n y = "<<y; } };
  • 63. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in derived class class gamma : public beta, public alpha { private: int n,m; public: gamma(int a, float b, int c, int d): alpha(a), beta(b) { m = c; n = d; cout << "n gamma initialized n"; } void show_mn() { cout << "n m = "<<m; cout << "n n = "<<n; } };
  • 64. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Constructors in derived class int main() { gamma g(5, 7.65, 30, 100); cout << "n"; g.show_x(); g.show_y(); g.show_mn(); return 0; } Output: beta initialized alpha initialized gamma initialized x = 5 y = 7.65 m = 30 n = 100
  • 65. • The most frequent use of inheritance is for deriving classes using existing classes, which provides reusability. The existing classes remains unchanged. By reusability, the development time of software is reduced. • The derived classes extend the properties of base classes to generate more dominant object. • The same base class can be used by a number of derived classes in class hierarchy. • When a class is derived from more than one class, all the derived classes have the same properties as that of base classes. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Advantages of Inheritance
  • 66. • The increased time/effort it takes the program to jump through all the levels of overloaded classes. – If a given class has ten levels of abstraction above it, then it will essentially take ten jumps to run through a function defined in each of those classes • Two classes (base and inherited class) get tightly coupled. – This means one cannot be used independent of each other. • Also with time, during maintenance adding new features both base as well as derived classes are required to be changed. – If a method signature is changed then we will be affected in both cases (inheritance & composition) Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Disadvantages of Inheritance
  • 67. • If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that method. • Here things can get a bit complicated in case of inheritance because our programs will still compile, but the methods of the subclass will no longer be overriding superclass methods. These methods will become independent methods in their own right. Nilesh Dalvi, Lecturer@Patkar-Varde College, Goregaon(W). Disadvantages of Inheritance