Object-Oriented Programming (OOP) Lecture No.
22
Inheritance in Classes
If
a class B inherits from class A, then B contains all the characteristics (information structure and behavior) of class A The parent class is called base class and the child class is called derived class Besides inherited characteristics, derived class may have its own unique characteristics
UML Notation
Parent Class Base Class
Child Class
Derived Class
Inheritance in C++
There
are three types of inheritance in C++
Public Private Protected
IS A Relationship
IS
A relationship is modeled with the help of public inheritance
Syntax
class ChildClass : public BaseClass{ ... };
Example
class Person{ ... }; class Student: public Person{ ... };
Accessing Members
Public
members of base class become public member of derived class Private members of base class are not accessible from outside of base class, even in the derived class (Information Hiding)
Example
class Person{ char *name; int age; ... public: const char *GetName() const; int GetAge() const; ... };
Example
class Student: public Person{ int semester; int rollNo; ... public: int GetSemester() const; int GetRollNo() const; void Print() const; ... };
Example
void Student::Print() ERROR { cout << name << is in << semester << semester; }
Example
void Student::Print() { cout << GetName() << is in semester << semester; }
Example
int main(){ Student stdt; stdt.semester = 0;//error stdt.name = NULL; //error cout << stdt.GetSemester(); cout << stdt.GetName(); return 0; }
Allocation in Memory
The
object of derived class is represented in memory as follows
base member1 base member2 ...
Data members of base class
derived member1 derived member2 ...
Data members of derived class
Allocation in Memory
Every
object of derived class has an anonymous object of base class
Constructors
The
anonymous object of base class must be initialized using constructor of base class When a derived class object is created the constructor of base class is executed before the constructor of derived class
Constructors
base member1 base member2 ... derived member1 derived member2 ... Base class constructor initializes the anonymous object
Derived class constructor initializes the derived class object
Example
class Parent{ public: Parent(){ cout << Parent Constructor...;} }; class Child : public Parent{ public: Child(){ cout << Child Constructor...;} };
Example
int main(){ Child cobj; return 0; }
Output: Parent Constructor... Child Constructor...
Constructor
If
default constructor of base class does not exist then the compiler will try to generate a default constructor for base class and execute it before executing constructor of derived class
Constructor
If
the user has given only an overloaded constructor for base class, the compiler will not generate default constructor for base class
Example
class Parent{ public: Parent(int i){} }; class Child : public Parent{ public: Child(){} } Child_Object; //ERROR
Base Class Initializer
C++
has provided a mechanism to explicitly call a constructor of base class from derived class syntax is similar to member initializer and is referred as base-class initialization
The
Example
class Parent{ public: Parent(int i){}; }; class Child : public Parent{ public: Child(int i): Parent(i) {} };
Example
class Parent{ public: Parent(){cout << Parent Constructor...;} ... }; class Child : public Parent{ public: Child():Parent() {cout << Child Constructor...;} ... };
Base Class Initializer
User
can provide base class initializer and member initializer simultaneously
Example
class Parent{ public: Parent(){} }; class Child : public Parent{ int member; public: Child():member(0), Parent() {} };
Base Class Initializer
The
base class initializer can be written after member initializer for derived class The base class constructor is executed before the initialization of data members of derived class.
Initializing Members
Derived
class can only initialize members of base class using overloaded constructors
Derived class can not initialize the public data member of base class using member initialization list
Example
class Person{ public: int age; char *name; ... public: Person(); };
Example
class Student: public Person{ private: int semester; ... public: Student(int a):age(a) { //error } };
Reason
It
will be an assignment not an initialization
Destructors
Destructors
are called in reverse order of constructor called Derived class destructor is called before the base class destructor is called
Example
class Parent{ public: Parent(){cout <<Parent Constructor;} ~Parent(){cout<<Parent Destructor;} }; class Child : public Parent{ public: Child(){cout << Child Constructor;} ~Child(){cout << Child Destructo;} };
Example
Output:
Parent Constructor Child Constructor Child Destructor Parent Destructor