object oriented programming (oop) - cs304 power point slides lecture 22
TRANSCRIPT
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
1/34
Object-Oriented Programming
(OOP)Lecture No. 22
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
2/34
Inheritance in Classes If a class B inherits from class A, then B contains
all the characteristics (information structure andbehavior) of class A
The parent class is called baseclass and thechild class is called derivedclass
Besides inherited characteristics, derived classmay have its own unique characteristics
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
3/34
UML Notation
Parent Class
Child Class
Base Class
Derived Class
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
4/34
Inheritance in C++
There are three types of inheritance in C++
Public
Private
Protected
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
5/34
IS A Relationship
IS A relationship is modeled with the help ofpublic inheritance
Syntax
class ChildClass
: public BaseClass{
...
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
6/34
Example
class Person{
...
};
class Student: public Person{
...
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
7/34
Accessing Members
Public members of base class become
public member of derived class
Private members of base class are notaccessible from outside of base class,
even in the derived class (InformationHiding)
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
8/34
Exampleclass Person{
char *name;int age;
...
public:const char *GetName() const;
int GetAge() const;
...
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
9/34
class Student: public Person{
int semester;int rollNo;...
public:
int GetSemester() const;int GetRollNo() const;void Print() const;...
};
Example
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
10/34
Example
void Student::Print()
{
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
11/34
Example
void Student::Print()
{
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
12/34
Exampleint main(){
Student stdt;
stdt.semester = 0;//error
stdt.name= NULL; //error
cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
13/34
Allocation in Memory
The object of derived class is represented
in memory as follows
Data members of
base class
Data members of
derived class
base member1base member2
...
derived member1derived member2
...
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
14/34
Allocation in Memory
Every object of derived class has an
anonymous object of base class
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
15/34
Constructors
The anonymous object of base class must
be initialized using constructor of baseclass
When a derived class object is created the
constructor of base class is executedbefore the constructor of derived class
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
16/34
Constructors
Base class constructor
initializes the anonymous
objectDerived class constructor
initializes the derived
class object
base member1
base member2
...derived member1
derived member2
...
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
17/34
class Parent{
public:Parent(){ cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
18/34
Example
int main(){
Child cobj;
return 0;
}
Output:
Parent Constructor...
Child Constructor...
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
19/34
Constructor
If default constructor of base class does
not exist then the compiler will try togenerate a default constructor for baseclass and execute it before executing
constructor of derived class
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
20/34
Constructor
If the user has given only an overloaded
constructor for base class, the compilerwill not generate default constructor forbase class
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
21/34
class Parent{
public:Parent(int i){}
};
class Child : public Parent{public:
Child(){}
} Child_Object;//ERROR
Example
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
22/34
Base Class Initializer
C++ has provided a mechanism toexplicitly call a constructor of base classfrom derived class
The syntax is similar to member initializerand is referred as base-class initialization
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
23/34
class Parent{
public:Parent(int i){};
};
class Child : public Parent{
public:
Child(int i): Parent(i)
{}
};
Example
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
24/34
class Parent{public:
Parent(){cout
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
25/34
Base Class Initializer
User can provide base class initializer and
member initializer simultaneously
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
26/34
class Parent{
public:Parent(){}
};class Child : public Parent{
int member;public:
Child():member(0), Parent(){}
};
Example
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
27/34
Base Class Initializer
The base class initializer can be writtenafter member initializer for derived class
The base class constructor is executedbefore the initialization of data members
of derived class.
l b
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
28/34
Initializing Members
Derived class can only initialize members
of base class using overloadedconstructors
Derived class can not initialize the public data
member of base class using memberinitialization list
l
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
29/34
Exampleclass Person{
public:int age;char *name;...
public:Person();
};
E l
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
30/34
Exampleclass Student: public Person{
private:int semester;
...
public:
Student(int a):age(a)
{ //error
}
};
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
31/34
Reason
It will be an assignment not an initialization
D t t
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
32/34
Destructors
Destructors are called in reverse order ofconstructor called
Derived class destructor is called before thebase class destructor is called
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
33/34
E l
-
8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
34/34
Example
Output:
Parent Constructor
Child Constructor
Child Destructor
Parent Destructor