22
I. Hrivnacova @ Data Processing Course 2019 1 Inheritance Data Processing Course, I. Hrivnacova, IPN Orsay OOP , Classes Reminder Inheritance Relationship Derived Class Definition Overriding Functions Polymorphism Pure Virtual Functions and Abstract Classes

Inheritance - ipn · I. Hrivnacova @ Data Processing Course 2019 7 Inheritance The common behavior of objects can be expressed by means of inheritance – There is no equivalent data

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

  • I. Hrivnacova @ Data Processing Course 2019 1

    Inheritance

    Data Processing Course,I. Hrivnacova, IPN Orsay

    ● OOP, Classes – Reminder● Inheritance Relationship● Derived Class Definition● Overriding Functions● Polymorphism● Pure Virtual Functions and

    Abstract Classes

  • I. Hrivnacova @ Data Processing Course 2019 2

    Object-Oriented Programming (OOP)● The basic unit of OOP is a class● A class is an implementation of an

    abstract data type:– It describes both the attributes

    (data) of an object and itsoperations (methods)

    ● The OOP languages let you thinkin the problem space, and usesoftware objects to represent andabstract entities of the problemspace to solve the problem.

    Chua Hock-Chuan: Programming Notes

  • I. Hrivnacova @ Data Processing Course 2019 3

    A ClassA class can be visualized as a three-compartmentbox, as illustrated:

    ● Classname (or identifier): identifies the class.● Data Members (or variables, attributes, states,

    fields): – contain the static attributes of the class.

    ● Member Functions (or methods, behaviors,operations): – contain the dynamic operations of the class.

    ● In other words, a class encapsulates the staticattributes (data) and dynamic behaviors(operations that operate on the data) in a box.

    ● Class Members: The data members andmember functions are collectively called classmembers.Chua Hock-Chuan: Programming Notes

  • I. Hrivnacova @ Data Processing Course 2019 4

    Example Of Classes, Instances

    ● The class and instances diagrams aredrawn according to the UnifiedModeling Language (UML) notations.

    Chua Hock-Chuan: Programming Notes

  • I. Hrivnacova @ Data Processing Course 2019 5

    Class Definition

    class Car { private: int yearOfManufacture; int mileage; int licensePlate;

    public: int getYearOfManufacture(); void drive(float m); float getMileage();

    void Car(); void ~Car();};

    The members declared afterprivate are protected from beingaccessed by application programers(Data encapsulation)

    For the application developeronly the public interface isof interest.

  • I. Hrivnacova @ Data Processing Course 2019 6

    Inheritance

    ● The real life example of inheritance is child and parents, all theproperties of father are inherited by his son.

    Source: http://www.sitesbay.com/cpp/cpp-inheritance

  • I. Hrivnacova @ Data Processing Course 2019 7

    Inheritance

    ● The common behavior of objects can be expressed by meansof inheritance– There is no equivalent data model in conventional (procedural)

    programming● A class A can inherit the state (variables) and behavior

    (methods) of another class B– The relationship "Is a" ( "Inherits from")– We say class A is a base class (or a generalization, or a super-type)

    of class B and class B is a class derived from Class A (or aspecialization, or a subtype)

  • I. Hrivnacova @ Data Processing Course 2019 8

    Inheritance – Example● The Vehicle class describes the

    properties of a vehicle as a baseclass (common properties)

    ● Class hierarchy arises wherecommon terms become more andmore concrete

    ● Inheritance is the "is-a"relationship: – a truck is a "vehicle with a

    speedometer"– a "vehicle with a speedometer" is a

    vehicleNicolai M. Josuttis: Object-Oriented Programming in C++

  • I. Hrivnacova @ Data Processing Course 2019 9

    Inheritance -Class Definition

    Shape

    mWidthmHeight

    setData()

    Rectangle

    getArea()

    Triangle

    getArea)

    class Shape { public: void setData(float w, float h) { mWidth = w; mHeight = h; } protected: float mWidth; float mHeight;};

    class Rectangle : public Shape { public: float getArea() { return mWidth * mHeight; }};

    class Triangle : public Shape { public: float getArea() { return mWidth * mHeight / 2.; }};

  • I. Hrivnacova @ Data Processing Course 2019 10

    Inheritance -Test Program

    class Shape { public: void setData(float w, float h) { mWidth = w; mHeight = h; } protected: float mWidth; float mHeight;};

    class Rectangle : public Shape { public: float getArea() { return mWidth * mHeight; }};

    class Triangle : public Shape { public: float getArea() { return mWidth * mHeight / 2.; }};

    #include "shapes.h"#include using namespace std;

    int main(){ // Create object of derived type Rectangle r1; // Call function of base class r1.setData(3., 4.); // Call function of derived class cout

  • I. Hrivnacova @ Data Processing Course 2019 11

    Inheritance -Test Program

    #include "shapes.h"#include using namespace std;

    int main(){ // Create object of derived type Rectangle r1; // Call function of base class r1.setData(3., 4.); // Call function of derived class cout

  • I. Hrivnacova @ Data Processing Course 2019 12

    Derived Class Definition

    ● The syntax for definition of a derived class: class Derived : public Base { ... };

    – The inheritance with private and protected member access specifierwill not be discussed in this course

    ● The derived class inherits data and functions of the base class● The protected members of the base class can be accessed

    by the derived class, the private members are inaccessible

  • I. Hrivnacova @ Data Processing Course 2019 13

    Overriding Functions

    Shape

    mWidthmHeight

    setData()getArea)

    Rectangle

    getArea()

    Triangle

    getArea)

    class Shape { public: void setData(float w, float h) { mWidth = w; mHeight = h; } virtual float getArea() { return 0; } protected: float mWidth; float mHeight;};

    class Rectangle : public Shape { public: virtual float getArea() { return mWidth * mHeight; }};

    class Triangle : public Shape { public: virtual float getArea() { return mWidth * mHeight / 2.; }};

  • I. Hrivnacova @ Data Processing Course 2019 14

    class Shape { public: void setData(float w, float h) { mWidth = w; mHeight = h; } virtual float getArea() { return 0; } protected: float mWidth; float mHeight;};

    class Rectangle : public Shape { public: virtual float getArea() { return mWidth * mHeight; }};

    class Triangle : public Shape { public: virtual float getArea() { return mWidth * mHeight / 2.; }};

    // Test program#include "shapes.h"#include using namespace std;

    int main() { // Create object of derived type Rectangle r1; // Get base class pointer to rectangle Shape* s1 = &r1; s1->setData(3., 4.); cout

  • I. Hrivnacova @ Data Processing Course 2019 15

    // Test program#include "shapes.h"#include using namespace std;

    int main() { // Create object of derived type Rectangle r1; // Get base class pointer to rectangle Shape* s1 = &r1; s1->setData(3., 4.); cout

  • I. Hrivnacova @ Data Processing Course 2019 16

    Overriding Functions

    ● A member function of a class can be redefined in its derivedclasses if it is declared with keyword virtual.– The member function getArea() has been declared as virtual– The virtual keyword in derived classes is not necessary, but it is

    a good to use it to make the virtual functions "visible".● The advantage of having virtual function is that we are able to

    access the function of a derived class by a pointer variable ofbase class.

    ● Non virtual functions with the same name in a class hierarchyshould be avoided

  • I. Hrivnacova @ Data Processing Course 2019 17

    Polymorphism// test program#include "shapes.h"#include #include using namespace std;

    int main(){ // Create objects Rectangle r1; r1.setData(3., 4.); Triangle t1; t1.setData(5., 6.); Shape s3; s3.setData(6., 7.);

    // Handle object via base type vector shapes; shapes.push_back(&r1); shapes.push_back(&t1); shapes.push_back(&s3); for ( Shape* shape : shapes ) { cout

  • I. Hrivnacova @ Data Processing Course 2019 18

    Polymorphism// test program#include "shapes.h"#include #include using namespace std;

    int main(){ // Create objects Rectangle r1; r1.setData(3., 4.); Triangle t1; t1.setData(5., 6.); Shape s3; s3.setData(6., 7.);

    // Handle object via base type vector shapes; shapes.push_back(&r1); shapes.push_back(&t1); shapes.push_back(&s3); for ( Shape* shape : shapes ) { cout

  • I. Hrivnacova @ Data Processing Course 2019 19

    Polymorphism

    ● An ability to create an object that has more than one form● The objects belonging to different types can respond to method

    ( field, or property) calls of the same name, each one accordingto an appropriate type-specific behavior.

    ● The programmer (and the program) does not have to know theexact type of the object in advance, and so the exact behavioris determined at run-time: this is called late binding or dynamicbinding

  • I. Hrivnacova @ Data Processing Course 2019 20

    Pure Virtual Functions

    Shape

    mWidthmHeight

    setData()getArea)

    Rectangle

    getArea()

    Triangle

    getArea)

    class Shape { public: void setData(float w, float h) { mWidth = w; mHeight = h; } virtual float getArea() = 0;

    protected: float mWidth; float mHeight;};

    class Rectangle : public Shape { public: virtual float getArea() { return mWidth * mHeight; }};

    class Triangle : public Shape { public: virtual float getArea() { return mWidth * mHeight / 2.; }};

  • I. Hrivnacova @ Data Processing Course 2019 21

    // test program#include "shapes.h"#include #include using namespace std;

    int main(){ // Create objects Rectangle r1; r1.setData(3., 4.); Triangle t1; t1.setData(5., 6.); Shape s3; s3.setData(6., 7.);

    // ...}

    class Shape { public: void setData(float w, float h) { mWidth = w; mHeight = h; } virtual float getArea() = 0;

    protected: float mWidth; float mHeight;};

    code.cxx:41:9: error: variable type 'Shape' is an abstract class Shape s3; ^code.cxx:7:17: note: unimplemented pure virtual method 'getArea' in 'Shape' virtual float getArea() = 0; ^

  • I. Hrivnacova @ Data Processing Course 2019 22

    Abstract Classes

    ● It is impossible to create an instance of the class that has notdefined all inherited pure virtual functions

    ● Classes whose instance cannot be created are called abstract classes.

    ● We can force derived classes to implement the interfacesrequired by declaring pure virtual functions– All classes derived from Shape MUST implement getArea()

    Slide 1Slide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22