34
Object-Oriented Object-Oriented Programming (OOP) Programming (OOP) Lecture No. 22 Lecture No. 22

Oop lecture22

Embed Size (px)

Citation preview

Page 1: Oop lecture22

Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)

Lecture No. 22Lecture No. 22

Page 2: Oop lecture22

Inheritance in ClassesInheritance in Classes► If a class B inherits from class A, then B If a class B inherits from class A, then B

contains all the characteristics contains all the characteristics (information structure and behavior) of (information structure and behavior) of class Aclass A

► The parent class is called The parent class is called basebase class and class and the child class is called the child class is called derivedderived class class

► Besides inherited characteristics, derived Besides inherited characteristics, derived class may have its own unique class may have its own unique characteristicscharacteristics

Page 3: Oop lecture22

UML NotationUML Notation

Parent Class

Child Class

Base Class

Derived Class

Page 4: Oop lecture22

Inheritance in C++Inheritance in C++

►There are three types of inheritance in There are three types of inheritance in C++C++

PublicPublic PrivatePrivate ProtectedProtected

Page 5: Oop lecture22

““IS A” RelationshipIS A” Relationship

► IS A relationship is modeled with the help IS A relationship is modeled with the help of public inheritanceof public inheritance

► SyntaxSyntaxclass ChildClassclass ChildClass

: public BaseClass{: public BaseClass{......};};

Page 6: Oop lecture22

ExampleExampleclass Person{class Person{

......

};};

class Student: public Person{class Student: public Person{

......

};};

Page 7: Oop lecture22

Accessing MembersAccessing Members►Public members of base class Public members of base class

become public member of derived become public member of derived classclass

►Private members of base class are Private members of base class are not accessible from outside of base not accessible from outside of base class, even in the derived class class, even in the derived class (Information Hiding)(Information Hiding)

Page 8: Oop lecture22

ExampleExampleclass Person{class Person{char *name;char *name;int age;int age;......

public:public:const char *GetName() const;const char *GetName() const;int GetAge() const;int GetAge() const;......

};};

Page 9: Oop lecture22

class Student: public Person{class Student: public Person{int semester;int semester;int rollNo;int rollNo;......

public:public:int GetSemester() const;int GetSemester() const;int GetRollNo() const;int GetRollNo() const;void Print() const;void Print() const;......

};};

ExampleExample

Page 10: Oop lecture22

ExampleExample

void Student::Print()void Student::Print()

{{

cout << cout << namename << “ is in” << “ << “ is in” << “ semester ” << semester;semester ” << semester;

}}

ERROR

Page 11: Oop lecture22

ExampleExample

void Student::Print()void Student::Print()

{{

cout << cout << GetName()GetName()

<< “ is in semester ” << “ is in semester ” << semester;<< semester;

}}

Page 12: Oop lecture22

ExampleExampleint main(){int main(){Student stdt;Student stdt;

stdt.semester = 0;//errorstdt.semester = 0;//errorstdt.namestdt.name = NULL; //error = NULL; //errorcout << stdt.GetSemester();cout << stdt.GetSemester();cout << cout << stdt.GetName();stdt.GetName();return 0;return 0;

}}

Page 13: Oop lecture22

Allocation in MemoryAllocation in Memory►The object of derived class is The object of derived class is

represented in memory as followsrepresented in memory as follows

Data members of base class

Data members of derived class

base member1base member2

...

derived member1derived member2

...

Page 14: Oop lecture22

Allocation in MemoryAllocation in Memory►Every object of derived class has an Every object of derived class has an

anonymous object of base classanonymous object of base class

Page 15: Oop lecture22

ConstructorsConstructors►The anonymous object of base class The anonymous object of base class

must be initialized using constructor must be initialized using constructor of base classof base class

►When a derived class object is When a derived class object is created the constructor of base class created the constructor of base class is executed before the constructor of is executed before the constructor of derived classderived class

Page 16: Oop lecture22

ConstructorsConstructors

Base class constructor initializes the anonymous object

Derived class constructor initializes the derived class object

base member1base member2

...

derived member1derived member2

...

Page 17: Oop lecture22

class Parent{class Parent{public:public:Parent(){ cout << Parent(){ cout <<

““Parent Constructor...”;}Parent Constructor...”;}};};class Child : public Parent{class Child : public Parent{public:public:Child(){Child(){ cout << cout <<

““Child Constructor...”;}Child Constructor...”;}};};

