Download - Computer Notes - Private Inheritance
-
8/3/2019 Computer Notes - Private Inheritance
1/29
Private Inheritance
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
2/29
Class Collectionclass Collection {
...public:
void AddElement(int);bool SearchElement(int);
bool SearchElementAgain(int);bool DeleteElement(int);
};http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
3/29
Class Setclass Set: private Collection {
private:...
public:void AddMember(int);
bool IsMember(int);bool DeleteMember(int);
};http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
4/29
Specialization (Restriction) the derived class is behaviourally
incompatible with the base class
Behaviourally incompatible meansthat base class cant always be
replaced by the derived class
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
5/29
Specialization (Restriction) Specialization (Restriction) can be
implemented using private andprotected inheritance
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
6/29
Example Specialization
(Restriction)Person
age : [0..125]
Adult
age : [18..125]
setAge( a )
setAge( a )
age = a
If age < 18 then
errorelse
age = a
-
8/3/2019 Computer Notes - Private Inheritance
7/29
Exampleclass Person{
protected:int age;
public:
bool SetAge(int _age){if (_age >=0 && _age
-
8/3/2019 Computer Notes - Private Inheritance
8/29
Exampleclass Adult : private Person {
public:bool SetAge(int _age){
if (_age >=18 && _age
-
8/3/2019 Computer Notes - Private Inheritance
9/29
Private Inheritance Only member functions and friend
functions of a derived class canconvert pointer or reference of
derived object to that of parent object
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
10/29
Exampleclass Parent{};
class Child : private Parent{};
int main(){
Child cobj;
Parent *pptr = & cobj; //Error
return 0;
}
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
11/29
Examplevoid DoSomething(const Parent &);
Child::Child(){
Parent & pPtr =static_cast(*this);
DoSomething(pPtr);
// DoSomething(*this);
}http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
12/29
Private Inheritance The child class object has an
anonymous object of parent classobject
The default constructor and copyconstructor of parent class are called
when needed
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
13/29
Exampleclass Parent{public:
Parent(){cout
-
8/3/2019 Computer Notes - Private Inheritance
14/29
Exampleclass Child: private Parent{public:
Child(){cout
-
8/3/2019 Computer Notes - Private Inheritance
15/29
Exampleint main() {
Child cobj1;Child cobj2 = cobj1;
//Child cobj2(cobj1);return 0;
}
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
16/29
Example Output:
Parent Constructor
Child ConstructorParent Copy Constructor
Child Copy Constructor
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
17/29
Private Inheritance The base class that is more then one
level down the hierarchy cannotaccess the member function of
parent class, if we are using private
inheritance
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
18/29
Class Hierarchyclass GrandParent{public :
void DoSomething();};
class Parent: private GrandParent{void SomeFunction(){
DoSomething();
}
};
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
19/29
Exampleclass Child: private Parent
{public:
Child() {DoSomething(); //Error
}
};http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
20/29
Private Inheritance The base class that is more then one
level down the hierarchy cannotconvert the pointer or reference to
child object to that of parent, if we
are using private inheritance
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
21/29
Class Hierarchyvoid DoSomething(GrandParent&);
class GrandParent{};
class Parent: private GrandParent{public:
Parent() {DoSomething(*this);}
};http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
22/29
Exampleclass Child: private Parent {
public:Child()
{DoSomething(*this); //Error
}
};http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
23/29
Protected Inheritance Use protected inheritance if you want
to build class hierarchy usingimplemented in terms of
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
24/29
Protected Inheritance If B is a protected base and D is
derived class then public andprotected members of B can be used
by member functions and friends of
classes derived from D
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
25/29
Class Hierarchyclass GrandParent{public :
void DoSomething();};
class Parent: protected GrandParent{void SomeFunction(){
DoSomething();
}
};
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
26/29
Exampleclass Child: protected Parent
{public:
Child()
{
DoSomething();
}
}; http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
27/29
Protected Inheritance If B is a protected base and D is
derived class then only friends andmembers of D and friends and
members of class derived from D
can convert D* to B* or D& to B&
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
28/29
Class Hierarchyvoid DoSomething(GrandParent&);
class GrandParent{
};
class Parent: protected GrandParent{};
http://ecomputernotes.com
-
8/3/2019 Computer Notes - Private Inheritance
29/29
Exampleclass Child: protected Parent {
public:Child()
{DoSomething(*this);
}
};