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

What's hot

Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Разработка кросс-платформенного кода между iPhone &lt; -> Windows с помощью o...
Yandex
 
C++ Multiple Inheritance
C++ Multiple InheritanceC++ Multiple Inheritance
C++ Multiple Inheritance
harshaltambe
 
C# for C++ programmers
C# for C++ programmersC# for C++ programmers
C# for C++ programmers
Mark Whitaker
 
C++ polymorphism
C++ polymorphismC++ polymorphism
C++ polymorphism
FALLEE31188
 
Hybrid inheritance
Hybrid inheritanceHybrid inheritance
Hybrid inheritance
zindadili
 
Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)Inheritance, polymorphisam, abstract classes and composition)
Inheritance, polymorphisam, abstract classes and composition)
farhan amjad
 
E5
E5E5
E5
lksoo
 
OOP with Java - continued
OOP with Java - continuedOOP with Java - continued
OOP with Java - continued
RatnaJava
 

What's hot (8)

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

Similar to Inheritance

chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
study material
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
RutujaTandalwade
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
DeepasCSE
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
Ameen Sha'arawi
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
Kamlesh Singh
 
Object Oriented Programming - Inheritance
Object Oriented Programming - InheritanceObject Oriented Programming - Inheritance
Object Oriented Programming - Inheritance
Dudy Ali
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
Amritsinghmehra
 
Inheritance and its types explained.ppt
Inheritance and its types  explained.pptInheritance and its types  explained.ppt
Inheritance and its types explained.ppt
SarthakKumar93
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
shubhra chauhan
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
CS3391 -OOP -UNIT – II  NOTES FINAL.pdfCS3391 -OOP -UNIT – II  NOTES FINAL.pdf
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
AALIM MUHAMMED SALEGH COLLEGE OF ENGINEERING
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
Praveen M Jigajinni
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
EmanAsem4
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
kishu0005
 
inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPM
PKKArc
 
Inheritance
InheritanceInheritance
Inheritance
poonam.rwalia
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
BhavyaJain137
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
Aadil Ansari
 
2.6 Types of Inheritance in OOP C++.pptx
2.6 Types of Inheritance in OOP C++.pptx2.6 Types of Inheritance in OOP C++.pptx
2.6 Types of Inheritance in OOP C++.pptx
tirseishwaricomp
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
University College of Engineering Kakinada, JNTUK - Kakinada, India
 
Inheritance
InheritanceInheritance
Inheritance
zindadili
 

Similar to Inheritance (20)

chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
Inheritance.pptx
Inheritance.pptxInheritance.pptx
Inheritance.pptx
 
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptxINHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
 
Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++Support for Object-Oriented Programming (OOP) in C++
Support for Object-Oriented Programming (OOP) in C++
 
Java Basic day-2
Java Basic day-2Java Basic day-2
Java Basic day-2
 
Object Oriented Programming - Inheritance
Object Oriented Programming - InheritanceObject Oriented Programming - Inheritance
Object Oriented Programming - Inheritance
 
OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Inheritance and its types explained.ppt
Inheritance and its types  explained.pptInheritance and its types  explained.ppt
Inheritance and its types explained.ppt
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
CS3391 -OOP -UNIT – II  NOTES FINAL.pdfCS3391 -OOP -UNIT – II  NOTES FINAL.pdf
CS3391 -OOP -UNIT – II NOTES FINAL.pdf
 
6 Inheritance
6 Inheritance6 Inheritance
6 Inheritance
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
inheritance in OOPM
inheritance in OOPMinheritance in OOPM
inheritance in OOPM
 
Inheritance
InheritanceInheritance
Inheritance
 
Multiple Inheritance
Multiple InheritanceMultiple Inheritance
Multiple Inheritance
 
Object oriented programming 2
Object oriented programming 2Object oriented programming 2
Object oriented programming 2
 
2.6 Types of Inheritance in OOP C++.pptx
2.6 Types of Inheritance in OOP C++.pptx2.6 Types of Inheritance in OOP C++.pptx
2.6 Types of Inheritance in OOP C++.pptx
 
Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2Object Oriented Programming using C++ - Part 2
Object Oriented Programming using C++ - Part 2
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
nedcocy
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
PriyankaKilaniya
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
mahaffeycheryld
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
Yasser Mahgoub
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
171ticu
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
bijceesjournal
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
VANDANAMOHANGOUDA
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
AjmalKhan50578
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
PreethaV16
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
edwin408357
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
Anant Corporation
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
UReason
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 

Recently uploaded (20)

一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
一比一原版(爱大毕业证书)爱荷华大学毕业证如何办理
 
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
Prediction of Electrical Energy Efficiency Using Information on Consumer's Ac...
 
Generative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdfGenerative AI Use cases applications solutions and implementation.pdf
Generative AI Use cases applications solutions and implementation.pdf
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 08 Doors and Windows.pdf
 
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样学校原版美国波士顿大学毕业证学历学位证书原版一模一样
学校原版美国波士顿大学毕业证学历学位证书原版一模一样
 
Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...Rainfall intensity duration frequency curve statistical analysis and modeling...
Rainfall intensity duration frequency curve statistical analysis and modeling...
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
ITSM Integration with MuleSoft.pptx
ITSM  Integration with MuleSoft.pptxITSM  Integration with MuleSoft.pptx
ITSM Integration with MuleSoft.pptx
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
Welding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdfWelding Metallurgy Ferrous Materials.pdf
Welding Metallurgy Ferrous Materials.pdf
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
Object Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOADObject Oriented Analysis and Design - OOAD
Object Oriented Analysis and Design - OOAD
 
Engineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdfEngineering Standards Wiring methods.pdf
Engineering Standards Wiring methods.pdf
 
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by AnantLLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
LLM Fine Tuning with QLoRA Cassandra Lunch 4, presented by Anant
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
Data Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason WebinarData Driven Maintenance | UReason Webinar
Data Driven Maintenance | UReason Webinar
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 

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++