ExampleExample

Page 18: Oop lecture22

ExampleExampleint main(){int main(){

Child cobj;Child cobj;

return 0;return 0;

}}

Output:Parent Constructor...Child Constructor...

Page 19: Oop lecture22

ConstructorConstructor► If default constructor of base class If default constructor of base class

does not exist then the compiler will does not exist then the compiler will try to generate a default constructor try to generate a default constructor for base class and execute it before for base class and execute it before executing constructor of derived executing constructor of derived classclass

Page 20: Oop lecture22

ConstructorConstructor► If the user has given only an If the user has given only an

overloaded constructor for base overloaded constructor for base class, the compiler will not generate class, the compiler will not generate default constructor for base classdefault constructor for base class

Page 21: Oop lecture22

class Parent{class Parent{

public:public:

Parent(int i){}Parent(int i){}

};};

class Child : public Parent{class Child : public Parent{

public:public:

Child(){}Child(){}

} } Child_Object;Child_Object; //ERROR //ERROR

ExampleExample

Page 22: Oop lecture22

Base Class InitializerBase Class Initializer

►C++ has provided a mechanism to C++ has provided a mechanism to explicitly call a constructor of base explicitly call a constructor of base class from derived classclass from derived class

►The syntax is similar to member The syntax is similar to member initializer and is referred as base-initializer and is referred as base-class initializationclass initialization

Page 23: Oop lecture22

class Parent{class Parent{public:public:Parent(int i){…};Parent(int i){…};

};};class Child : public Parent{class Child : public Parent{public:public:Child(int i): Child(int i): Parent(i)Parent(i){…}{…}

};};

ExampleExample

Page 24: Oop lecture22

class Parent{class Parent{public:public:

Parent(){cout << Parent(){cout << ““Parent Constructor...”;}Parent Constructor...”;}

......};};class Child : public Parent{class Child : public Parent{public:public:

Child():Child():Parent()Parent(){cout << “Child Constructor...”;}{cout << “Child Constructor...”;}......

};};

ExampleExample

Page 25: Oop lecture22

Base Class InitializerBase Class Initializer►User can provide base class initializer User can provide base class initializer

and member initializer and member initializer simultaneouslysimultaneously

Page 26: Oop lecture22

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

Page 27: Oop lecture22

Base Class InitializerBase Class Initializer

►The base class initializer can be The base class initializer can be written after member initializer for written after member initializer for derived classderived class

►The base class constructor is The base class constructor is executed before the initialization of executed before the initialization of data members of derived class.data members of derived class.

Page 28: Oop lecture22

Initializing MembersInitializing Members►Derived class can only initialize Derived class can only initialize

members of base class using members of base class using overloaded constructorsoverloaded constructors Derived class can not initialize the public Derived class can not initialize the public

data member of base class using data member of base class using member initialization listmember initialization list

Page 29: Oop lecture22

ExampleExampleclass Person{class Person{publicpublic::int age;int age;char *name;char *name;......

public:public:Person();Person();

};};

Page 30: Oop lecture22

ExampleExampleclass Student: public Person{class Student: public Person{private:private:int semester;int semester;

......public:public:Student(int a):Student(int a):age(a)age(a){{ //error //error}}

};};

Page 31: Oop lecture22

ReasonReason

► It will be an assignment not an It will be an assignment not an initializationinitialization

Page 32: Oop lecture22

DestructorsDestructors

►Destructors are called in reverse order Destructors are called in reverse order of constructor calledof constructor called

►Derived class destructor is called Derived class destructor is called before the base class destructor is before the base class destructor is calledcalled

Page 33: Oop lecture22

class Parent{class Parent{public:public:

Parent(){cout <<“Parent Constructor”;}Parent(){cout <<“Parent Constructor”;}~Parent(){cout<<“Parent Destructor”;}~Parent(){cout<<“Parent Destructor”;}

};};

class Child : public Parent{class Child : public Parent{public:public:

Child(){cout << “Child Constructor”;}Child(){cout << “Child Constructor”;}~Child(){cout << “Child Destructo”;}~Child(){cout << “Child Destructo”;}

};};

ExampleExample

Page 34: Oop lecture22

ExampleExampleOutput:Output:

Parent ConstructorParent Constructor

Child ConstructorChild Constructor

Child Destructor Child Destructor

Parent DestructorParent Destructor