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

Embed Size (px)

Citation preview

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

    1/35

    Object-Oriented Programming

    (OOP)Lecture No. 31

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

    2/35

    Multiple Inheritance

    A class can inherit from morethen one class

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

    3/35

    Multiple Inheritance

    Transmitter...

    Transmit()

    Receiver...

    Receive()

    Phone

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

    4/35

    Example

    class Phone: public Transmitter,public Receiver

    {...};

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

    5/35

    Multiple Inheritance

    Derived class can inherit frompublic base class as well asprivate and protected baseclasses

    class Mermaid:private Woman, private Fish

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

    6/35

    Multiple Inheritance

    The derived class inherits datamembers and functions form allthe base classes

    Object of derived class canperform all the tasks that an

    object of base class can perform

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

    7/35

    Example

    int main(){Phone obj;

    obj.Transmit();obj.Receive();return 0;

    }

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

    8/35

    Multiple Inheritance

    When using public multipleinheritance, the object of derivedclass can replace the objects ofall the base classes

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

    9/35

    Example

    int main(){Phone obj;

    Transmitter * tPtr = &obj;Receiver * rPtr = &obj;return 0;

    }

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

    10/35

    Multiple Inheritance

    The pointer of one base classcannot be used to call thefunction of another base class

    The functions are called based on

    static type

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

    11/35

    Example

    int main(){Phone obj;

    Transmitter * tPtr = &obj;tPtr->Transmit();tPtr->Receive(); //Error return 0;

    }

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

    12/35

    Example

    int main(){Phone obj;

    Receiver * rPtr = &obj;rPtr->Receive();rPtr->Transmit(); //Error return 0;

    }

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

    13/35

    Multiple Inheritance

    If more than one base class havea function with same signaturethen the child will have two copiesof that function

    Calling such function will result inambiguity

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

    14/35

    Multiple Inheritance

    AmphibiousVehicle

    Land Vehicle Water Vehicle

    Car Boat

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

    15/35

    Example

    class LandVehicle{public:

    int GetMaxLoad();};class WaterVehicle{

    public:int GetMaxLoad();

    };

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

    16/35

    Exampleclass AmphibiousVehicle:

    public LandVehicle,public WaterVehicle{

    };int main(){ AmphibiousVehicle obj;

    obj.GetMaxLoad(); // Errorreturn 0;}

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

    17/35

    Multiple Inheritance

    Programmer must explicitlyspecify the class name whencalling ambiguous function

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

    18/35

    Example

    int main(){ AmphibiousVehicle obj;

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

    }

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

    19/35

    Multiple Inheritance

    The ambiguous call problem canarise when dealing with multiplelevel of multiple inheritance

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

    20/35

    Multiple Inheritance

    AmphibiousVehicle

    Land Vehicle Water Vehicle

    Vehicle

    Car Boat

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

    21/35

    Example

    class Vehicle{public:

    int GetMaxLoad();};class LandVehicle : public Vehicle{

    };class WaterVehicle : public Vehicle{};

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

    22/35

    Exampleclass AmphibiousVehicle:

    public LandVehicle,public WaterVehicle{

    };int main(){ AmphibiousVehicle obj;

    obj.GetMaxLoad(); // Errorreturn 0;}

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

    23/35

    Example

    int main(){ AmphibiousVehicle obj;obj.Vehicle::GetMaxLoad(); //Errorreturn 0;

    } Vehicle is accessible through two

    paths

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

    24/35

    Multiple Inheritance

    AmphibiousVehicle

    Land Vehicle Water Vehicle

    Vehicle

    Car Boat

    Vehicle

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

    25/35

    Example

    int main(){ AmphibiousVehicle obj;obj.LandVehicle::GetMaxLoad();obj.WaterVehicle::GetMaxLoad();

    return 0;}

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

    26/35

    Multiple Inheritance

    Data member must be used withcare when dealing with more thenone level on inheritance

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

    27/35

    Example

    class Vehicle{protected:

    int weight;};class LandVehicle : public Vehicle{

    };class WaterVehicle : public Vehicle{};

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

    28/35

    Exampleclass AmphibiousVehicle:

    public LandVehicle,public WaterVehicle{

    public: AmphibiousVehicle(){

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

    }}; There are multiple copies of data member

    weight

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

    29/35

    Memory View

    Data Members ofVehicle

    Data Members ofLandVehicle

    Data Members of AmphibiousVehicle

    Data Members ofVehicle

    Data Members ofWaterVehicle

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

    30/35

    Virtual Inheritance

    In virtual inheritance there isexactly one copy of theanonymous base class object

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

    31/35

    Exampleclass Vehicle{protected:

    int weight;};

    class LandVehicle :public virtual Vehicle{

    };

    class WaterVehicle :public virtual Vehicle{};

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

    32/35

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

    33/35

    Memory View

    Data Members of Vehicle

    Data Members ofLandVehicle

    Data Members of AmphibiousVehicle

    Data Members ofWaterVehicle

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

    34/35

    Virtual Inheritance

    Virtual inheritance must be usedwhen necessary There are situation when

    programmer would want to usetwo distinct data members

    inherited from base class ratherthen one

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

    35/35

    Example

    Student

    BS Student MS Student PhD Student

    MS/PhD Student

    GPA