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

What's hot

java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
Arjun Shanka
 

What's hot (20)

Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Learn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & InheritanceLearn C# Programming - Classes & Inheritance
Learn C# Programming - Classes & Inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
 
Unit 3 Java
Unit 3 JavaUnit 3 Java
Unit 3 Java
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
E4
E4E4
E4
 
Inheritance in oops
Inheritance in oopsInheritance in oops
Inheritance in oops
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
OOP Concepets and UML Class Diagrams
OOP Concepets and UML Class DiagramsOOP Concepets and UML Class Diagrams
OOP Concepets and UML Class Diagrams
 
OOP Inheritance
OOP InheritanceOOP Inheritance
OOP Inheritance
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
 
Inheritance used in java
Inheritance used in javaInheritance used in java
Inheritance used in java
 
inheritance
inheritanceinheritance
inheritance
 
Principles of Object Oriented Programming
Principles of Object Oriented ProgrammingPrinciples of Object Oriented Programming
Principles of Object Oriented Programming
 

Viewers also liked

My personal development plan
My personal development planMy personal development plan
My personal development plan
07naeemahmed
 
My personal development plan
My personal development planMy personal development plan
My personal development plan
karinairina
 
Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015
Zeal Liew
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
Princess Sam
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
Tech_MX
 

Viewers also liked (20)

Week 22 virtual class sport_26.9.2013
Week 22 virtual class sport_26.9.2013Week 22 virtual class sport_26.9.2013
Week 22 virtual class sport_26.9.2013
 
11 constructors in derived classes
11 constructors in derived classes11 constructors in derived classes
11 constructors in derived classes
 
My personal development plan
My personal development planMy personal development plan
My personal development plan
 
Lecture18
Lecture18Lecture18
Lecture18
 
Personal development plan, realising your potential
Personal development plan, realising your potentialPersonal development plan, realising your potential
Personal development plan, realising your potential
 
My personal development plan
My personal development planMy personal development plan
My personal development plan
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015Personal Development Plan 2014 - 2015
Personal Development Plan 2014 - 2015
 
Uid
UidUid
Uid
 
virtual classroom for college major project for computer science.
virtual classroom for college major project for computer science.virtual classroom for college major project for computer science.
virtual classroom for college major project for computer science.
 
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
Lecture 7, c++(complete reference,herbet sheidt)chapter-17.
 
Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++Abstract Base Class and Polymorphism in C++
Abstract Base Class and Polymorphism in C++
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Lec 42.43 - virtual.functions
Lec 42.43 - virtual.functionsLec 42.43 - virtual.functions
Lec 42.43 - virtual.functions
 
Virtual base class
Virtual base classVirtual base class
Virtual base class
 
Personality Development
Personality DevelopmentPersonality Development
Personality Development
 
Personality development presentation
Personality development presentationPersonality development presentation
Personality development presentation
 
Personality
PersonalityPersonality
Personality
 
Presentation On Personality
Presentation On PersonalityPresentation On Personality
Presentation On Personality
 
My Personal Development Plan
My Personal Development PlanMy Personal Development Plan
My Personal Development Plan
 

Similar to OOPs Lecture 2

9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
Terry Yoast
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
sundas65
 

Similar to OOPs Lecture 2 (20)

11 Inheritance.ppt
11 Inheritance.ppt11 Inheritance.ppt
11 Inheritance.ppt
 
Inheritance in C++
Inheritance in C++Inheritance in C++
Inheritance in C++
 
Inheritance
InheritanceInheritance
Inheritance
 
4 Classes & Objects
4 Classes & Objects4 Classes & Objects
4 Classes & Objects
 
Oops
OopsOops
Oops
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Class properties
Class propertiesClass properties
Class properties
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
Nested class in java
Nested class in javaNested class in java
Nested class in java
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
lecture 6.pdf
lecture 6.pdflecture 6.pdf
lecture 6.pdf
 
9781111530532 ppt ch10
9781111530532 ppt ch109781111530532 ppt ch10
9781111530532 ppt ch10
 
Inheritance
Inheritance Inheritance
Inheritance
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1Intro To C++ - Class #22: Inheritance, Part 1
Intro To C++ - Class #22: Inheritance, Part 1
 
Inheritance
InheritanceInheritance
Inheritance
 

Recently uploaded

Recently uploaded (20)

Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
GraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysisGraphAware - Transforming policing with graph-based intelligence analysis
GraphAware - Transforming policing with graph-based intelligence analysis
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 

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