SlideShare a Scribd company logo
Today’s LectureToday’s Lecture
 Access SpecifiersAccess Specifiers
 Derived ClassesDerived Classes
 Type of InheritanceType of Inheritance
 Derived class Constructors (Constructors in single Inheritance)Derived class Constructors (Constructors in single Inheritance)
 Multiple InheritanceMultiple Inheritance
 Constructors in multiple InheritanceConstructors in multiple Inheritance
 Constructors in single/multiple Inheritance with arguments.Constructors in single/multiple Inheritance with arguments.
AbbasajmalAbbasajmal
0314995850003149958500
InheritanceInheritance
 Inheritance is the second most important feature of OOP.Inheritance is the second most important feature of OOP.
 In inheritance, the code of existing classes is used for making newIn inheritance, the code of existing classes is used for making new
classes.classes.
 This saves time for writing and debugging the entire code for a newThis saves time for writing and debugging the entire code for a new
class.class.
 To inherit means to receive.To inherit means to receive.
 In inheritance a new class is written such that it can access or use theIn inheritance a new class is written such that it can access or use the
members of an existing class.members of an existing class.
 The new class that can access the members of an existing class isThe new class that can access the members of an existing class is
called the derived class or child class. The existing class is called thecalled the derived class or child class. The existing class is called the
base class or parent class.base class or parent class.
InheritanceInheritance
 The derived class can use the data members and member functions of theThe derived class can use the data members and member functions of the
base class.base class.
 It can also have its own data members and member functions.It can also have its own data members and member functions.
 Thus a derived class can even be larger than a base class.Thus a derived class can even be larger than a base class.
InheritanceInheritance
 The figure shows the relationship between aThe figure shows the relationship between a
derived class and the base class.derived class and the base class.
 The arrow is drawn from the derived classThe arrow is drawn from the derived class
to the base class.to the base class.
 The direction of the arrow indicates that theThe direction of the arrow indicates that the
derived class can access members of thederived class can access members of the
base class but base class cannot accessbase class but base class cannot access
members of its derived class.members of its derived class.
 In the figure shown on the left, the derivedIn the figure shown on the left, the derived
class has only one member of its own. Theclass has only one member of its own. The
two members shown in dotted lines are thetwo members shown in dotted lines are the
members of the base class. The derivedmembers of the base class. The derived
class can also access these two members ofclass can also access these two members of
the base class. Thus, whereas an object ofthe base class. Thus, whereas an object of
the base class can access only twothe base class can access only two
members, an object of the derived class canmembers, an object of the derived class can
access three members. A new class can beaccess three members. A new class can be
derived from one or more existing Classes.derived from one or more existing Classes.
SpecifiersSpecifiers
 Public [Already Discussed]Public [Already Discussed]
 Private [Already Discussed]Private [Already Discussed]
 ProtectedProtected
 The public members of a class are accessible by all functions in the program and the privateThe public members of a class are accessible by all functions in the program and the private
members of a class are accessible only by member functions and friend functions of thatmembers of a class are accessible only by member functions and friend functions of that
class. Similarly, the protected members of a class are accessible by the member functionsclass. Similarly, the protected members of a class are accessible by the member functions
and friend functions of that Class.and friend functions of that Class.
 The protected members of a base class are, however, accessible by members of its derivedThe protected members of a base class are, however, accessible by members of its derived
classes but the private members of the base class are not accessible directly by members ofclasses but the private members of the base class are not accessible directly by members of
its derived classes. This is the main difference between the protected and the private accessits derived classes. This is the main difference between the protected and the private access
specifiers.specifiers.
 The protected members of a base class fall between private and public member. TheseThe protected members of a base class fall between private and public member. These
members are public for the derived class but for the rest of the program, these are treated asmembers are public for the derived class but for the rest of the program, these are treated as
private.private.
Defining Derived ClassDefining Derived Class
The syntax for defining a derived class is slightly different from theThe syntax for defining a derived class is slightly different from the
syntax of the base class definition. The declaration of a derived classsyntax of the base class definition. The declaration of a derived class
also includes the name of the base class from which it is derived.also includes the name of the base class from which it is derived.
The general syntax for defining a derived class is:The general syntax for defining a derived class is:
class sub_class_name : specifierclass sub_class_name : specifier
base_class_name{ ………………………………….base_class_name{ ………………………………….
members of derived classmembers of derived class
………………………………………………………………………………}}
WhereWhere
sub_class_name : represents name of the derived class.sub_class_name : represents name of the derived class.
: (colon) sets relation between the classes.: (colon) sets relation between the classes.
specifier represents the access specifier. It may be public, private orspecifier represents the access specifier. It may be public, private or
Protected, base_class_name : represents name of the base class.Protected, base_class_name : represents name of the base class.
For example, a class "student" isFor example, a class "student" is
defined as:defined as:
class student {class student {
private: Char name[15], address[15];private: Char name[15], address[15];
public:public:
void input (void);void input (void);
void show(void);void show(void);
};};
 The class student has two data members and two memberThe class student has two data members and two member
functions. Suppose the marks obtained by a student in threefunctions. Suppose the marks obtained by a student in three
different subjects and the total marks of these subjects are todifferent subjects and the total marks of these subjects are to
be included as new data members in the above class. This isbe included as new data members in the above class. This is
done by adding new members in the class. There are two waysdone by adding new members in the class. There are two ways
in which these new members can be added to the class:in which these new members can be added to the class:
•• Add new members in the originalAdd new members in the original
•• Define a new class that has the new members and that alsoDefine a new class that has the new members and that also
uses members of the existing "student" class. Using theuses members of the existing "student" class. Using the
members of an existing class is the principle of inheritance.members of an existing class is the principle of inheritance.
The new class is the derived class. The existing class serves asThe new class is the derived class. The existing class serves as
the base class for the derived class.the base class for the derived class.
Example ExplanationExample Explanation
Driving a new class from the existing class reduces the size of theDriving a new class from the existing class reduces the size of the
program. It also eliminates duplication of code within theprogram. It also eliminates duplication of code within the
program. For example, let the name of the new class be marks.program. For example, let the name of the new class be marks.
This class uses the members of the existing student class.This class uses the members of the existing student class.
class marks : public student {class marks : public student {
private:private:
int s1,s2,s3, total;int s1,s2,s3, total;
public:public:
void inputmarks(void);void inputmarks(void);
void show_detail(void); };void show_detail(void); };
Defining Derived ClassDefining Derived Class
(cont…)(cont…)
 The cIass marks is derived from the class student. The class marks is theThe cIass marks is derived from the class student. The class marks is the
