Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

Embed Size (px)

Citation preview

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    1/29

    Object-Oriented Programming

    (OOP)Lecture No. 27

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    2/29

    Class Collection

    class Collection {...

    public:

    void AddElement(int);

    bool SearchElement(int);

    bool SearchElementAgain(int);bool DeleteElement(int);

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    3/29

    Class Set

    class Set: private Collection {private:

    ...

    public:

    void AddMember(int);

    bool IsMember(int);bool DeleteMember(int);

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    4/29

    Specialization (Restriction)

    the derived class is behaviourallyincompatible with the base class

    Behaviourally incompatible

    means that base class cant

    always be replaced by the derivedclass

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    5/29

    Specialization (Restriction)

    Specialization (Restriction) can beimplemented using private and

    protected inheritance

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    6/29

    ExampleSpecialization

    (Restriction)Person

    age : [0..125]

    Adultage : [18..125]

    setAge( a )

    setAge( a )

    age = a

    If age < 18 then

    errorelse

    age = a

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    7/29

    Exampleclass Person{

    protected:

    int age;

    public:bool SetAge(int _age){

    if (_age >=0 && _age

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    8/29

    Example

    class Adult : private Person {public:

    bool SetAge(int _age){

    if (_age >=18 && _age

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    9/29

    Private Inheritance

    Only member functions and friendfunctions of a derived class can

    convert pointer or reference of

    derived object to that of parentobject

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    10/29

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    11/29

    Example

    void DoSomething(const Parent &);

    Child::Child(){

    Parent & pPtr =

    static_cast(*this);

    DoSomething(pPtr);// DoSomething(*this);

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    12/29

    Private Inheritance

    The child class object has ananonymous object of parent class

    object

    The default constructor and copy

    constructor of parent class are

    called when needed

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    13/29

    Exampleclass Parent{

    public:

    Parent(){

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    14/29

    Exampleclass Child: private Parent{

    public:Child(){

    cout

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    15/29

    Example

    int main() {Child cobj1;

    Child cobj2 = cobj1;//Child cobj2(cobj1);

    return 0;

    }

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    16/29

    Example

    Output:

    Parent ConstructorChild Constructor

    Parent Copy Constructor

    Child Copy Constructor

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    17/29

    Private Inheritance

    The base class that is more thenone level down the hierarchy

    cannot access the member

    function of parent class, if we areusing private inheritance

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    18/29

    Class Hierarchyclass GrandParent{

    public :

    void DoSomething();

    };

    class Parent: private GrandParent{

    void SomeFunction(){

    DoSomething();}

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    19/29

    Example

    class Child: private Parent{

    public:Child() {

    DoSomething(); //Error

    }

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    20/29

    Private Inheritance

    The base class that is more thenone level down the hierarchy

    cannot convert the pointer or

    reference to child object to that ofparent, if we are using private

    inheritance

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    21/29

    Class Hierarchy

    void DoSomething(GrandParent&);class GrandParent{

    };class Parent: private GrandParent{

    public:

    Parent() {DoSomething(*this);}

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    22/29

    Example

    class Child: private Parent {public:

    Child(){

    DoSomething(*this); //Error

    }

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    23/29

    Protected Inheritance

    Use protected inheritance if youwant to build class hierarchy

    using implemented in terms of

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    24/29

    Protected Inheritance

    If B is a protected base and D isderived class then public and

    protected members of B can be

    used by member functions andfriends of classes derived from D

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    25/29

    Class Hierarchyclass GrandParent{

    public :

    void DoSomething();

    };

    class Parent: protected GrandParent{

    void SomeFunction(){

    DoSomething();}

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    26/29

    Example

    class Child: protected Parent{

    public:

    Child()

    {

    DoSomething();}

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    27/29

    Protected Inheritance

    If B is a protected base and D isderived class then only friends

    and members of D and friends

    and members of class derivedfrom D can convert D* to B* or D&

    to B&

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    28/29

    Class Hierarchy

    void DoSomething(GrandParent&);

    class GrandParent{

    };

    class Parent: protectedGrandParent{

    };

  • 8/13/2019 Object Oriented Programming (OOP) - CS304 Power Point Slides Lecture 27

    29/29

    Example

    class Child: protected Parent {public:

    Child()

    {

    DoSomething(*this);

    }

    };