Inheritance e

download Inheritance e

of 50

Transcript of Inheritance e

  • 7/29/2019 Inheritance e

    1/50

    Inheritance

  • 7/29/2019 Inheritance e

    2/50

    Reusability--building new components by utilizing existing components-

    is yet another important aspect of OO paradigm.

    It is always good/ productive if we are able to reuse something that is

    already exists rather than creating the same all over again.

    This is achieve by creating new classes, reusing the properties of existing

    classes.

    It saves money , time etc.

    To use a class that is already created and tested properly saves the effort

    of testing and developing same again.

    In C++ one class is tested and adapted properly can be used by the

    programmers to suit there requirements.

    Inheritance: Introduction

  • 7/29/2019 Inheritance e

    3/50

    Definition This mechanism of deriving a

    new class from existing/oldclass is called inheritance.

    The old class is known asbase class, super class orparent class

    The new class is known as

    sub class derived class,or child

    class.

    Example:

    DOGS

    LIONS TIGERS LEOPARDS

    CATS HUMANS

    MAMMALS

  • 7/29/2019 Inheritance e

    4/50

    Define a Class Hierarchy

    Syntax:

    classDerivedClassName : access-levelBaseClassName

    where access-level specifies the type of derivation

    private by default, or

    public

    Any class can serve as a base class

    Thus a derived class can also be a base class

  • 7/29/2019 Inheritance e

    5/50

    Implementing Inheritance in C++ by Deriving Classes From

    the Base Class

    Syntax:class

    {

    };class :

    {

    ...

    };

  • 7/29/2019 Inheritance e

    6/50

    class Rectangle{

    private:

    int numVertices;

    float *xCoord, *yCoord;

    public:

    void set(float *x, float *y, int

    nV);

    float area();

    };

    Inheritance Concept

    Rectangle

    Triangle

    Polygon

    class Polygon{

    private:

    int numVertices;

    float *xCoord, *yCoord;

    public:

    void set(float *x, float *y, int

    nV);};

    class Triangle{

    private:

    int numVertices;

    float *xCoord, *yCoord;

    public:

    void set(float *x, float *y, int

    nV);

    float area();

  • 7/29/2019 Inheritance e

    7/50

    RectangleTriangle

    Polygonclass Polygon{

    protected:

    int numVertices;

    float *xCoord, float *yCoord;

    public:

    void set(float *x, float *y, int nV);

    };

    class Rectangle : public

    Polygon{

    public:

    float area();

    };

    class Rectangle{

    protected:

    int numVertices;

    float *xCoord, float *yCoord;

    public:

    void set(float *x, float *y, int nV);

    float area();

    };

    Inheritance Concept

  • 7/29/2019 Inheritance e

    8/50

    RectangleTriangle

    Polygonclass Polygon{

    protected:

    int numVertices;

    float *xCoord, float *yCoord;

    public:

    void set(float *x, float *y, int nV);};

    class Triangle : public

    Polygon{

    public:

    float area();

    };

    class Triangle{

    protected:

    int numVertices;

    float *xCoord, float *yCoord;

    public:

    void set(float *x, float *y, int

    nV);

    float area();

    Inheritance Concept

  • 7/29/2019 Inheritance e

    9/50

    Inheritance Concept

    Point

    Circle 3D-Point

    class Point{

    protected:

    int x, y;

    public:

    void set (int a, int b);};

    class Circle : public Point{private:

    double r;

    };

    class 3D-Point: publicPoint{

    private:

    int z;

    };

    x

    y

    x

    y

    r

    x

    y

    z

  • 7/29/2019 Inheritance e

    10/50

    What to inherit?

    In principle, every member of a base class

    is inherited by a derived class

    just with different access permission

    However, there are exceptions for

    constructor and destructor

    operator=() member

    friends

    Since all these functions are class-specific

  • 7/29/2019 Inheritance e

    11/50

    Access specifiers of derivationThe public access specifier

    The protected access specifier

    The private access specifier

    Sequence of invokingconstructors and destructorsConstructors are called in the

    order of Base to Derived

    Destructors are called in theorder of Derived to Base

  • 7/29/2019 Inheritance e

    12/50

    Public Access Specifier Defines that all the:

    private members of a base class remain

    private in the object

    protected members remain protected

    the public members remain public

  • 7/29/2019 Inheritance e

    13/50

    Protected Access Specifier

    Defines that all the:

    the private members of a base class remain

    private in the object

    the protected members remain protected

    but all the public members of the base class

    become protected

  • 7/29/2019 Inheritance e

    14/50

    Private Specifier

    Defines that all the:

    private members of a base class remain

    private in the object

    public and protected members in the base

    class become private

  • 7/29/2019 Inheritance e

    15/50

    Access Rights of Derived Classes

    The type of inheritance defines the access level for the

    members of derived classthat are inherited from the baseclass

    private protected public

    private - - -

    protected private protected protectedpublic private protected public

    Type of Inheritance

    AccessControl

    forMem

    bers

  • 7/29/2019 Inheritance e

    16/50

    ons ruc or u es or er veClasses

    When a base class and derived class both have

    constructor then the constructor function are executedin the order of derivation. so the constructor of thebase is executed first followed by the execution ofconstructor of derived class and so on down the

    hierarchy.

    If base class constructor takes no parameter then thisinvocation of constructor is implicit but if it does take

    parameters then it s necessary to invoke it explicitly ineach derived class. this is because the derived classconstructor cannot have access to the private datamember of the base class and hence cannot initializethem directly

  • 7/29/2019 Inheritance e

    17/50

    Constructor Rules for Derived Classes

    The default constructor and the destructor ofthe base class are always called when a newobject of a derived class is created or destroyed.

    class A {public:

    A ( )

    {cout

  • 7/29/2019 Inheritance e

    18/50

    Class base

    {int a;

    Public:

    Base(int a1)

    {

    A=a1;

    }

    Void show()

    {Cout

  • 7/29/2019 Inheritance e

    19/50

    Constructor Rules for Derived Classes

    You can also specify an constructor of the base

    class other than the default constructor

    class A {

    public:

    A ( )

    {cout

  • 7/29/2019 Inheritance e

    20/50

    Inheritance Relationship (Contd.)

    The types of inheritance are:

    Single inheritance

    Is displayed when a class inherits attributes

    from a single class

    Multilevel inheritance

    A

    B

    A

    B

    C

  • 7/29/2019 Inheritance e

    21/50

    Hierarchical inheritance

    Hybrid inheritance

    Multiple inheritance

    A B

    C

    A

    B

    D

    C

  • 7/29/2019 Inheritance e

    22/50

    exampleClass base_class

    {

    Private:Int num1;

    Public:

    Void base_read()

    {

    Coutnum1;}

    Void base_show()

    {

    Cout

  • 7/29/2019 Inheritance e

    23/50

    Class derived_class:public base_class

    {

    Private:Int num2;

    Public:

    Void derived_read()

    {

    Coutnum2;}

    Void derived_show()

    {

    Cout

  • 7/29/2019 Inheritance e

    24/50

    void main()

    {

    derived_class d1;

    d1.base_read();d1.derived_read();

    d1.base_show();

    d1.derived_show();

    getch();

    }

  • 7/29/2019 Inheritance e

    25/50

    Multiple Inheritance

    Is the phenomenon where a classmay inherit from two or moreclasses

    Syntax:

    class derived : public base1, publicbase2

    {

    //Body of class

    };

  • 7/29/2019 Inheritance e

    26/50

    multiple

    Class base1

    {

    protected:

    int a;

    public:

    void show().

    {

    cout

  • 7/29/2019 Inheritance e

    27/50

    Class base2

    {

    protected:

    int b;

    public:

    void display()

    {

    cout

  • 7/29/2019 Inheritance e

    28/50

    Class derived:public base1,public base2

    {

    public:

    Void setdata(int x.int y){

    a=x;

    b=y

    }

    };

  • 7/29/2019 Inheritance e

    29/50

    Void main()

    {

    Derived d;d.setdata(10,20);

    d.show();

    d.dispaly();Getch();

    }

  • 7/29/2019 Inheritance e

    30/50

    http://www.slideworld.com/slideshow.aspx/O

    OPS-INHERITANCE-ppt-2768891#

    http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891http://www.slideworld.com/slideshow.aspx/OOPS-INHERITANCE-ppt-2768891
  • 7/29/2019 Inheritance e

    31/50

    Ambiguities in Multiple Inheritance

    Can arise when two base classes contain afunction of the same name

    Can arise when the derived class has

    multiple copies of the same base class

    Class A

    Class B

    Class D

    Class C

  • 7/29/2019 Inheritance e

    32/50

    Ambiguities in Multiple Inheritance (Contd.)

    Can arise when two base classes

    contain a function of the samename

    Example:

    #includeclass base1

    {

    public:

    void disp()

    {

    cout

  • 7/29/2019 Inheritance e

    33/50

    Ambiguities in Multiple Inheritance (Contd.)

    class base2

    {

    public:

    void disp()

    {

    cout

  • 7/29/2019 Inheritance e

    34/50

    Ambiguities in Multiple Inheritance (Contd.)

    int main()

    {

    derived Dvar;

    Dvar.disp(); //Ambiguous

    function callreturn 0;

    }

  • 7/29/2019 Inheritance e

    35/50

    Ambiguities in Multiple Inheritance (Contd.)

    Can be resolved in two ways:

    By using the scope resolution operator D1.base1::disp();

    D1.base2::disp();OR

    Defining explicitly member function

    By overriding the function in the derivedclass

    Void disp()

    {

    base1::disp();

    base2::disp();

    }

  • 7/29/2019 Inheritance e

    36/50

    class person

    {

    private:

    char name[10];int phn;

    public:

    void read()

    {

    coutname>>phn;

    }void show()

    {

    cout

  • 7/29/2019 Inheritance e

    37/50

    class student

    {

    private:

    int rollno;

    char course;public:

    void read()

    {

    coutrollno>>course;

    }void show()

    {

    cout

  • 7/29/2019 Inheritance e

    38/50

    class info:public student,public person{

    void inputdata()

    {person::read();

    student::read();

    coutgender;}

  • 7/29/2019 Inheritance e

    39/50

    void outputdata()

    {

    person::show();

    student::show();

    cout

  • 7/29/2019 Inheritance e

    40/50

    Can arise when the derived class has multiple copies

    of the same base class

    Class A

    Class B

    Class D

    Class C

  • 7/29/2019 Inheritance e

    41/50

    Virtual base class

    The duplication of inheritedmembers due to these multiple

    paths can be avoided by making

    the common base class as virtual

    base class while declaring the

    direct or intermediate base classes

    When the class is made a virtual

    base class,C++ takes necessary

    care to see that only on e copy ofthat class is inherited,regardless of

    how many inheritance paths exist

    between the virtual base class and

    a derived class.

    grandparent

    Child text

    Parent 1 Parent 2

  • 7/29/2019 Inheritance e

    42/50

    Virtual Base Class

    When same class is inherited more than once via multiple paths,multiple copies of the base class member are created in memory.By declaring the base class as virtual only 1 copy of the base classisinherited

    Allows to have only one copy of the baseclass members in memory when a classinherits same properties or methods morethan once through multiple paths

    Is implemented by using the virtualqualifier when inheriting from the base class

    Solution

  • 7/29/2019 Inheritance e

    43/50

    Class a

    {.};

    Class b:virtual public a

    {};

    Class c:virtual public a

    {};Class d:public b,public c

    {..};

  • 7/29/2019 Inheritance e

    44/50

    Invocation of Constructors

    Is done in the following order:

    1. Virtual base class constructors in the order

    of inheritance

    2. Non-virtual base class constructors in the

    order of inheritance

    3. Member objects' constructors in the

    order of declaration

    4. Derived class constructors

  • 7/29/2019 Inheritance e

    45/50

    class grandparent

    {protected:

    int base_data;

    public:

    void readgp()

    {

    coutbase_data;}

    };

  • 7/29/2019 Inheritance e

    46/50

    Class parent1:virtual public grandparent

    {protected:

    int parent1_data;

    public:void read1()

    {

    coutparent1_data;}

    };

  • 7/29/2019 Inheritance e

    47/50

    Class parent2:virtual public grandparent

    {protected:

    int parent2_data;

    public:

    void read2(){

    coutparent2_data;

    }};

    l hild bli bli

  • 7/29/2019 Inheritance e

    48/50

    class child:public parent1,public parent2

    {

    private :

    int sum;

    public:

    int add()

    {sum=base_data+parent1_data+parent2_data;

    }

    void show()

    {

    cout

  • 7/29/2019 Inheritance e

    49/50

    void main(){

    child c1;

    c1.readgp();

    c1.read1();

    c1.read2();

    c1.add();

    c1.show();

    getch();

    }

  • 7/29/2019 Inheritance e

    50/50

    Abstract class-

    In certain situations a programmer may create a classbut never creates its object, such a class whose objectcan not be created is called abstract class

    And whose object can be created is known as concreteclass.

    it is designed only to be inherited

    Eg-A is base class which act as

    abstract class

    B and C are concrete class

    A

    B C