Computer Notes - Multiple Inheritance II

download Computer Notes - Multiple Inheritance II

of 35

Transcript of Computer Notes - Multiple Inheritance II

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    1/35

    Multiple Inheritance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    2/35

    Multiple Inheritance

    A class can inherit from more thenone class

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    3/35

    Multiple Inheritance

    Transmitter...

    Transmit()

    Receiver...

    Receive()

    Phone

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    4/35

    Example

    class Phone: public Transmitter,public Receiver

    {...

    };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    5/35

    Multiple Inheritance Derived class can inherit from public

    base class as well as private andprotected base classes

    class Mermaid:

    private Woman, private Fish

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    6/35

    Multiple Inheritance The derived class inherits data

    members and functions form all thebase classes

    Object of derived class can performall the tasks that an object of base

    class can perform

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    7/35

    Exampleint main(){

    Phone obj;obj.Transmit();

    obj.Receive();return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    8/35

    Multiple Inheritance

    When using public multipleinheritance, the object of derived

    class can replace the objects of allthe base classes

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    9/35

    Exampleint main(){

    Phone obj;Transmitter * tPtr = &obj;

    Receiver * rPtr = &obj;return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    10/35

    Multiple Inheritance

    The pointer of one base class cannotbe used to call the function of

    another base class The functions are called based on

    static type

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    11/35

    Exampleint main(){

    Phone obj;Transmitter * tPtr = &obj;

    tPtr->Transmit();tPtr->Receive(); //Error

    return 0;

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    12/35

    Exampleint main(){

    Phone obj;Receiver * rPtr = &obj;

    rPtr->Receive();rPtr->Transmit(); //Error

    return 0;

    }http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    13/35

    Multiple Inheritance

    If more than one base class have afunction with same signature then

    the child will have two copies of thatfunction

    Calling such function will result inambiguity

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    14/35

    Multiple Inheritance

    AmphibiousVehicle

    Land Vehicle Water Vehicle

    Car Boat

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    15/35

    Exampleclass LandVehicle{

    public:int GetMaxLoad();

    };class WaterVehicle{

    public:

    int GetMaxLoad();

    };http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    16/35

    Exampleclass AmphibiousVehicle:public LandVehicle,

    public WaterVehicle{};

    int main(){AmphibiousVehicle obj;

    obj.GetMaxLoad(); // Error

    return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    17/35

    Multiple Inheritance

    Programmer must explicitly specifythe class name when calling

    ambiguous function

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    18/35

    Exampleint main(){

    AmphibiousVehicle obj;obj.LandVehicle::GetMaxLoad();

    obj.WaterVehicle::GetMaxLoad();return 0;

    }

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    19/35

    Multiple Inheritance

    The ambiguous call problem canarise when dealing with multiple level

    of multiple inheritance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    20/35

    Multiple Inheritance

    AmphibiousVehicle

    Land Vehicle Water Vehicle

    Vehicle

    Car Boat

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    21/35

    Exampleclass Vehicle{

    public:int GetMaxLoad();

    };class LandVehicle : public Vehicle{

    };

    class WaterVehicle : public Vehicle{

    };http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    22/35

    Exampleclass AmphibiousVehicle:public LandVehicle,

    public WaterVehicle{};

    int main(){AmphibiousVehicle obj;

    obj.GetMaxLoad(); // Error

    return 0;}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    23/35

    Exampleint main()

    {AmphibiousVehicle obj;

    obj.Vehicle::GetMaxLoad(); //Error

    return 0;

    }

    Vehicle is accessible through two paths

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    24/35

    Multiple Inheritance

    AmphibiousVehicle

    Land Vehicle Water Vehicle

    Vehicle

    Car Boat

    Vehicle

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    25/35

    Exampleint main(){

    AmphibiousVehicle obj;

    obj.LandVehicle::GetMaxLoad();

    obj.WaterVehicle::GetMaxLoad();

    return 0;}

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    26/35

    Multiple Inheritance

    Data member must be used withcare when dealing with more then

    one level on inheritance

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    27/35

    Exampleclass Vehicle{

    protected:int weight;

    };

    class LandVehicle : public Vehicle{

    };

    class WaterVehicle : public Vehicle{

    }; http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    28/35

    Exampleclass AmphibiousVehicle:public LandVehicle,

    public WaterVehicle{public:

    AmphibiousVehicle(){

    LandVehicle::weight = 10;WaterVehicle::weight = 10;

    }

    };

    There are multiple copies of data memberweight http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    29/35

    Memory View

    Data Members of

    Vehicle

    Data Members of

    LandVehicle

    Data Members of AmphibiousVehicle

    Data Members of

    Vehicle

    Data Members of

    WaterVehicle

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    30/35

    Virtual Inheritance

    In virtual inheritance there is exactlyone copy of the anonymous base

    class object

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    31/35

    Exampleclass Vehicle{

    protected:

    int weight;};

    class LandVehicle :

    public virtual Vehicle{};

    class WaterVehicle :

    public virtual Vehicle{

    };

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    32/35

    Example

    class AmphibiousVehicle:

    public LandVehicle,public WaterVehicle{

    public:

    AmphibiousVehicle(){

    weight = 10;

    }

    }; http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    33/35

    Memory View

    Data Members of Vehicle

    Data Members ofLandVehicle

    Data Members of AmphibiousVehicle

    Data Members ofWaterVehicle

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    34/35

    Virtual Inheritance

    Virtual inheritance must be used

    when necessary There are situation when

    programmer would want to use two

    distinct data members inherited from

    base class rather then one

    http://ecomputernotes.com

  • 8/3/2019 Computer Notes - Multiple Inheritance II

    35/35

    Example

    Student

    BS Student MS Student PhD Student

    MS/PhD Student

    GPA