Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 22
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
IS A Relationship
IS
Syntax
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
Allocation in Memory
Every
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; }
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
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...;} ... };
Example
class Parent{ public: Parent(){} }; class Child : public Parent{ int member; public: Child():member(0), Parent() {} };
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
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: