Computer Programming Inheritance

Embed Size (px)

Citation preview

  • 8/13/2019 Computer Programming Inheritance

    1/40

    1

    CS 103

    Computer Programming (CP)

    Inheritance

    Single Inheritance Multiple Inheritance

  • 8/13/2019 Computer Programming Inheritance

    2/40

    Inheritance

    Inheritance is an is-a relationship Example:Every Employee is a Person

    Inheritance lets us create new classes from

    existing classes New classes are called the derived classes

    Existing classes are called thebase classes

    Derived classes inherit the properties of the

    Base classes

    2

  • 8/13/2019 Computer Programming Inheritance

    3/40

    Inheritance (cont'd.)

    Inheritance can be either single

    inheritance or multiple

    inheritance.

    Single Inheritance: derived class has a singlebase class

    Multiple Inheritance: derived class has more

    than one base class

    3

  • 8/13/2019 Computer Programming Inheritance

    4/40

    Inheritance (cont'd.)

    Inheritance can be viewed as atree-likeor

    hierarchicalstructure wherein a base class

    is shown with its derived classes.

    4

  • 8/13/2019 Computer Programming Inheritance

    5/40

    Inheritance (cont'd.)

    General syntax of a derived class:

    wherememberAccessSpecifier ispublic,

    protected,orprivate(default)

    Theprivate members of a base class areprivate to the base class

    derived class cannot directly access them

    5

  • 8/13/2019 Computer Programming Inheritance

    6/40

    Inheritance (cont'd.)

    public members of base class can be

    inherited aspublicorprivatemembers

    The derived class can include additional

    members data and/or functions

    The derived class can redefine thepublic

    member functions of the base class

    Applies only to the objects of the derived class

    All members of the base class are also

    member variables of the derived class

    6

  • 8/13/2019 Computer Programming Inheritance

    7/40

    Two Important Issues Related to

    Inheritance

    1. Redefining (Overriding) Member Functions of

    the Base Class

    2. Constructors of Derived and Base Classes

    7

  • 8/13/2019 Computer Programming Inheritance

    8/40

    1. Redefining (Overriding) Member

    Functions of the Base Class

    To redefine apublicmember function:

    Corresponding function in derived class must have

    same name/number/types of parameters

    If derived class overrides a publicmember function of the base class, then to

    call the base class function, specify:

    Name of the base class

    Scope resolution operator (: :)

    Function name with appropriate parameter list

    8

  • 8/13/2019 Computer Programming Inheritance

    9/40

    Function Overriding vs. Function

    Overloading

    The name of the function being redefined in the

    derived class must have the same name and thesame set of parameters (function

    overriding).

    If the corresponding functions in the base class

    and the derived class have the same name but

    different sets of parameters, then this isfunction overloading in the derived class.

    9

  • 8/13/2019 Computer Programming Inheritance

    10/40

    Base Class : rectangleType

    //rectangleType.hclass Rectangle

    {

    private:

    double length;

    double width;

    public:

    Rectangle();

    Rectangle(double l, double w);

    void setDimension(double l, double w);

    double getLength() const;

    double getWidth() const;

    double area() const;

    double perimeter() const;

    void print() const;

    };

    10

  • 8/13/2019 Computer Programming Inheritance

    11/40

    rectangleType Class Diagram11

  • 8/13/2019 Computer Programming Inheritance

    12/40

    //rectangleType.cpp

    Rectangle::Rectangle()

    {

    length = 0;width = 0;

    }

    Rectangle::Rectangle(double l, double w)

    {

    setDimension(l, w);

    }

    void Rectangle::setDimension(double l, double w)

    {

    length = l;

    width = w;

    }double Rectangle::getLength() const

    {

    return length;

    }

    12

  • 8/13/2019 Computer Programming Inheritance

    13/40

    13double Rectangle::getWidth()const

    {

    return width;

    }

    double Rectangle::area() const{

    return length * width;

    }

    double Rectangle::perimeter() const

    {

    return 2 * (length + width);}

    void Rectangle::print() const

    {

    cout

  • 8/13/2019 Computer Programming Inheritance

    14/40

    Derived Class :boxType

    //boxType.h

    class Box: public Rectangle

    {

    private:

    double height;

    public:Box();

    Box(double l, double w, double h);

    void setDimension(double l, double w, double h);

    double getHeight() const;

    double area() const;double volume() const;

    void print() const;

    };

    14

  • 8/13/2019 Computer Programming Inheritance

    15/40

    boxType Class Diagram

    boxTypeis derived from rectangleType,andit is apublic inheritance

    Also overridesprint andarea

    15

  • 8/13/2019 Computer Programming Inheritance

    16/40

    boxType.cpp

    Box::Box()

    {

    height = 0.0;}

    Box::Box(double l, double w, double h):Rectangle(l, w)

    {

    height = h;

    }

    void Box::setDimension(double l, double w, double h)

    {

    Rectangle::setDimension(l, w);

    height = h;

    }

    double Box::getHeight() const{

    return height;

    }

    16

  • 8/13/2019 Computer Programming Inheritance

    17/40

    double Box::area() const

    {

    return 2 * (getLength() * getWidth()

    + getLength() * height+ getWidth() * height);

    }

    double Box::volume() const

    {

    return Rectangle::area() * height;

    }

    void Box::print() const

    {

    Rectangle::print();

    cout

  • 8/13/2019 Computer Programming Inheritance

    18/40

    2. Constructors of Derived and

    Base Classes

    Derived class constructor cannot directly accessprivate members of the base class

    Derived class can directly initialize onlypublic

    member variables of the base class

    When a derived object is declared

    It must execute one of the base class constructors

    Call to base class constructor is specified in

    heading of derived class constructor definition

    18

  • 8/13/2019 Computer Programming Inheritance

    19/40

    Constructors of Derived and Base

    Classes (cont'd.)

    19

    Box::Box()

    {

    height = 0.0;

    }

    Box::Box(double l, double w, double h):Rectangle(l, w)

    { height = h;

    }

  • 8/13/2019 Computer Programming Inheritance

    20/40

    20int main()

    {

    Rectangle Rectangle1;

    Rectangle Rectangle2(8, 6);

    cout

  • 8/13/2019 Computer Programming Inheritance

    21/40

    21Box Box1;

    Box Box2(10, 7, 3);

    cout

  • 8/13/2019 Computer Programming Inheritance

    22/40

    Constructors of Derived and Base

    Classes (cont'd.)Rect angl e Rect angl e1( 5. 0, 3. 0) ;Box Box1( 6. 0, 5. 0, 4. 0) ;

    22

  • 8/13/2019 Computer Programming Inheritance

    23/40

    Destructors in a Derived Class

    Destructors

    Used to deallocate dynamic memory allocated by the objects

    of a class

    When a derived class object goes out of scope

    Automatically invokes its destructor

    When the destructor of the derived class executes

    Automatically invokes the destructor of the base class

    23

  • 8/13/2019 Computer Programming Inheritance

    24/40

    Header File of a Derived Class

    To define new classes

    Create new header files

    To create new classes based on previously defined

    classesHeader files of the new classes contain commands that specify

    where to look for the definitions of the base classes

    The definitions of the member functions can be

    placed in a separate file

    24

  • 8/13/2019 Computer Programming Inheritance

    25/40

    Multiple Inclusions of a Header

    File

    Use the preprocessor command (#include) to

    include a header file in a program

    The preprocessor processes the program before it

    is compiled To avoid multiple inclusion of a file in a program

    Use certain preprocessor commands in the header file

    (file guard)

    25

  • 8/13/2019 Computer Programming Inheritance

    26/40

    Problem: Solution:

    26

    Multiple Inclusions of a Header File

    (contd.)

  • 8/13/2019 Computer Programming Inheritance

    27/40

    Inheritance and Access Specifiers

    // Inherit from Base publicly

    class Der: public Base

    {

    };

    // Inherit from Base privately

    class Der: private Base{

    };

    // Inherit from Base protectedly

    class Der: protected Base

    {

    };

    class Der: Base // Defaults to private inheritance

    {

    };

    27

  • 8/13/2019 Computer Programming Inheritance

    28/40

    public Inheritance

    IfmemberAccessSpecifierispublic:

    publicmembers ofAarepublicmembers

    ofB and can be directly accessed in class Bprotectedmembers ofAareprotected

    members ofBand can be directly accessed bymember functions (and friend functions) ofB

    privatemembers ofAare hidden in Bandcan be accessed by member functions of Bthroughpublicorprotectedmembers ofA

    28

  • 8/13/2019 Computer Programming Inheritance

    29/40

    public Inheritance (contd.)

    publicInheritance

    Base Access

    Specifier

    Derived Access

    Specifier

    Derived Class

    Access?public public Yes

    private private No

    protected protected Yes

    29

  • 8/13/2019 Computer Programming Inheritance

    30/40

    protected Inheritance

    If memberAccessSpecifier isprotected:

    public members ofA are protectedmembers of B and can be accessed by themember functions (and friend functions) ofB

    protectedmembers ofAareprotectedmembers of B and can be accessed by the

    member functions (and friend functions) ofBprivatemembers ofAare hidden in Band

    can be accessed by member functions of Bthroughpublicorprotectedmembers ofA

    30

  • 8/13/2019 Computer Programming Inheritance

    31/40

    protected Inheritance (contd.)

    protected Inheritance

    Base Access

    Specifier

    Derived Access

    Specifier

    Derived Class

    Access?public protected Yes

    private private No

    protected protected Yes

    31

  • 8/13/2019 Computer Programming Inheritance

    32/40

    private Inheritance

    If memberAccessSpecifier isprivate:

    publicmembers ofAareprivatemembersofBand can be accessed by member functionsofB

    protected members ofA areprivatemembers ofBand can be accessed by member

    functions (and friend functions) ofBprivatemembers ofAare hidden in Band

    can be accessed by member functions of Bthroughpublic/protectedmembers ofA

    32

  • 8/13/2019 Computer Programming Inheritance

    33/40

    private Inheritance (contd.)

    private Inheritance

    Base Access

    Specifier

    Derived Access

    Specifier

    Derived Class

    Access?public private Yes

    private private No

    protected private Yes

    33

  • 8/13/2019 Computer Programming Inheritance

    34/40

    Multiple Inheritance

    Multiple Inheritance enables a derived class to

    inherit members from more than one base class.

    Lets say we wanted to write a program to keep

    track of a group of teachers. A teacher is a person.

    However, a teacher is also an employee.

    Multiple inheritance can be used to create a

    Teacher class that inherits properties from both

    Person and Employee.

    To use multiple inheritance, simply specify each

    base class (just like in single inheritance),

    separated by a comma.

    34

  • 8/13/2019 Computer Programming Inheritance

    35/40

    MultipleInheritance.cpp

    35

  • 8/13/2019 Computer Programming Inheritance

    36/40

    Problems with Multiple Inheritance

    While multiple inheritance seems like a simpleextension of single inheritance, multiple

    inheritance introduces a lot of issues that can

    markedly increase the complexity of programs and

    make them a maintenance terrible.

    36

  • 8/13/2019 Computer Programming Inheritance

    37/40

    DiamondProblem.cpp

    37

  • 8/13/2019 Computer Programming Inheritance

    38/40

    virtual Base Class

    To share a base class, simply insert thevirtualkeyword in the inheritance list of the

    derived class.

    This creates what is called a virtual base

    class, which means there is only one base object

    that is shared.

    38

  • 8/13/2019 Computer Programming Inheritance

    39/40

    class PoweredDevice{

    };

    class Scanner: virtual public PoweredDevice

    {};

    class Printer: virtual public PoweredDevice

    {

    };

    class Copier: public Scanner, public Printer

    {

    };

    39

  • 8/13/2019 Computer Programming Inheritance

    40/40

    Now, when you create a Copier class, you will getonly one copy ofPoweredDevicethat will be

    shared by both Scanner andPrinter.

    However, this leads to one more problem: if

    Scanner and Printer share aPoweredDevicebase class, who is responsible

    for creating it?

    The answer, as it turns out, is Copier. The

    Copier constructor is responsible for creating

    PoweredDevice.

    40