SlideShare a Scribd company logo
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
OOPS Using C++ 1
OOPS Using C++ 1
1
Object Oriented Programming
Using C++
Subject Teacher:-
Dr. Shiraz Khurana
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
2
What is Inheritance ?
■ Reusability.
■ Is the process by which new class (or
classes are created from existing class (or
classes).
■ Derived classes have all the features of
the base class in addition to some new
features.
■ The new inheriting class is called derived
or sub class and inherited class is called
base or super class.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
3
How to inherit a class?
1. Define a base class of which all properties are to
be inherited.
2. Define a derived class as
class derived_cls_name : access-specifier
base_cls_name
{ BODY OF THE DERIVED CLASS } ;
3. Access specifier can be either public , protected
or private (if nothing is specified then default
access-specifier is private)
Access specifier also called visibility mode which
specifies whether the features of the base class
are privately or publicly derived.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
4
■ If access specifier is public
1. All public members of the base class
become public members of derived class.
2. All protected members of base class
become protected members of derived
class.
3. Private members of base class are still
private to base class ,thus not accessible to
derived class.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
5
Inheritance using public access specifier
#include"iostream.h"
#include<string.h>
class student
{ int rollno; char name[12];
public:
void get_details()
{ cout<<"enter roll no and name";
cin>>rollno>>name; }
void display()
{ cout<<"roll no is "<<rollno<<"and name is
"<<name;} }; // Base class ends here
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
6
class cse_student :public student
{
char sub1[10],sub2[10];
public:
cse_student( ) // CONSTRUCTOR
{ strcpy(sub1,"OOPS");
strcpy(sub2,"PROGRAMMING");}
void sub_display()
{ cout<<"and subjects
are"<<sub1<<"and"<<sub2;
} };
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
7
int main (void)
{
cse_student c1;
c1.get_details();
c1.display();
c1.sub_display();
return 0;
}
Public members of base class
are accessed as public of
derived
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
8
Output
■ enter roll no and name 12 ABC
■ roll no is 12
■ and name is ABC
■ and subjects are OOPS and
PROGRAMMING
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
9
■ If access specifier is private then
1. All public and protected members of base
class become private members of derived
class.
2. Private members of base class remain as
private to it and hence not accessible to
derived class.
3. All public and protected members of a class
can be accessed by its own objects using
the dot operator.
4. No member of base class is accessible to
the objects of the derived class.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
10
Inheritance using private access specifier
#include"iostream.h"
#include<string.h>
class student
{
int rollno; char name[12];
public:
void get_details()
{ cout<<"enter roll no and name";
cin>>rollno>>name;}
void display()
{ cout<<"roll no is "<<rollno<<"and name is"<<name;}
};
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
11
class cse_student :private student
{
char sub1[10],sub2[10];
public:
cse_student() // CONSTRUCTOR
{ strcpy(sub1,"OOPS");
strcpy(sub2,"PROGRAMMING");
get_details();
}
void sub_display()
{ display();
// cout<<"rollno is "<<rollno; // Rollno not accessible
cout<<"and subjects are"<<sub1<<"and"<<sub2;}
};
Now private member of
cse_student
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
12
int main (void)
{
cse_student c1;
//c1.get_details(); private member of
//c1.display(); base class in-accessible
c1.sub_display();
return 0;
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
13
Output
enter roll no and name
12
neeraj
roll no is 12 and name is neeraj
and subjects are OOPS and PROGRAMMING
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
14
“Protected” Access Specifier
■ A protected member of a class behaves
similar to private members ( that is
accessible only to members of the class)
BUT
■ If the class is inherited by some class then
protected members of base class may be
accessible to derived class (different from
PRIVATE MEMBERS OF BASE CLASS)
■ Thus protected members of a class are despite
being private to it can be inherited by some other
class.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
15
#include"iostream.h"
class student
{
protected:
int rollno; char name[12];
void get_details()
{ cout<<"enter roll no and name";
cin>>rollno>>name;}
public:
student()
{ get_details(); }
void display()
{ cout<<endl<<"roll no is "<<rollno<<"and name is
"<<name<<endl; } };
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
16
class it_student :public student
{
char sub1[10],sub2[10];
public:
it_student()
{
strcpy(sub1,"OOPS");
strcpy(sub2,"PROGRAMMING");
get_details(); // valid as get_details is PROTECTED and
not PRIVATE
}
void display1()
{
cout<<"rollno and name are"<<rollno<<"t"<<name;
cout<<endl<<"and subjects are"<<sub1<<"and"<<sub2;
}};
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
17
int main (void)
{
student s;
//s.get_details(); // get_details is private to student
s.display(); // valid being PUBLIC to student
it_student c1;
//c1.get_details(); //get_details is private to it_student
c1.display(); //valid ,display() is public to
//it_student
c1.display1();
return 0;
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
18
Output
■ enter roll no and name123 angel
■ roll no is 123and name is angel
■ enter roll no and name45 angel2
■ rollno and name are45 angel2
and subjects are OOPS and
PROGRAMMING
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
19
Inheriting a class in protected mode
■ Both public and protected members of base
class become protected members of derived
class.
■ Private members of base class remain
private to it and hence in-accessible to
derived class.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
20
Visibility of inherited members
Base class
visibility
Derived class visibilty
Public
derivation
Private
derivation
Protected
Derivation
Private Not
inherited
Not
Inherited
Not
Inherited
Protected Protected Private Protected
Public Public Private Protected
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
Forms Of Inheritance
(a) Single Inheritance
(b) Multiple Inheritance
(c) Hierarchical Inheritance
(d) Multilevel Inheritance
(e) Hybrid Inheritance
OOPS Using C++ 21
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
22
More on inheritance….
Derived class may inherit traits from more
than one class ( multiple inheritance)
Class Z: access_specifier X, access_specifier
Y...
Y
X
Z
Multiple Inheritance
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
23
Example of multiple inheritance
class X
{
protected:
int a;
public:
void getX (int m)
{
a=m;
}
void display()
{
cout<<"The number in X is
"<<a<<endl;
}
};
class Y
{
protected:
int b;
public:
void getY (int n)
{
b=n;
}
void display1()
{
cout<<"The number in Y is
"<<b<<endl;
}
};
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
24
class Z: public X, public Y
{
protected:
int c;
public:
Z(int k)
{
getX(1);
getY(2);
c=k;
}
void disp()
{
display();
display1();
cout<<"the number in Z is <<c;
}
};
int main (void)
{
Z obj(3);
obj.disp();
return 0;
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
25
Output
■ The number in X is 1
■ The number in Y is 2
■ the number in Z is 3
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
26
Ambiguity
class M
{ public: void display(){cout<<“ class M”;}
};
class N
{ public: void display(){cout<<“ class N”;}
};
class P : public M , public N
{ public: void display(){cout<<“ class P”;}
};
In this case function of derived class overrides the inherited
function so when object of P calls display function it simply
calls display of P. To invoke display of M & N we use scope
resolution operator.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
27
int main ()
{
P p;
p.display();
p.M::display();
p.N::display();
p.P::display();
return 0;
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
28
■ Inheriting one class by more than one class
leads to Hierarchical inheritance.
Z
X
W
Y
Hierarchical Inheritance
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
29
Example
class college
{
protected:
char name[12];
void disp()
{
cout<<"name of college
is"<<name<<endl;}
public:
college()
{cout<<"constructor of college
is called"<<endl;
strcpy (name, "PIET");
}};
class it_dept : public college
{
int faculty;
public:
void get_faculty()
{
cout<<"enter no of faculty
members in IT" <<endl ;
cin>>faculty; }
void display()
{ disp(); // now a protected
member of it_dept
cout<<"no of teachers in IT
"<<faculty<<endl;
}
};
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
30
class ece_dept : public
college
{
int faculty;
public:
void get_faculty()
{
cout<<"enter no of faculty
members in ECE"<<endl;
cin>>faculty;}
void display()
{ disp(); // now a protected
member of ece_dept
cout<<"no of teachers in
ECE "<<faculty<<endl;}};
void main()
{ it_dept C;
ece_dept E;
C.get_faculty();
E.get_faculty();
C.display();
E.display();
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
31
OUTPUT
constructor of college is called
constructor of college is called
enter no of faculty members in IT
12
enter no of faculty members in ECE
36
name of college is PIET
no of teachers in IT 12
name of college is PIET
no of teachers in ECE 36
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
32
■ Deriving one class from other derived class
leads to Multilevel inheritance.
Y
Z
Multilevel Inheritance
X
Base class
Intermediate
base class
Derived class
test
result
Student
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
33
Example
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number=a;}
void put_number()
{cout<<“Roll number
is:"<<roll_number<<“n”;
}};
class test : public student
{
protected:
float sub1,sub2;
public:
void get_marks(float x, float y)
{
sub1=x;
sub2=y;}
void put_marks()
{ cout<<“Marks in sub1=
"<<sub1<<endl;
cout<<“Marks in sub2=
"<<sub2<<endl;
}
};
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
34
class result : public test
{
float total;
public:
void display()
{
total=sub1+sub2;
put_number();
put_marks();
cout<<“Total ="<< total
<<endl;
}};
int main()
{
result Abc;
Abc.get_number(111);
Abc.get_marks(75.0,59.5);
Abc.display();
return 0;
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
35
■ When more than one inheritance is used, it
is called hybrid inheritance.
Z
W
Hybrid Inheritance
Sub class of X and Y
Base class of X,Y
Sub class
of W
Sub
class of
W
Y
X
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
36
Virtual base class
■ In case of hybrid inheritance shown, two
copies of W-members are created in class Z.
Int I
Int I( of W)
Int J
Int I (of W)
Int K
Int I (of X)
Int I (of Y)
Int J (of X)
Int K (of Y)
X
Z
Y
W
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
37
class W
{
public:
int i;
};
class X : public W
{ public:
int j;
};
class Y: public W
{
public:
int k;
};
class Z: public X, public Y
{
public:
int sum;
};
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
38
int main (void)
{
Z obj;
obj.X::i=1;
obj.j=2;
obj.k=3;
obj.sum=obj. X::i+ obj.j+obj.k;
// obj.i will be ambiguous
cout<<"the sum is "<<obj.sum;
return 0;
}
OUTPUT
The sum is 6
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
39
■ If X and Y derives class W as a virtual class
then only one copy of W-members will be
inherited by X and Y.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
40
class W
{
public:
int i;
};
class X : virtual public W
{ public:
int j;
};
class Y: public virtual W
{
public:
int k;
};
class Z: public X, public Y
{
public:
int sum;
};
int main(void)
{
Z obj;
//obj.X::i=1;
obj.i=1;
obj.j=2;
obj.k=3;
obj.sum= obj.i + obj.j + obj.k;
cout<<"the sum is "<<obj.sum;
return 0;
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
41
Abstract Classes
■ An abstract class is one that is not used to
create objects.
■ An abstract class is only designed to act as a
base class(to be inherited by other classes).
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
42
Constructors in inheritance
■ Constructors play an important role in
initializing objects.
■ In case of inheritance the base class
constructors is first called of and then the
derived class constructor is called.
■ When base class contains only default
constructor then derived class may or may
not have the constructor.
■ When 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.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
43
More about constructors….
■ When an object of derived class is created
In case of multi level inheritance constructors are
called in the order of their derivation.
class derived1 : public base
{ };
class derived2: public derived1 { };
That is base class constructor is called first, then
constructor for derived1 class is called, then
constructor for derived2 class is called.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
44
■ In case of multiple inheritance, constructors
are called in the order specified from left to
right .
■ class derived: public base1,public base2
then
derived d;
will create an object of derived class by calling
constructor for base classes base1 and then
base2
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
45
Destructors in inheritance
■ When an object of derived class is
destroyed
1. In case of multi level inheritance
,destructors are called in the reverse order
of their derivation
class derived : public base1,public base2{ };
Then destructor for derived is called then
destructor for base2 is called then lastly
destructor defined in base1 will be called.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
46
Passing arguments to base class
■ When an object of derived class is created,
constructors of base class is also invoked.
■ If initialization of only derived class variables
are to be done then arguments to the
constructor can be passed in usual way.
■ But if base class constructor (invoked
implicitly) also require some arguments then
…….?
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
47
■ Constructor of derived class should be
defined as
Derived constructor (arg_list) :
base1(arg_list1),base2(arg_list)……
{ body of the constructor }
■ Arg_list for base classes will contain those
arguments that are required by them,but are
passed while calling derived class
constructor.
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
48
#include<iostream.h>
#include<conio.h>
#include<string.h>
class college
{
protected:
int id ;
char name[12];
void disp()
{
cout<<"name of college is
"<<name<<endl;
}
public:
college(int a)
{
id=a;
cout<<"constructor of college is
called"<<endl;
strcpy (name,"PIET");
}
};
class it_dept : public college
{
int faculty;
public:
it_dept (int a, int b) : college (a)
{ faculty=b; }
void display()
{ disp(); // now a protected member of
it_dept
cout<<“initialized college id is “<<id;
cout<<"no of teachers in IT “
<<faculty<<endl;
} };
int main()
{ clrscr();
it_dept C(10,12);
C.display();
return 0;
}
Argumen
t for base
construct
or
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
49
OUTPUT
■ constructor of base college is called
■ initialized college id is 10
■ name of college is PIET
■ no of teachers in it 12
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
50
■ The constructors for virtual base classes are
invoked before any non virtual base classes.
METHOD OF INHERITANCE ORDER OF EXECUTION
class B : public A
{
};
class A : public B, public C
{
};
class A : public B, virtual public C
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
51
Initializing the class objects
constructor (arglist) : initialization-section
{
assignment-section
}
Example: class ABC{ int a;
public : ABC(int x)
{ a=x;
cout<< “” ABC Constructor & values”<< a;}
};
class XYZ : public ABC
{ int b,c;
public: XYZ(int i, int j): a(i),b(2*j) { c=j;
cout<< “XYZ Constructor & values”<<b<<c;}
};
main()
{ XYZ x(2,3)
}
OOPS Using C++
PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY
52
Member Classes : Nesting Of classes
class alpha { };
class beta { };
class gamma
{ alpha a;
beta b;
………
};
All objects of gamma class will contain the
objects a,b; This kind of relationship is called
containership or nesting.
OOPS Using C++

More Related Content

PPTX
Inheritance and Interfaces
PPTX
Inheritance
PPT
inhertance c++
PPT
Constructors.16
PPTX
Virtual base class
PDF
Design of OO language
PPTX
Technical aptitude Test 1 CSE
PPTX
Technical aptitude test 2 CSE
Inheritance and Interfaces
Inheritance
inhertance c++
Constructors.16
Virtual base class
Design of OO language
Technical aptitude Test 1 CSE
Technical aptitude test 2 CSE

What's hot (8)

PDF
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
PDF
C++ Multiple Inheritance
PPTX
C# for C++ programmers
PPT
C++ polymorphism
PPTX
Hybrid inheritance
PPT
Inheritance, polymorphisam, abstract classes and composition)
PDF
E5
PPTX
OOP with Java - continued
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
C++ Multiple Inheritance
C# for C++ programmers
C++ polymorphism
Hybrid inheritance
Inheritance, polymorphisam, abstract classes and composition)
E5
OOP with Java - continued
Ad

Similar to Inheritance (20)

PPT
Inheritance : Extending Classes
PPT
Inheritance in C++
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
PPTX
00ps inheritace using c++
PPTX
Inheritance.pptx
PDF
Chapter25 inheritance-i
PPT
inheritance
PPTX
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
PPTX
Inheritance_with_its_types_single_multi_hybrid
PPTX
Inheritance in c++ part1
PPTX
OOPS Basics With Example
PPTX
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PPTX
Access controlaspecifier and visibilty modes
PPTX
Lecture 5.mte 407
PPTX
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
PDF
C++ prgms 4th unit Inheritance
PPT
MODULE2_INHERITANCE_SESSION1.ppt computer
PPT
11 Inheritance.ppt
PPT
Inheritance in C++
PPTX
labwork practice on inhetitance-1.pptx
Inheritance : Extending Classes
Inheritance in C++
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
00ps inheritace using c++
Inheritance.pptx
Chapter25 inheritance-i
inheritance
Lecture 6, c++(complete reference,herbet sheidt)chapter-16
Inheritance_with_its_types_single_multi_hybrid
Inheritance in c++ part1
OOPS Basics With Example
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
Access controlaspecifier and visibilty modes
Lecture 5.mte 407
inheriTANCE IN OBJECT ORIENTED PROGRAM.pptx
C++ prgms 4th unit Inheritance
MODULE2_INHERITANCE_SESSION1.ppt computer
11 Inheritance.ppt
Inheritance in C++
labwork practice on inhetitance-1.pptx
Ad

Recently uploaded (20)

PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Sustainable Sites - Green Building Construction
PPTX
Lecture Notes Electrical Wiring System Components
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Welding lecture in detail for understanding
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
additive manufacturing of ss316l using mig welding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Sustainable Sites - Green Building Construction
Lecture Notes Electrical Wiring System Components
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Model Code of Practice - Construction Work - 21102022 .pdf
Operating System & Kernel Study Guide-1 - converted.pdf
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
CYBER-CRIMES AND SECURITY A guide to understanding
Welding lecture in detail for understanding
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
additive manufacturing of ss316l using mig welding
Embodied AI: Ushering in the Next Era of Intelligent Systems
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf

Inheritance

  • 1. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY OOPS Using C++ 1 OOPS Using C++ 1 1 Object Oriented Programming Using C++ Subject Teacher:- Dr. Shiraz Khurana
  • 2. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 2 What is Inheritance ? ■ Reusability. ■ Is the process by which new class (or classes are created from existing class (or classes). ■ Derived classes have all the features of the base class in addition to some new features. ■ The new inheriting class is called derived or sub class and inherited class is called base or super class. OOPS Using C++
  • 3. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 3 How to inherit a class? 1. Define a base class of which all properties are to be inherited. 2. Define a derived class as class derived_cls_name : access-specifier base_cls_name { BODY OF THE DERIVED CLASS } ; 3. Access specifier can be either public , protected or private (if nothing is specified then default access-specifier is private) Access specifier also called visibility mode which specifies whether the features of the base class are privately or publicly derived. OOPS Using C++
  • 4. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 4 ■ If access specifier is public 1. All public members of the base class become public members of derived class. 2. All protected members of base class become protected members of derived class. 3. Private members of base class are still private to base class ,thus not accessible to derived class. OOPS Using C++
  • 5. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 5 Inheritance using public access specifier #include"iostream.h" #include<string.h> class student { int rollno; char name[12]; public: void get_details() { cout<<"enter roll no and name"; cin>>rollno>>name; } void display() { cout<<"roll no is "<<rollno<<"and name is "<<name;} }; // Base class ends here OOPS Using C++
  • 6. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 6 class cse_student :public student { char sub1[10],sub2[10]; public: cse_student( ) // CONSTRUCTOR { strcpy(sub1,"OOPS"); strcpy(sub2,"PROGRAMMING");} void sub_display() { cout<<"and subjects are"<<sub1<<"and"<<sub2; } }; OOPS Using C++
  • 7. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 7 int main (void) { cse_student c1; c1.get_details(); c1.display(); c1.sub_display(); return 0; } Public members of base class are accessed as public of derived OOPS Using C++
  • 8. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 8 Output ■ enter roll no and name 12 ABC ■ roll no is 12 ■ and name is ABC ■ and subjects are OOPS and PROGRAMMING OOPS Using C++
  • 9. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 9 ■ If access specifier is private then 1. All public and protected members of base class become private members of derived class. 2. Private members of base class remain as private to it and hence not accessible to derived class. 3. All public and protected members of a class can be accessed by its own objects using the dot operator. 4. No member of base class is accessible to the objects of the derived class. OOPS Using C++
  • 10. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 10 Inheritance using private access specifier #include"iostream.h" #include<string.h> class student { int rollno; char name[12]; public: void get_details() { cout<<"enter roll no and name"; cin>>rollno>>name;} void display() { cout<<"roll no is "<<rollno<<"and name is"<<name;} }; OOPS Using C++
  • 11. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 11 class cse_student :private student { char sub1[10],sub2[10]; public: cse_student() // CONSTRUCTOR { strcpy(sub1,"OOPS"); strcpy(sub2,"PROGRAMMING"); get_details(); } void sub_display() { display(); // cout<<"rollno is "<<rollno; // Rollno not accessible cout<<"and subjects are"<<sub1<<"and"<<sub2;} }; Now private member of cse_student OOPS Using C++
  • 12. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 12 int main (void) { cse_student c1; //c1.get_details(); private member of //c1.display(); base class in-accessible c1.sub_display(); return 0; } OOPS Using C++
  • 13. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 13 Output enter roll no and name 12 neeraj roll no is 12 and name is neeraj and subjects are OOPS and PROGRAMMING OOPS Using C++
  • 14. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 14 “Protected” Access Specifier ■ A protected member of a class behaves similar to private members ( that is accessible only to members of the class) BUT ■ If the class is inherited by some class then protected members of base class may be accessible to derived class (different from PRIVATE MEMBERS OF BASE CLASS) ■ Thus protected members of a class are despite being private to it can be inherited by some other class. OOPS Using C++
  • 15. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 15 #include"iostream.h" class student { protected: int rollno; char name[12]; void get_details() { cout<<"enter roll no and name"; cin>>rollno>>name;} public: student() { get_details(); } void display() { cout<<endl<<"roll no is "<<rollno<<"and name is "<<name<<endl; } }; OOPS Using C++
  • 16. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 16 class it_student :public student { char sub1[10],sub2[10]; public: it_student() { strcpy(sub1,"OOPS"); strcpy(sub2,"PROGRAMMING"); get_details(); // valid as get_details is PROTECTED and not PRIVATE } void display1() { cout<<"rollno and name are"<<rollno<<"t"<<name; cout<<endl<<"and subjects are"<<sub1<<"and"<<sub2; }}; OOPS Using C++
  • 17. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 17 int main (void) { student s; //s.get_details(); // get_details is private to student s.display(); // valid being PUBLIC to student it_student c1; //c1.get_details(); //get_details is private to it_student c1.display(); //valid ,display() is public to //it_student c1.display1(); return 0; } OOPS Using C++
  • 18. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 18 Output ■ enter roll no and name123 angel ■ roll no is 123and name is angel ■ enter roll no and name45 angel2 ■ rollno and name are45 angel2 and subjects are OOPS and PROGRAMMING OOPS Using C++
  • 19. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 19 Inheriting a class in protected mode ■ Both public and protected members of base class become protected members of derived class. ■ Private members of base class remain private to it and hence in-accessible to derived class. OOPS Using C++
  • 20. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 20 Visibility of inherited members Base class visibility Derived class visibilty Public derivation Private derivation Protected Derivation Private Not inherited Not Inherited Not Inherited Protected Protected Private Protected Public Public Private Protected OOPS Using C++
  • 21. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY Forms Of Inheritance (a) Single Inheritance (b) Multiple Inheritance (c) Hierarchical Inheritance (d) Multilevel Inheritance (e) Hybrid Inheritance OOPS Using C++ 21
  • 22. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 22 More on inheritance…. Derived class may inherit traits from more than one class ( multiple inheritance) Class Z: access_specifier X, access_specifier Y... Y X Z Multiple Inheritance OOPS Using C++
  • 23. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 23 Example of multiple inheritance class X { protected: int a; public: void getX (int m) { a=m; } void display() { cout<<"The number in X is "<<a<<endl; } }; class Y { protected: int b; public: void getY (int n) { b=n; } void display1() { cout<<"The number in Y is "<<b<<endl; } }; OOPS Using C++
  • 24. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 24 class Z: public X, public Y { protected: int c; public: Z(int k) { getX(1); getY(2); c=k; } void disp() { display(); display1(); cout<<"the number in Z is <<c; } }; int main (void) { Z obj(3); obj.disp(); return 0; } OOPS Using C++
  • 25. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 25 Output ■ The number in X is 1 ■ The number in Y is 2 ■ the number in Z is 3 OOPS Using C++
  • 26. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 26 Ambiguity class M { public: void display(){cout<<“ class M”;} }; class N { public: void display(){cout<<“ class N”;} }; class P : public M , public N { public: void display(){cout<<“ class P”;} }; In this case function of derived class overrides the inherited function so when object of P calls display function it simply calls display of P. To invoke display of M & N we use scope resolution operator. OOPS Using C++
  • 27. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 27 int main () { P p; p.display(); p.M::display(); p.N::display(); p.P::display(); return 0; } OOPS Using C++
  • 28. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 28 ■ Inheriting one class by more than one class leads to Hierarchical inheritance. Z X W Y Hierarchical Inheritance OOPS Using C++
  • 29. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 29 Example class college { protected: char name[12]; void disp() { cout<<"name of college is"<<name<<endl;} public: college() {cout<<"constructor of college is called"<<endl; strcpy (name, "PIET"); }}; class it_dept : public college { int faculty; public: void get_faculty() { cout<<"enter no of faculty members in IT" <<endl ; cin>>faculty; } void display() { disp(); // now a protected member of it_dept cout<<"no of teachers in IT "<<faculty<<endl; } }; OOPS Using C++
  • 30. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 30 class ece_dept : public college { int faculty; public: void get_faculty() { cout<<"enter no of faculty members in ECE"<<endl; cin>>faculty;} void display() { disp(); // now a protected member of ece_dept cout<<"no of teachers in ECE "<<faculty<<endl;}}; void main() { it_dept C; ece_dept E; C.get_faculty(); E.get_faculty(); C.display(); E.display(); } OOPS Using C++
  • 31. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 31 OUTPUT constructor of college is called constructor of college is called enter no of faculty members in IT 12 enter no of faculty members in ECE 36 name of college is PIET no of teachers in IT 12 name of college is PIET no of teachers in ECE 36 OOPS Using C++
  • 32. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 32 ■ Deriving one class from other derived class leads to Multilevel inheritance. Y Z Multilevel Inheritance X Base class Intermediate base class Derived class test result Student OOPS Using C++
  • 33. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 33 Example class student { protected: int roll_number; public: void get_number(int a) { roll_number=a;} void put_number() {cout<<“Roll number is:"<<roll_number<<“n”; }}; class test : public student { protected: float sub1,sub2; public: void get_marks(float x, float y) { sub1=x; sub2=y;} void put_marks() { cout<<“Marks in sub1= "<<sub1<<endl; cout<<“Marks in sub2= "<<sub2<<endl; } }; OOPS Using C++
  • 34. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 34 class result : public test { float total; public: void display() { total=sub1+sub2; put_number(); put_marks(); cout<<“Total ="<< total <<endl; }}; int main() { result Abc; Abc.get_number(111); Abc.get_marks(75.0,59.5); Abc.display(); return 0; } OOPS Using C++
  • 35. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 35 ■ When more than one inheritance is used, it is called hybrid inheritance. Z W Hybrid Inheritance Sub class of X and Y Base class of X,Y Sub class of W Sub class of W Y X OOPS Using C++
  • 36. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 36 Virtual base class ■ In case of hybrid inheritance shown, two copies of W-members are created in class Z. Int I Int I( of W) Int J Int I (of W) Int K Int I (of X) Int I (of Y) Int J (of X) Int K (of Y) X Z Y W OOPS Using C++
  • 37. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 37 class W { public: int i; }; class X : public W { public: int j; }; class Y: public W { public: int k; }; class Z: public X, public Y { public: int sum; }; OOPS Using C++
  • 38. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 38 int main (void) { Z obj; obj.X::i=1; obj.j=2; obj.k=3; obj.sum=obj. X::i+ obj.j+obj.k; // obj.i will be ambiguous cout<<"the sum is "<<obj.sum; return 0; } OUTPUT The sum is 6 OOPS Using C++
  • 39. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 39 ■ If X and Y derives class W as a virtual class then only one copy of W-members will be inherited by X and Y. OOPS Using C++
  • 40. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 40 class W { public: int i; }; class X : virtual public W { public: int j; }; class Y: public virtual W { public: int k; }; class Z: public X, public Y { public: int sum; }; int main(void) { Z obj; //obj.X::i=1; obj.i=1; obj.j=2; obj.k=3; obj.sum= obj.i + obj.j + obj.k; cout<<"the sum is "<<obj.sum; return 0; } OOPS Using C++
  • 41. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 41 Abstract Classes ■ An abstract class is one that is not used to create objects. ■ An abstract class is only designed to act as a base class(to be inherited by other classes). OOPS Using C++
  • 42. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 42 Constructors in inheritance ■ Constructors play an important role in initializing objects. ■ In case of inheritance the base class constructors is first called of and then the derived class constructor is called. ■ When base class contains only default constructor then derived class may or may not have the constructor. ■ When 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. OOPS Using C++
  • 43. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 43 More about constructors…. ■ When an object of derived class is created In case of multi level inheritance constructors are called in the order of their derivation. class derived1 : public base { }; class derived2: public derived1 { }; That is base class constructor is called first, then constructor for derived1 class is called, then constructor for derived2 class is called. OOPS Using C++
  • 44. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 44 ■ In case of multiple inheritance, constructors are called in the order specified from left to right . ■ class derived: public base1,public base2 then derived d; will create an object of derived class by calling constructor for base classes base1 and then base2 OOPS Using C++
  • 45. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 45 Destructors in inheritance ■ When an object of derived class is destroyed 1. In case of multi level inheritance ,destructors are called in the reverse order of their derivation class derived : public base1,public base2{ }; Then destructor for derived is called then destructor for base2 is called then lastly destructor defined in base1 will be called. OOPS Using C++
  • 46. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 46 Passing arguments to base class ■ When an object of derived class is created, constructors of base class is also invoked. ■ If initialization of only derived class variables are to be done then arguments to the constructor can be passed in usual way. ■ But if base class constructor (invoked implicitly) also require some arguments then …….? OOPS Using C++
  • 47. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 47 ■ Constructor of derived class should be defined as Derived constructor (arg_list) : base1(arg_list1),base2(arg_list)…… { body of the constructor } ■ Arg_list for base classes will contain those arguments that are required by them,but are passed while calling derived class constructor. OOPS Using C++
  • 48. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 48 #include<iostream.h> #include<conio.h> #include<string.h> class college { protected: int id ; char name[12]; void disp() { cout<<"name of college is "<<name<<endl; } public: college(int a) { id=a; cout<<"constructor of college is called"<<endl; strcpy (name,"PIET"); } }; class it_dept : public college { int faculty; public: it_dept (int a, int b) : college (a) { faculty=b; } void display() { disp(); // now a protected member of it_dept cout<<“initialized college id is “<<id; cout<<"no of teachers in IT “ <<faculty<<endl; } }; int main() { clrscr(); it_dept C(10,12); C.display(); return 0; } Argumen t for base construct or OOPS Using C++
  • 49. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 49 OUTPUT ■ constructor of base college is called ■ initialized college id is 10 ■ name of college is PIET ■ no of teachers in it 12 OOPS Using C++
  • 50. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 50 ■ The constructors for virtual base classes are invoked before any non virtual base classes. METHOD OF INHERITANCE ORDER OF EXECUTION class B : public A { }; class A : public B, public C { }; class A : public B, virtual public C OOPS Using C++
  • 51. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 51 Initializing the class objects constructor (arglist) : initialization-section { assignment-section } Example: class ABC{ int a; public : ABC(int x) { a=x; cout<< “” ABC Constructor & values”<< a;} }; class XYZ : public ABC { int b,c; public: XYZ(int i, int j): a(i),b(2*j) { c=j; cout<< “XYZ Constructor & values”<<b<<c;} }; main() { XYZ x(2,3) } OOPS Using C++
  • 52. PANIPAT INSTITUTE OF ENGINEERING AND TECHNOLOGY 52 Member Classes : Nesting Of classes class alpha { }; class beta { }; class gamma { alpha a; beta b; ……… }; All objects of gamma class will contain the objects a,b; This kind of relationship is called containership or nesting. OOPS Using C++