derived class. The class student is the base class.derived class. The class student is the base class.
 The derived class has four data members of integer type and two memberThe derived class has four data members of integer type and two member
functions.functions.
 It also uses the code of the base class student.It also uses the code of the base class student.
 The derived class cannot directly access the private data members the baseThe derived class cannot directly access the private data members the base
class student by using the dot operator.class student by using the dot operator.
 These members are only accessible to the derived class through theThese members are only accessible to the derived class through the
interface functions within the base class.interface functions within the base class.
Defining Derived ClassDefining Derived Class
(cont..)(cont..)
 There are three kinds of inheritance.There are three kinds of inheritance.
•• Public • Private • ProtectedPublic • Private • Protected
 Public InheritancePublic Inheritance
In Public Inheritance, the public members of the base classIn Public Inheritance, the public members of the base class
become the public members of the derived class.become the public members of the derived class.
Thus the objects of the derived class can access publicThus the objects of the derived class can access public
members (both data and functions) of the base class.members (both data and functions) of the base class.
Similarly, the protected data members of the base class alsoSimilarly, the protected data members of the base class also
become the protected members of derived class.become the protected members of derived class.
Types of InheritanceTypes of Inheritance
class sub_class_name : public base_class_name {class sub_class_name : public base_class_name {
……………………………………
……………………………………
) ;) ;
wherewhere
public: specifies the public inheritance.public: specifies the public inheritance.
sub_class__name: represents name of the derived class.sub_class__name: represents name of the derived class.
base_class_name: represents name of the base class.base_class_name: represents name of the base class.
The general syntax for deriving a public classThe general syntax for deriving a public class
from base class is:from base class is:
Example: Public InheritanceExample: Public Inheritance
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class A {class A {
private:private:
int a1,a2;int a1,a2;
protected:protected:
int pa1,pa2;int pa1,pa2;
public:public:
void ppp (void){void ppp (void){
cout<<"Value of pal of classcout<<"Value of pal of class
A="<<pa1<<endl;A="<<pa1<<endl;
cout<<"Value of pa2 of classcout<<"Value of pa2 of class
A="<<pa2<<endl; }};A="<<pa2<<endl; }};
class B : public A {class B : public A {
public:public:
void get(void){void get(void){
cout << "Enter value pal ? ";cout << "Enter value pal ? ";
cin>>pa1;cin>>pa1;
cout<<"Enter value pa2 ?";cout<<"Enter value pa2 ?";
cin>>pa2;}};cin>>pa2;}};
main (){main (){
B ob;B ob;
ob.get();ob.get();
ob.ppp();ob.ppp();
};};
In the above program the class B is publicly derived from classIn the above program the class B is publicly derived from class
A. The object of the class BA. The object of the class B
 Cannot access the private data member’s a1 & a2 of base classCannot access the private data member’s a1 & a2 of base class
A.A.
 Can access the public function member ppp of base class A.Can access the public function member ppp of base class A.
 Can access the protected data members pa1 & pa2 of baseCan access the protected data members pa1 & pa2 of base
class A.class A.
Example ExplanationExample Explanation
In private inheritance all the member in base class became theIn private inheritance all the member in base class became the
private members od the derived class.private members od the derived class.
In private Inheritance the objects of the derived class cannotIn private Inheritance the objects of the derived class cannot
access any member of the base class directly. Its objects can onlyaccess any member of the base class directly. Its objects can only
access the public members of the derived class directly.access the public members of the derived class directly.
The general syntax for deriving private class from base class is:The general syntax for deriving private class from base class is:
class sub_class_name : private base_class_name {class sub_class_name : private base_class_name {
…………………………………………………………………….}.}
Private: specifies private inheritance.Private: specifies private inheritance.
sub_class_name: represents name of the derived class.sub_class_name: represents name of the derived class.
base_class_name: represents name of the base class.base_class_name: represents name of the base class.
Private InheritancePrivate Inheritance
Example: Private InheritanceExample: Private Inheritance
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class A {class A {
private:private:
int a1,a2;int a1,a2;
protected:protected:
int pa1,pa2;int pa1,pa2;
public:public:
void ppp (void){void ppp (void){
cout<<"Value of pa1 of classcout<<"Value of pa1 of class
A="<<pa1<<endl;A="<<pa1<<endl;
cout<<"Value of pa2 of classcout<<"Value of pa2 of class
A="<<pa2<<endl;}A="<<pa2<<endl;}
};};
class B : private Aclass B : private A
{ public:{ public:
void get(void)void get(void)
{cout<<"Enter value of pa1 ";{cout<<"Enter value of pa1 ";
cin>>pa1;cin>>pa1;
cout<<"Enter value of pa2 =";cout<<"Enter value of pa2 =";
cin>>pa2;cin>>pa2;
cout<<"Value of pa1 of classcout<<"Value of pa1 of class
A="<<pa1<<endl;A="<<pa1<<endl;
cout<<"Value of pa2 of classcout<<"Value of pa2 of class
A="<<pa2<<endl;}};A="<<pa2<<endl;}};
main ( ){main ( ){
B ob;B ob;
ob.get(); }ob.get(); }
In the above program, the class B is derived as private for theIn the above program, the class B is derived as private for the
base class A. The objects of class B:base class A. The objects of class B:
 cannot access the private data members a1 & a2 of base classcannot access the private data members a1 & a2 of base class
A.A.
 cannot access the public function member ppp of base class A.cannot access the public function member ppp of base class A.
 can only access the public members “get” of derived class Bcan only access the public members “get” of derived class B
directly.directly.
Example ExplanationExample Explanation
In protected inheritance the public and protected members of theIn protected inheritance the public and protected members of the
base class became the protected members of the derived classbase class became the protected members of the derived class
whereas the private members of the base class remain private inwhereas the private members of the base class remain private in
derived class.derived class.
The object of the class that is derived as protected can accessThe object of the class that is derived as protected can access
only the public members of the derived class directly.only the public members of the derived class directly.
The general syntax for deriving a protected Class from base classThe general syntax for deriving a protected Class from base class
is:is: class sub_class_name: protected base_class_name {class sub_class_name: protected base_class_name {
……………………………………};};
where protected: specifies protected inheritance.where protected: specifies protected inheritance.
sub_class_name: represents name of the derived class.sub_class_name: represents name of the derived class.
Base_class_name: represents name of the base class.Base_class_name: represents name of the base class.
Protected InheritanceProtected Inheritance
Example: Protected InheritanceExample: Protected Inheritance
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class Aclass A
{{
private :private :
int a1,a2;int a1,a2;
protected:protected:
int pa1,pa2;int pa1,pa2;
public:public:
void get(void){void get(void){
cout<<"value of pa1 of class A ";cout<<"value of pa1 of class A ";
cout<<"value of pa2 of class A ?";}};cout<<"value of pa2 of class A ?";}};
class B: protected A{class B: protected A{
public:public:
void get (void){void get (void){
cout<<" Enter value of pa1=";cout<<" Enter value of pa1=";
cin>>pa1;cin>>pa1;
cout<<"Value of pa2";cout<<"Value of pa2";
cin>>pa2;cin>>pa2;
cout<<"Value of pa1 of classcout<<"Value of pa1 of class
A="<<pa1<<endl;A="<<pa1<<endl;
cout<<"Value of pa2 of classcout<<"Value of pa2 of class
A="<<pa2<<endl;}};A="<<pa2<<endl;}};
main(){main(){
B ob;B ob;
ob.get(); }ob.get(); }
In the above program, thc class B is derived as protected from theIn the above program, thc class B is derived as protected from the
base class A. The objects of class B:base class A. The objects of class B:
 can only access the public members of derived class Acan only access the public members of derived class A
directly.directly.
 Difference between private and protected inheritanceDifference between private and protected inheritance
 If you inherit a class in private mode the public members of the base class become private in the
derived class. If the base class has some protected members they also become private. Now
these members are not accessible by the functions of the classes which are derived from the
child class. 
Incase of protected inheritance, the public members of the base class become protected in the
derived class. If the base class has got protected members they also become protected in the
derived class. Now the main difference between protected and private inheritance is that the
members inherited in protected mode are ready for further inheritance which is not the case for
private inheritance. 
Example ExplanationExample Explanation
 In inheritance, a constructor of the derived class as well theIn inheritance, a constructor of the derived class as well the
constructor functions of the base class are automaticallyconstructor functions of the base class are automatically
executed when an object of the derived class is created.executed when an object of the derived class is created.
 The following example explains the concept of constructorThe following example explains the concept of constructor
functions in single inheritance.functions in single inheritance.
Derived Class ConstructorsDerived Class Constructors
Example: Derived Class ConstructorsExample: Derived Class Constructors
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class bb{class bb{
public:public:
bb(void){bb(void){
cout<<"Constructor of basecout<<"Constructor of base
class"<<endl;class"<<endl;
}}
};};
class dd:public bb{class dd:public bb{
public:public:
dd(void){dd(void){
cout<<"Constructor of derivedcout<<"Constructor of derived
class"<<endl;class"<<endl;
}}
};};
main(){main(){
dd abc;dd abc;
}}
 In the above program, when an object of the derived class ddIn the above program, when an object of the derived class dd
is created, the constructors of both the derived class as well asis created, the constructors of both the derived class as well as
the base class are executed.the base class are executed.
Example ExplanationExample Explanation
 In Multiple inheritances, a class is derived by using more thanIn Multiple inheritances, a class is derived by using more than
one classes.one classes.
 In multiple inheritance the derived class receives the membersIn multiple inheritance the derived class receives the members
of two or more base classes.of two or more base classes.
 Multiple inheritance is a powerful feature of Object OrientedMultiple inheritance is a powerful feature of Object Oriented
Programming.Programming.
 This technique reduces the program size and saveThis technique reduces the program size and save
programmer's. time. The programmer uses the existing baseprogrammer's. time. The programmer uses the existing base
classes in the program coding to solve problems.classes in the program coding to solve problems.
 The figure shown below illustrates the relation of derived classThe figure shown below illustrates the relation of derived class
with base classes.with base classes.
  
Multiple InheritanceMultiple Inheritance
  
Multiple InheritanceMultiple Inheritance
In the figure, Class C is derived from two base classes A & B. The syntax ofIn the figure, Class C is derived from two base classes A & B. The syntax of
multiple inheritance is similar to that of single inheritance class. The name ofmultiple inheritance is similar to that of single inheritance class. The name of
base classes are written separated by comma(,).base classes are written separated by comma(,).
Syntax:Syntax:
Class sub_class : sp1 b_class_1, sp2 b_class_2……..Class sub_class : sp1 b_class_1, sp2 b_class_2……..
{{
………………………………………………....
};};
Where :Where :
sp1 represents the access specifier of the first base class.sp1 represents the access specifier of the first base class.
sp2 represents the access specifier of the second base class.sp2 represents the access specifier of the second base class.
  
Multiple InheritanceMultiple Inheritance
Example: Multiple InheritanceExample: Multiple Inheritance
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class student {class student {
private:private:
char name[20],address[20];char name[20],address[20];
public:public:
void input (void){void input (void){
cout<<"Enter name:";cin>>name;cout<<"Enter name:";cin>>name;
cout<<"Enter address:";cin>>address;cout<<"Enter address:";cin>>address;
}}
void print(void){void print(void){
cout<<"Name:"<<name<<endl;cout<<"Name:"<<name<<endl;
cout<<"Address:"<<address<<endl;cout<<"Address:"<<address<<endl;
}}
};};
class marks {class marks {
private:private:
int s1,s2,total;int s1,s2,total;
public:public:
void inputmarks(void){void inputmarks(void){
cout<<"Enter marks of S1=";cin>>s1;cout<<"Enter marks of S1=";cin>>s1;
cout<<"Enter marks of S2=";cin>>s2;cout<<"Enter marks of S2=";cin>>s2;
total=s1+s2;}total=s1+s2;}
void showmarks(void){void showmarks(void){
cout<<"Marks in s1="<<s1<<endl;cout<<"Marks in s1="<<s1<<endl;
Example: Multiple Inheritance (Cont..)Example: Multiple Inheritance (Cont..)
cout<<"Marks incout<<"Marks in
s2="<<s2<<endl;s2="<<s2<<endl;
cout<<"Totalcout<<"Total
Marks="<<total<<endl;Marks="<<total<<endl;
}}
};};
class show: public student, publicclass show: public student, public
marks{marks{
public:public:
void showrec(){void showrec(){
cout<<"Record is"<<endl;cout<<"Record is"<<endl;
print();print();
showmarks();showmarks();
}}
};};
main(){main(){
show m;show m;
m.input();m.input();
m.inputmarks();m.inputmarks();
m.showrec();m.showrec();
}}
 When a class is derived from more than one base classes, the constructor ofWhen a class is derived from more than one base classes, the constructor of
the derived class as well as all of its base classes are executed when anthe derived class as well as all of its base classes are executed when an
object of the derived class is created.object of the derived class is created.
 If the constructor functions have no parameters then first the constructorIf the constructor functions have no parameters then first the constructor
functions of the base class are executed and then the constructor functionsfunctions of the base class are executed and then the constructor functions
of the derived class is executed.of the derived class is executed.
Constructor in Multiple InheritanceConstructor in Multiple Inheritance
Example: Constructor in Multiple InheritanceExample: Constructor in Multiple Inheritance
#include<iostream>#include<iostream>
using namespace std;using namespace std;
class aa{class aa{
public:public:
aa(){aa(){
cout<<"Class aa"<<endl;cout<<"Class aa"<<endl;
}}
};};
class bb{class bb{
public:public:
bb(){bb(){
cout<<"Class bb"<<endl;cout<<"Class bb"<<endl;
};};
class cc:public aa,public bb{class cc:public aa,public bb{
public:public:
cc(){cc(){
cout<<"Class cc"<<endl;cout<<"Class cc"<<endl;
}}
};};
main(){main(){
cc o;cc o;
}}
 In the above program, three classes are created.In the above program, three classes are created.
 The cc class is publicly derived from two class aa and bb.The cc class is publicly derived from two class aa and bb.
 All the classes have constructor functions are executed in theAll the classes have constructor functions are executed in the
following sequence.following sequence.
1.1. Constructor of class aa is executed first.Constructor of class aa is executed first.
2.2. Constructor of class bb is executed second.Constructor of class bb is executed second.
3.3. Constructor of class cc is executed last.Constructor of class cc is executed last.
Example ExplanationExample Explanation
 Constructor in Single Inheritance with argumentsConstructor in Single Inheritance with arguments
 Constructor in Multiple Inheritance with argumentsConstructor in Multiple Inheritance with arguments
AssignmentAssignment

More Related Content

PPT
Inheritance in java
PPTX
Inheritance and its types In Java
PDF
javainheritance
PPTX
Java Inheritance - sub class constructors - Method overriding
PPT
inheritance
PPTX
Java(inheritance)
PDF
Inheritance In Java
PPTX
Inheritance In Java
Inheritance in java
Inheritance and its types In Java
javainheritance
Java Inheritance - sub class constructors - Method overriding
inheritance
Java(inheritance)
Inheritance In Java
Inheritance In Java

What's hot (20)

PPT
Inheritance and Polymorphism
PDF
Learn C# Programming - Classes & Inheritance
PPTX
Inheritance ppt
PPT
Unit 3 Java
PPTX
Inheritance in java
PDF
java-06inheritance
PPT
Java inheritance
PDF
E4
PPTX
Inheritance in oops
PPTX
Java Inheritance
PPT
Java: Inheritance
DOCX
OOP Concepets and UML Class Diagrams
PDF
OOP Inheritance
PPTX
Inheritance in Java
PPT
friend function(c++)
PPTX
Inheritance in JAVA PPT
PDF
Inheritance used in java
PPT
inheritance
PDF
Principles of Object Oriented Programming
Inheritance and Polymorphism
Learn C# Programming - Classes & Inheritance
Inheritance ppt
Unit 3 Java
Inheritance in java
java-06inheritance
Java inheritance
E4
Inheritance in oops
Java Inheritance
Java: Inheritance
OOP Concepets and UML Class Diagrams
OOP Inheritance
Inheritance in Java
friend function(c++)
Inheritance in JAVA PPT
Inheritance used in java
inheritance
Principles of Object Oriented Programming
Ad

Viewers also liked (20)

PPT
Week 22 virtual class sport_26.9.2013
PPT
11 constructors in derived classes
PPTX
My personal development plan
PPT
Lecture18
PPTX
Personal development plan, realising your potential
PPTX
My personal development plan
PPTX
Interoduction to c++
PDF
Personal Development Plan 2014 - 2015
PPTX
Uid
PPTX
virtual classroom for college major project for computer science.
PPTX
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
PPTX
Abstract Base Class and Polymorphism in C++
PPTX
07. Virtual Functions
PPT
Lec 42.43 - virtual.functions
PPTX
Virtual base class
PPS
Personality Development
PPT
Personality development presentation
PPTX
Personality
PPS
Presentation On Personality
PPTX
My Personal Development Plan
Week 22 virtual class sport_26.9.2013
11 constructors in derived classes
My personal development plan
Lecture18
Personal development plan, realising your potential
My personal development plan
Interoduction to c++
Personal Development Plan 2014 - 2015
Uid
virtual classroom for college major project for computer science.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Abstract Base Class and Polymorphism in C++
07. Virtual Functions
Lec 42.43 - virtual.functions
Virtual base class
Personality Development
Personality development presentation
Personality
Presentation On Personality
My Personal Development Plan
Ad

Similar to OOPs Lecture 2 (20)

PDF
lecture-2021inheritance-160705095417.pdf
PPTX
[OOP - Lec 20,21] Inheritance
PPT
11 Inheritance.ppt
PPT
Inheritance in C++
PDF
Chapter 6 and inheritance OOP C++ tu ioe
PPTX
Intro To C++ - Class #22: Inheritance, Part 1
PPTX
Inheritance
PPT
Inheritance OOP Concept in C++.
PPTX
Inheritance
PPTX
Inheritance
PPTX
Lecture 5.mte 407
PPTX
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
PPT
PPT
session 24_Inheritance.ppt
PPT
Lecturespecial
PPT
PPT
Inheritance in C++
PPTX
Concept of Classes in c++.ist mse16.pptx
PPTX
Inheritance in c++
PDF
2 BytesC++ course_2014_c11_ inheritance
lecture-2021inheritance-160705095417.pdf
[OOP - Lec 20,21] Inheritance
11 Inheritance.ppt
Inheritance in C++
Chapter 6 and inheritance OOP C++ tu ioe
Intro To C++ - Class #22: Inheritance, Part 1
Inheritance
Inheritance OOP Concept in C++.
Inheritance
Inheritance
Lecture 5.mte 407
INHERITANCE IN C++ +2 COMPUTER SCIENCE CBSE AND STATE SYLLABUS
session 24_Inheritance.ppt
Lecturespecial
Inheritance in C++
Concept of Classes in c++.ist mse16.pptx
Inheritance in c++
2 BytesC++ course_2014_c11_ inheritance

Recently uploaded (20)

PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
Transform Your Business with a Software ERP System
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
medical staffing services at VALiNTRY
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administration Chapter 2
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Introduction to Artificial Intelligence
Upgrade and Innovation Strategies for SAP ERP Customers
Odoo POS Development Services by CandidRoot Solutions
VVF-Customer-Presentation2025-Ver1.9.pptx
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Understanding Forklifts - TECH EHS Solution
Transform Your Business with a Software ERP System
PTS Company Brochure 2025 (1).pdf.......
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Navsoft: AI-Powered Business Solutions & Custom Software Development
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Softaken Excel to vCard Converter Software.pdf
history of c programming in notes for students .pptx
How Creative Agencies Leverage Project Management Software.pdf
Reimagine Home Health with the Power of Agentic AI​
medical staffing services at VALiNTRY
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administration Chapter 2
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Introduction to Artificial Intelligence

OOPs Lecture 2

  • 1. Today’s LectureToday’s Lecture  Access SpecifiersAccess Specifiers  Derived ClassesDerived Classes  Type of InheritanceType of Inheritance  Derived class Constructors (Constructors in single Inheritance)Derived class Constructors (Constructors in single Inheritance)  Multiple InheritanceMultiple Inheritance  Constructors in multiple InheritanceConstructors in multiple Inheritance  Constructors in single/multiple Inheritance with arguments.Constructors in single/multiple Inheritance with arguments. AbbasajmalAbbasajmal 0314995850003149958500
  • 2. InheritanceInheritance  Inheritance is the second most important feature of OOP.Inheritance is the second most important feature of OOP.  In inheritance, the code of existing classes is used for making newIn inheritance, the code of existing classes is used for making new classes.classes.  This saves time for writing and debugging the entire code for a newThis saves time for writing and debugging the entire code for a new class.class.  To inherit means to receive.To inherit means to receive.  In inheritance a new class is written such that it can access or use theIn inheritance a new class is written such that it can access or use the members of an existing class.members of an existing class.  The new class that can access the members of an existing class isThe new class that can access the members of an existing class is called the derived class or child class. The existing class is called thecalled the derived class or child class. The existing class is called the base class or parent class.base class or parent class.
  • 3. InheritanceInheritance  The derived class can use the data members and member functions of theThe derived class can use the data members and member functions of the base class.base class.  It can also have its own data members and member functions.It can also have its own data members and member functions.  Thus a derived class can even be larger than a base class.Thus a derived class can even be larger than a base class.
  • 4. InheritanceInheritance  The figure shows the relationship between aThe figure shows the relationship between a derived class and the base class.derived class and the base class.  The arrow is drawn from the derived classThe arrow is drawn from the derived class to the base class.to the base class.  The direction of the arrow indicates that theThe direction of the arrow indicates that the derived class can access members of thederived class can access members of the base class but base class cannot accessbase class but base class cannot access members of its derived class.members of its derived class.  In the figure shown on the left, the derivedIn the figure shown on the left, the derived class has only one member of its own. Theclass has only one member of its own. The two members shown in dotted lines are thetwo members shown in dotted lines are the members of the base class. The derivedmembers of the base class. The derived class can also access these two members ofclass can also access these two members of the base class. Thus, whereas an object ofthe base class. Thus, whereas an object of the base class can access only twothe base class can access only two members, an object of the derived class canmembers, an object of the derived class can access three members. A new class can beaccess three members. A new class can be derived from one or more existing Classes.derived from one or more existing Classes.
  • 5. SpecifiersSpecifiers  Public [Already Discussed]Public [Already Discussed]  Private [Already Discussed]Private [Already Discussed]  ProtectedProtected  The public members of a class are accessible by all functions in the program and the privateThe public members of a class are accessible by all functions in the program and the private members of a class are accessible only by member functions and friend functions of thatmembers of a class are accessible only by member functions and friend functions of that class. Similarly, the protected members of a class are accessible by the member functionsclass. Similarly, the protected members of a class are accessible by the member functions and friend functions of that Class.and friend functions of that Class.  The protected members of a base class are, however, accessible by members of its derivedThe protected members of a base class are, however, accessible by members of its derived classes but the private members of the base class are not accessible directly by members ofclasses but the private members of the base class are not accessible directly by members of its derived classes. This is the main difference between the protected and the private accessits derived classes. This is the main difference between the protected and the private access specifiers.specifiers.  The protected members of a base class fall between private and public member. TheseThe protected members of a base class fall between private and public member. These members are public for the derived class but for the rest of the program, these are treated asmembers are public for the derived class but for the rest of the program, these are treated as private.private.
  • 6. Defining Derived ClassDefining Derived Class The syntax for defining a derived class is slightly different from theThe syntax for defining a derived class is slightly different from the syntax of the base class definition. The declaration of a derived classsyntax of the base class definition. The declaration of a derived class also includes the name of the base class from which it is derived.also includes the name of the base class from which it is derived. The general syntax for defining a derived class is:The general syntax for defining a derived class is: class sub_class_name : specifierclass sub_class_name : specifier base_class_name{ ………………………………….base_class_name{ …………………………………. members of derived classmembers of derived class ………………………………………………………………………………}} WhereWhere sub_class_name : represents name of the derived class.sub_class_name : represents name of the derived class. : (colon) sets relation between the classes.: (colon) sets relation between the classes. specifier represents the access specifier. It may be public, private orspecifier represents the access specifier. It may be public, private or Protected, base_class_name : represents name of the base class.Protected, base_class_name : represents name of the base class.
  • 7. For example, a class "student" isFor example, a class "student" is defined as:defined as: class student {class student { private: Char name[15], address[15];private: Char name[15], address[15]; public:public: void input (void);void input (void); void show(void);void show(void); };};
  • 8.  The class student has two data members and two memberThe class student has two data members and two member functions. Suppose the marks obtained by a student in threefunctions. Suppose the marks obtained by a student in three different subjects and the total marks of these subjects are todifferent subjects and the total marks of these subjects are to be included as new data members in the above class. This isbe included as new data members in the above class. This is done by adding new members in the class. There are two waysdone by adding new members in the class. There are two ways in which these new members can be added to the class:in which these new members can be added to the class: •• Add new members in the originalAdd new members in the original •• Define a new class that has the new members and that alsoDefine a new class that has the new members and that also uses members of the existing "student" class. Using theuses members of the existing "student" class. Using the members of an existing class is the principle of inheritance.members of an existing class is the principle of inheritance. The new class is the derived class. The existing class serves asThe new class is the derived class. The existing class serves as the base class for the derived class.the base class for the derived class. Example ExplanationExample Explanation
  • 9. Driving a new class from the existing class reduces the size of theDriving a new class from the existing class reduces the size of the program. It also eliminates duplication of code within theprogram. It also eliminates duplication of code within the program. For example, let the name of the new class be marks.program. For example, let the name of the new class be marks. This class uses the members of the existing student class.This class uses the members of the existing student class. class marks : public student {class marks : public student { private:private: int s1,s2,s3, total;int s1,s2,s3, total; public:public: void inputmarks(void);void inputmarks(void); void show_detail(void); };void show_detail(void); }; Defining Derived ClassDefining Derived Class (cont…)(cont…)
  • 10.  The cIass marks is derived from the class student. The class marks is theThe cIass marks is derived from the class student. The class marks is the derived class. The class student is the base class.derived class. The class student is the base class.  The derived class has four data members of integer type and two memberThe derived class has four data members of integer type and two member functions.functions.  It also uses the code of the base class student.It also uses the code of the base class student.  The derived class cannot directly access the private data members the baseThe derived class cannot directly access the private data members the base class student by using the dot operator.class student by using the dot operator.  These members are only accessible to the derived class through theThese members are only accessible to the derived class through the interface functions within the base class.interface functions within the base class. Defining Derived ClassDefining Derived Class (cont..)(cont..)
  • 11.  There are three kinds of inheritance.There are three kinds of inheritance. •• Public • Private • ProtectedPublic • Private • Protected  Public InheritancePublic Inheritance In Public Inheritance, the public members of the base classIn Public Inheritance, the public members of the base class become the public members of the derived class.become the public members of the derived class. Thus the objects of the derived class can access publicThus the objects of the derived class can access public members (both data and functions) of the base class.members (both data and functions) of the base class. Similarly, the protected data members of the base class alsoSimilarly, the protected data members of the base class also become the protected members of derived class.become the protected members of derived class. Types of InheritanceTypes of Inheritance
  • 12. class sub_class_name : public base_class_name {class sub_class_name : public base_class_name { …………………………………… …………………………………… ) ;) ; wherewhere public: specifies the public inheritance.public: specifies the public inheritance. sub_class__name: represents name of the derived class.sub_class__name: represents name of the derived class. base_class_name: represents name of the base class.base_class_name: represents name of the base class. The general syntax for deriving a public classThe general syntax for deriving a public class from base class is:from base class is:
  • 13. Example: Public InheritanceExample: Public Inheritance #include<iostream>#include<iostream> using namespace std;using namespace std; class A {class A { private:private: int a1,a2;int a1,a2; protected:protected: int pa1,pa2;int pa1,pa2; public:public: void ppp (void){void ppp (void){ cout<<"Value of pal of classcout<<"Value of pal of class A="<<pa1<<endl;A="<<pa1<<endl; cout<<"Value of pa2 of classcout<<"Value of pa2 of class A="<<pa2<<endl; }};A="<<pa2<<endl; }}; class B : public A {class B : public A { public:public: void get(void){void get(void){ cout << "Enter value pal ? ";cout << "Enter value pal ? "; cin>>pa1;cin>>pa1; cout<<"Enter value pa2 ?";cout<<"Enter value pa2 ?"; cin>>pa2;}};cin>>pa2;}}; main (){main (){ B ob;B ob; ob.get();ob.get(); ob.ppp();ob.ppp(); };};
  • 14. In the above program the class B is publicly derived from classIn the above program the class B is publicly derived from class A. The object of the class BA. The object of the class B  Cannot access the private data member’s a1 & a2 of base classCannot access the private data member’s a1 & a2 of base class A.A.  Can access the public function member ppp of base class A.Can access the public function member ppp of base class A.  Can access the protected data members pa1 & pa2 of baseCan access the protected data members pa1 & pa2 of base class A.class A. Example ExplanationExample Explanation
  • 15. In private inheritance all the member in base class became theIn private inheritance all the member in base class became the private members od the derived class.private members od the derived class. In private Inheritance the objects of the derived class cannotIn private Inheritance the objects of the derived class cannot access any member of the base class directly. Its objects can onlyaccess any member of the base class directly. Its objects can only access the public members of the derived class directly.access the public members of the derived class directly. The general syntax for deriving private class from base class is:The general syntax for deriving private class from base class is: class sub_class_name : private base_class_name {class sub_class_name : private base_class_name { …………………………………………………………………….}.} Private: specifies private inheritance.Private: specifies private inheritance. sub_class_name: represents name of the derived class.sub_class_name: represents name of the derived class. base_class_name: represents name of the base class.base_class_name: represents name of the base class. Private InheritancePrivate Inheritance
  • 16. Example: Private InheritanceExample: Private Inheritance #include<iostream>#include<iostream> using namespace std;using namespace std; class A {class A { private:private: int a1,a2;int a1,a2; protected:protected: int pa1,pa2;int pa1,pa2; public:public: void ppp (void){void ppp (void){ cout<<"Value of pa1 of classcout<<"Value of pa1 of class A="<<pa1<<endl;A="<<pa1<<endl; cout<<"Value of pa2 of classcout<<"Value of pa2 of class A="<<pa2<<endl;}A="<<pa2<<endl;} };}; class B : private Aclass B : private A { public:{ public: void get(void)void get(void) {cout<<"Enter value of pa1 ";{cout<<"Enter value of pa1 "; cin>>pa1;cin>>pa1; cout<<"Enter value of pa2 =";cout<<"Enter value of pa2 ="; cin>>pa2;cin>>pa2; cout<<"Value of pa1 of classcout<<"Value of pa1 of class A="<<pa1<<endl;A="<<pa1<<endl; cout<<"Value of pa2 of classcout<<"Value of pa2 of class A="<<pa2<<endl;}};A="<<pa2<<endl;}}; main ( ){main ( ){ B ob;B ob; ob.get(); }ob.get(); }
  • 17. In the above program, the class B is derived as private for theIn the above program, the class B is derived as private for the base class A. The objects of class B:base class A. The objects of class B:  cannot access the private data members a1 & a2 of base classcannot access the private data members a1 & a2 of base class A.A.  cannot access the public function member ppp of base class A.cannot access the public function member ppp of base class A.  can only access the public members “get” of derived class Bcan only access the public members “get” of derived class B directly.directly. Example ExplanationExample Explanation
  • 18. In protected inheritance the public and protected members of theIn protected inheritance the public and protected members of the base class became the protected members of the derived classbase class became the protected members of the derived class whereas the private members of the base class remain private inwhereas the private members of the base class remain private in derived class.derived class. The object of the class that is derived as protected can accessThe object of the class that is derived as protected can access only the public members of the derived class directly.only the public members of the derived class directly. The general syntax for deriving a protected Class from base classThe general syntax for deriving a protected Class from base class is:is: class sub_class_name: protected base_class_name {class sub_class_name: protected base_class_name { ……………………………………};}; where protected: specifies protected inheritance.where protected: specifies protected inheritance. sub_class_name: represents name of the derived class.sub_class_name: represents name of the derived class. Base_class_name: represents name of the base class.Base_class_name: represents name of the base class. Protected InheritanceProtected Inheritance
  • 19. Example: Protected InheritanceExample: Protected Inheritance #include<iostream>#include<iostream> using namespace std;using namespace std; class Aclass A {{ private :private : int a1,a2;int a1,a2; protected:protected: int pa1,pa2;int pa1,pa2; public:public: void get(void){void get(void){ cout<<"value of pa1 of class A ";cout<<"value of pa1 of class A "; cout<<"value of pa2 of class A ?";}};cout<<"value of pa2 of class A ?";}}; class B: protected A{class B: protected A{ public:public: void get (void){void get (void){ cout<<" Enter value of pa1=";cout<<" Enter value of pa1="; cin>>pa1;cin>>pa1; cout<<"Value of pa2";cout<<"Value of pa2"; cin>>pa2;cin>>pa2; cout<<"Value of pa1 of classcout<<"Value of pa1 of class A="<<pa1<<endl;A="<<pa1<<endl; cout<<"Value of pa2 of classcout<<"Value of pa2 of class A="<<pa2<<endl;}};A="<<pa2<<endl;}}; main(){main(){ B ob;B ob; ob.get(); }ob.get(); }
  • 20. In the above program, thc class B is derived as protected from theIn the above program, thc class B is derived as protected from the base class A. The objects of class B:base class A. The objects of class B:  can only access the public members of derived class Acan only access the public members of derived class A directly.directly.  Difference between private and protected inheritanceDifference between private and protected inheritance  If you inherit a class in private mode the public members of the base class become private in the derived class. If the base class has some protected members they also become private. Now these members are not accessible by the functions of the classes which are derived from the child class.  Incase of protected inheritance, the public members of the base class become protected in the derived class. If the base class has got protected members they also become protected in the derived class. Now the main difference between protected and private inheritance is that the members inherited in protected mode are ready for further inheritance which is not the case for private inheritance.  Example ExplanationExample Explanation
  • 21.  In inheritance, a constructor of the derived class as well theIn inheritance, a constructor of the derived class as well the constructor functions of the base class are automaticallyconstructor functions of the base class are automatically executed when an object of the derived class is created.executed when an object of the derived class is created.  The following example explains the concept of constructorThe following example explains the concept of constructor functions in single inheritance.functions in single inheritance. Derived Class ConstructorsDerived Class Constructors
  • 22. Example: Derived Class ConstructorsExample: Derived Class Constructors #include<iostream>#include<iostream> using namespace std;using namespace std; class bb{class bb{ public:public: bb(void){bb(void){ cout<<"Constructor of basecout<<"Constructor of base class"<<endl;class"<<endl; }} };}; class dd:public bb{class dd:public bb{ public:public: dd(void){dd(void){ cout<<"Constructor of derivedcout<<"Constructor of derived class"<<endl;class"<<endl; }} };}; main(){main(){ dd abc;dd abc; }}
  • 23.  In the above program, when an object of the derived class ddIn the above program, when an object of the derived class dd is created, the constructors of both the derived class as well asis created, the constructors of both the derived class as well as the base class are executed.the base class are executed. Example ExplanationExample Explanation
  • 24.  In Multiple inheritances, a class is derived by using more thanIn Multiple inheritances, a class is derived by using more than one classes.one classes.  In multiple inheritance the derived class receives the membersIn multiple inheritance the derived class receives the members of two or more base classes.of two or more base classes.  Multiple inheritance is a powerful feature of Object OrientedMultiple inheritance is a powerful feature of Object Oriented Programming.Programming.  This technique reduces the program size and saveThis technique reduces the program size and save programmer's. time. The programmer uses the existing baseprogrammer's. time. The programmer uses the existing base classes in the program coding to solve problems.classes in the program coding to solve problems.  The figure shown below illustrates the relation of derived classThe figure shown below illustrates the relation of derived class with base classes.with base classes.    Multiple InheritanceMultiple Inheritance
  • 26. In the figure, Class C is derived from two base classes A & B. The syntax ofIn the figure, Class C is derived from two base classes A & B. The syntax of multiple inheritance is similar to that of single inheritance class. The name ofmultiple inheritance is similar to that of single inheritance class. The name of base classes are written separated by comma(,).base classes are written separated by comma(,). Syntax:Syntax: Class sub_class : sp1 b_class_1, sp2 b_class_2……..Class sub_class : sp1 b_class_1, sp2 b_class_2…….. {{ ……………………………………………….... };}; Where :Where : sp1 represents the access specifier of the first base class.sp1 represents the access specifier of the first base class. sp2 represents the access specifier of the second base class.sp2 represents the access specifier of the second base class.    Multiple InheritanceMultiple Inheritance
  • 27. Example: Multiple InheritanceExample: Multiple Inheritance #include<iostream>#include<iostream> using namespace std;using namespace std; class student {class student { private:private: char name[20],address[20];char name[20],address[20]; public:public: void input (void){void input (void){ cout<<"Enter name:";cin>>name;cout<<"Enter name:";cin>>name; cout<<"Enter address:";cin>>address;cout<<"Enter address:";cin>>address; }} void print(void){void print(void){ cout<<"Name:"<<name<<endl;cout<<"Name:"<<name<<endl; cout<<"Address:"<<address<<endl;cout<<"Address:"<<address<<endl; }} };}; class marks {class marks { private:private: int s1,s2,total;int s1,s2,total; public:public: void inputmarks(void){void inputmarks(void){ cout<<"Enter marks of S1=";cin>>s1;cout<<"Enter marks of S1=";cin>>s1; cout<<"Enter marks of S2=";cin>>s2;cout<<"Enter marks of S2=";cin>>s2; total=s1+s2;}total=s1+s2;} void showmarks(void){void showmarks(void){ cout<<"Marks in s1="<<s1<<endl;cout<<"Marks in s1="<<s1<<endl;
  • 28. Example: Multiple Inheritance (Cont..)Example: Multiple Inheritance (Cont..) cout<<"Marks incout<<"Marks in s2="<<s2<<endl;s2="<<s2<<endl; cout<<"Totalcout<<"Total Marks="<<total<<endl;Marks="<<total<<endl; }} };}; class show: public student, publicclass show: public student, public marks{marks{ public:public: void showrec(){void showrec(){ cout<<"Record is"<<endl;cout<<"Record is"<<endl; print();print(); showmarks();showmarks(); }} };}; main(){main(){ show m;show m; m.input();m.input(); m.inputmarks();m.inputmarks(); m.showrec();m.showrec(); }}
  • 29.  When a class is derived from more than one base classes, the constructor ofWhen a class is derived from more than one base classes, the constructor of the derived class as well as all of its base classes are executed when anthe derived class as well as all of its base classes are executed when an object of the derived class is created.object of the derived class is created.  If the constructor functions have no parameters then first the constructorIf the constructor functions have no parameters then first the constructor functions of the base class are executed and then the constructor functionsfunctions of the base class are executed and then the constructor functions of the derived class is executed.of the derived class is executed. Constructor in Multiple InheritanceConstructor in Multiple Inheritance
  • 30. Example: Constructor in Multiple InheritanceExample: Constructor in Multiple Inheritance #include<iostream>#include<iostream> using namespace std;using namespace std; class aa{class aa{ public:public: aa(){aa(){ cout<<"Class aa"<<endl;cout<<"Class aa"<<endl; }} };}; class bb{class bb{ public:public: bb(){bb(){ cout<<"Class bb"<<endl;cout<<"Class bb"<<endl; };}; class cc:public aa,public bb{class cc:public aa,public bb{ public:public: cc(){cc(){ cout<<"Class cc"<<endl;cout<<"Class cc"<<endl; }} };}; main(){main(){ cc o;cc o; }}
  • 31.  In the above program, three classes are created.In the above program, three classes are created.  The cc class is publicly derived from two class aa and bb.The cc class is publicly derived from two class aa and bb.  All the classes have constructor functions are executed in theAll the classes have constructor functions are executed in the following sequence.following sequence. 1.1. Constructor of class aa is executed first.Constructor of class aa is executed first. 2.2. Constructor of class bb is executed second.Constructor of class bb is executed second. 3.3. Constructor of class cc is executed last.Constructor of class cc is executed last. Example ExplanationExample Explanation
  • 32.  Constructor in Single Inheritance with argumentsConstructor in Single Inheritance with arguments  Constructor in Multiple Inheritance with argumentsConstructor in Multiple Inheritance with arguments AssignmentAssignment