Computer Notes - Private Inheritance - PowerPoint PPT Presentation

About This Presentation
Title:

Computer Notes - Private Inheritance

Description:

- Computer Notes - Private Inheritance in Object oriented Programming what is Private Inheritance Explain about it in detail .explain it with example – PowerPoint PPT presentation

Number of Views:251

less

Transcript and Presenter's Notes

Title: Computer Notes - Private Inheritance


1
Private Inheritance
http//ecomputernotes.com
2
Class Collection
  • class Collection
  • ...
  • public
  • void AddElement(int)
  • bool SearchElement(int)
  • bool SearchElementAgain(int)
  • bool DeleteElement(int)

http//ecomputernotes.com
3
Class Set
  • class Set private Collection
  • private
  • ...
  • public
  • void AddMember(int)
  • bool IsMember(int)
  • bool DeleteMember(int)

http//ecomputernotes.com
4
Specialization (Restriction)
  • the derived class is behaviourally incompatible
    with the base class
  • Behaviourally incompatible means that base class
    cant always be replaced by the derived class

http//ecomputernotes.com
5
Specialization (Restriction)
  • Specialization (Restriction) can be implemented
    using private and protected inheritance

http//ecomputernotes.com
6
Example Specialization (Restriction)
Person
age 0..125
age a
setAge( a )
If age lt 18 then error else age a
Adult
age 18..125
setAge( a )
7
Example
  • class Person
  • protected
  • int age
  • public
  • bool SetAge(int _age)
  • if (_age gt0 _age lt 125)
  • age _age
  • return true
  • return false

http//ecomputernotes.com
8
Example
  • class Adult private Person
  • public
  • bool SetAge(int _age)
  • if (_age gt18 _age lt 125)
  • age _age
  • return true
  • return false

http//ecomputernotes.com
9
Private Inheritance
  • Only member functions and friend functions of a
    derived class can convert pointer or reference of
    derived object to that of parent object

http//ecomputernotes.com
10
Example
  • class Parent
  • class Child private Parent
  • int main()
  • Child cobj
  • Parent pptr cobj //Error
  • return 0

http//ecomputernotes.com
11
Example
  • void DoSomething(const Parent )
  • ChildChild()
  • Parent pPtr
  • static_castltParent gt(this)
  • DoSomething(pPtr)
  • // DoSomething(this)

http//ecomputernotes.com
12
Private Inheritance
  • The child class object has an anonymous object of
    parent class object
  • The default constructor and copy constructor of
    parent class are called when needed

http//ecomputernotes.com
13
Example
  • class Parent
  • public
  • Parent()
  • cout ltlt Parent Constructor
  • Parent(const Parent prhs)
  • cout ltlt Parent Copy Constructor

http//ecomputernotes.com
14
Example
  • class Child private Parent
  • public
  • Child()
  • cout ltlt Child Constructor
  • Child(const Child crhs)
  • Parent(crhs)
  • cout ltlt Child Copy Constructor

http//ecomputernotes.com
15
Example
  • int main()
  • Child cobj1
  • Child cobj2 cobj1
  • //Child cobj2(cobj1)
  • return 0

http//ecomputernotes.com
16
Example
  • Output
  • Parent Constructor
  • Child Constructor
  • Parent Copy Constructor
  • Child Copy Constructor

http//ecomputernotes.com
17
Private Inheritance
  • The base class that is more then one level down
    the hierarchy cannot access the member function
    of parent class, if we are using private
    inheritance

http//ecomputernotes.com
18
Class Hierarchy
  • class GrandParent
  • public
  • void DoSomething()
  • class Parent private GrandParent
  • void SomeFunction()
  • DoSomething()

http//ecomputernotes.com
19
Example
  • class Child private Parent
  • public
  • Child()
  • DoSomething() //Error

http//ecomputernotes.com
20
Private Inheritance
  • The base class that is more then one level down
    the hierarchy cannot convert the pointer or
    reference to child object to that of parent, if
    we are using private inheritance

http//ecomputernotes.com
21
Class Hierarchy
  • void DoSomething(GrandParent)
  • class GrandParent
  • class Parent private GrandParent
  • public
  • Parent() DoSomething(this)

http//ecomputernotes.com
22
Example
  • class Child private Parent
  • public
  • Child()
  • DoSomething(this) //Error

http//ecomputernotes.com
23
Protected Inheritance
  • Use protected inheritance if you want to build
    class hierarchy using implemented in terms of

http//ecomputernotes.com
24
Protected Inheritance
  • If B is a protected base and D is derived class
    then public and protected members of B can be
    used by member functions and friends of classes
    derived from D

http//ecomputernotes.com
25
Class Hierarchy
  • class GrandParent
  • public
  • void DoSomething()
  • class Parent protected GrandParent
  • void SomeFunction()
  • DoSomething()

http//ecomputernotes.com
26
Example
  • class Child protected Parent
  • public
  • Child()
  • DoSomething()

http//ecomputernotes.com
27
Protected Inheritance
  • If B is a protected base and D is derived class
    then only friends and members of D and friends
    and members of class derived from D can convert
    D to B or D to B

http//ecomputernotes.com
28
Class Hierarchy
  • void DoSomething(GrandParent)
  • class GrandParent
  • class Parent protected GrandParent

http//ecomputernotes.com
29
Example
  • class Child protected Parent
  • public
  • Child()
  • DoSomething(this)
Write a Comment
User Comments (0)
About PowerShow.com