Oop lecture22
-
Upload
university-of-central-punjab -
Category
Education
-
view
148 -
download
1
Transcript of Oop lecture22
Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)
Lecture No. 22Lecture No. 22
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
UML NotationUML Notation
Parent Class
Child Class
Base Class
Derived Class
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
““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{......};};
ExampleExampleclass Person{class Person{
......
};};
class Student: public Person{class Student: public Person{
......
};};
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)
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;......
};};
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
ExampleExample
void Student::Print()void Student::Print()
{{
cout << cout << namename << “ is in” << “ << “ is in” << “ semester ” << semester;semester ” << semester;
}}
ERROR
ExampleExample
void Student::Print()void Student::Print()
{{
cout << cout << GetName()GetName()
<< “ is in semester ” << “ is in semester ” << semester;<< semester;
}}
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;
}}
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
...
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
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
ConstructorsConstructors
Base class constructor initializes the anonymous object
Derived class constructor initializes the derived class object
base member1base member2
...
derived member1derived member2
...
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
ExampleExampleint main(){int main(){
Child cobj;Child cobj;
return 0;return 0;
}}
Output:Parent Constructor...Child Constructor...
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
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
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
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
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
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
Base Class InitializerBase Class Initializer►User can provide base class initializer User can provide base class initializer
and member initializer and member initializer simultaneouslysimultaneously
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
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.
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
ExampleExampleclass Person{class Person{publicpublic::int age;int age;char *name;char *name;......
public:public:Person();Person();
};};
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}}
};};
ReasonReason
► It will be an assignment not an It will be an assignment not an initializationinitialization
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
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
ExampleExampleOutput:Output:
Parent ConstructorParent Constructor
Child ConstructorChild Constructor
Child Destructor Child Destructor
Parent DestructorParent Destructor