Upload
ecomputernotes
View
223
Download
0
Embed Size (px)
Citation preview
8/3/2019 Computer Notes - Inheritance in Class
1/34
InheritanceInheritance
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
2/34
Inheritance in ClassesInheritance in Classes If a class B inherits from class A, then B contains allIf a class B inherits from class A, then B contains all
the characteristics (information structure andthe characteristics (information structure andbehavior) of class Abehavior) of class A
The parent class is calledThe parent class is called basebase class and the childclass and the childclass is calledclass is called derivedderivedclassclass
Besides inherited characteristics, derived class mayBesides inherited characteristics, derived class may
have its own unique characteristicshave its own unique characteristics
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
3/34
UML NotationUML Notation
Parent Class
Child Class
Base Class
Derived Class
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
4/34
Inheritance in C++Inheritance in C++
There are three types of inheritance in C++There are three types of inheritance in C++
PublicPublic
PrivatePrivate
ProtectedProtected
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
5/34
IS AIS ARelationshipRelationship
IS A relationship is modeled with the help of publicIS A relationship is modeled with the help of publicinheritanceinheritance
SyntaxSyntax
classclass ChildClassChildClass
: public: public BaseClassBaseClass{{
......
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
6/34
ExampleExample
class Person{class Person{
......
};};
class Student: public Person{class Student: public Person{
......};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
7/34
Accessing MembersAccessing Members
Public members of base class become publicPublic members of base class become public
member of derived classmember of derived class
Private members of base class are notPrivate members of base class are notaccessible from outside of base class, even inaccessible from outside of base class, even in
the derived class (Information Hiding)the derived class (Information Hiding)
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
8/34
ExampleExampleclass Person{class Person{
char *name;char *name;
int age;int age;
......
public:public:
const char *const char *GetNameGetName() const;() const;intint GetAgeGetAge() const;() const;
......
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
9/34
class Student: public Person{class Student: public Person{int semester;int semester;intint rollNorollNo;;
......public:public:
intint GetSemesterGetSemester() const;() const;
intint GetRollNoGetRollNo() const;() const;void Print() const;void Print() const;......
};};
ExampleExample
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
10/34
ExampleExample
voidvoidStudent::PrintStudent::Print()(){{
coutcout
8/3/2019 Computer Notes - Inheritance in Class
11/34
ExampleExample
voidvoidStudent::PrintStudent::Print()()
{{
coutcout
8/3/2019 Computer Notes - Inheritance in Class
12/34
ExampleExampleint main(){int main(){
StudentStudent stdtstdt;;
stdt.semesterstdt.semester = 0;//error= 0;//error
stdt.namestdt.name = NULL; //error= NULL; //error
coutcout
8/3/2019 Computer Notes - Inheritance in Class
13/34
Allocation in MemoryAllocation in Memory
The object of derived class is represented inThe object of derived class is represented in
memory as followsmemory as follows
Data members ofbase class
Data members ofderived class
base member1
base member2...
derived member1derived member2
...
8/3/2019 Computer Notes - Inheritance in Class
14/34
Allocation in MemoryAllocation in Memory
Every object of derived class has anEvery object of derived class has an
anonymous object of base classanonymous object of base class
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
15/34
ConstructorsConstructors
The anonymous object of base class must beThe anonymous object of base class must be
initialized using constructor of base classinitialized using constructor of base class
When a derived class object is created theWhen a derived class object is created theconstructor of base class is executed beforeconstructor of base class is executed before
the constructor of derived classthe constructor of derived class
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
16/34
ConstructorsConstructors
Base class constructor
initializes the anonymous
object
Derived class constructor
initializes the derived class
object
base member1
base member2
...
derived member1derived member2
...
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
17/34
class Parent{class Parent{public:public:
Parent(){Parent(){ coutcout
8/3/2019 Computer Notes - Inheritance in Class
18/34
ExampleExample
intintmain(){main(){
ChildChildcobjcobj;;
return 0;return 0;}}
Output:
Parent Constructor...Child Constructor...
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
19/34
ConstructorConstructor
If default constructor of base class does notIf default constructor of base class does not
exist then the compiler will try to generate aexist then the compiler will try to generate a
default constructor for base class and executedefault constructor for base class and executeit before executing constructor of derivedit before executing constructor of derived
classclass
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
20/34
ConstructorConstructor
If the user has given only an overloadedIf the user has given only an overloaded
constructor for base class, the compiler willconstructor for base class, the compiler will
not generate default constructor for base classnot generate default constructor for base class
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
21/34
class Parent{class Parent{
public:public:
Parent(intParent(int i){}i){}};};
class Child : public Parent{class Child : public Parent{
public:public:Child(){}Child(){}
}} Child_ObjectChild_Object;; //ERROR//ERROR
ExampleExample
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
22/34
Base Class InitializerBase Class Initializer
C++ has provided a mechanism to explicitlyC++ has provided a mechanism to explicitly
call a constructor of base class from derivedcall a constructor of base class from derived
classclass
The syntax is similar to member initializer andThe syntax is similar to member initializer andis referred as baseis referred as base--class initializationclass initialization
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
23/34
class Parent{class Parent{
public:public:
Parent(intParent(int i){i){};};
};};
class Child : public Parent{class Child : public Parent{
public:public:
Child(intChild(int i):i): Parent(iParent(i))
{{}}
};};
ExampleExample
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
24/34
class Parent{class Parent{public:public:
Parent(){coutParent(){cout
8/3/2019 Computer Notes - Inheritance in Class
25/34
Base Class InitializerBase Class Initializer
User can provide base class initializer andUser can provide base class initializer and
member initializer simultaneouslymember initializer simultaneously
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
26/34
class Parent{class Parent{public:public:
Parent(){Parent(){}}
};};class Child : public Parent{class Child : public Parent{int member;int member;
public:public:Child():member(0),Child():member(0), Parent()Parent(){{}}
};};
ExampleExample
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
27/34
Base Class InitializerBase Class Initializer
The base class initializer can be written afterThe base class initializer can be written after
member initializer for derived classmember initializer for derived class
The base class constructor is executed beforeThe base class constructor is executed beforethe initialization of data members of derivedthe initialization of data members of derived
class.class.
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
28/34
Initializing MembersInitializing Members
Derived class can only initialize members ofDerived class can only initialize members of
base class using overloaded constructorsbase class using overloaded constructors
Derived class can not initialize the public dataDerived class can not initialize the public datamember of base class using member initializationmember of base class using member initialization
listlist
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
29/34
ExampleExampleclass Person{class Person{
publicpublic::intint age;age;
char *name;char *name;......
public:public:
Person();Person();};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
30/34
ExampleExampleclass Student: public Person{class Student: public Person{
private:private:
int semester;int semester;
......
public:public:
Student(intStudent(int a):a):age(aage(a))
{{ //error//error
}}
};};
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
31/34
ReasonReasonIt will be an assignment not an initializationIt will be an assignment not an initialization
8/3/2019 Computer Notes - Inheritance in Class
32/34
DestructorsDestructors
Destructors are called in reverse order ofDestructors are called in reverse order of
constructor calledconstructor called
Derived class destructor is called before theDerived class destructor is called before the
base class destructor is calledbase class destructor is called
http://ecomputernotes.com
8/3/2019 Computer Notes - Inheritance in Class
33/34
class Parent{class Parent{public:public:
Parent(){coutParent(){cout
8/3/2019 Computer Notes - Inheritance in Class
34/34
ExampleExample
Output:Output:
Parent ConstructorParent ConstructorChild ConstructorChild Constructor
Child DestructorChild DestructorParent DestructorParent Destructor
http://ecomputernotes.com