C++ Inheritance

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    C++ Inheritance - Presentation Transcript

    1. Inheritance Jussi Pohjolainen TAMK University of Applied Sciences
    2. Introduction to Inheritance
      • Inheritance is a relationship between two or more classes where derived class inherites behaviour and attributes of pre-existing (base) classes
      • Intended to help reuse of existing code with little or no modification
    3. Inheritance
      • Inheritance can be continous
        • Derived class can inherit another class, which inherits another class and so on
        • When changing the base class all the derived classes changes also
      • Example:
        • Mammal <– Human <– Worker <- Programmer
      • Could mammal be a derived class? If so, what would be the base class?
    4. Picture about Inheritance C lass B F eatures: a,b,c C lass D F eatures: a,b,d,e,f a b C lass A features: a,b c d e C lass C F eatures: a,b,d,e f
    5. Multiple Inheritance
      • In multiple inheritance a derived class has multiple base classes
      • C++ supports multiple base classes, Java don't
      Driver - license - Y ear of approval Conductor - A ccount number Taxi Driver - area House Boat Houseboat
    6. Inheritance and Capsulation
      • private
        • Is accessible only via the base class
      • public
        • Is accessible everywhere (base class, derived class, othe classes)
      • protected
        • Is accessible by the base class and derived classes
    7. Basic example
      • What are Programmer's attributes and methods?
      Human string name void sleep() void drink() void eat() Programmer int salary void implementApps() void beNerd()
    8. Overriding?
      • What about now?
      Human string name void sleep() void drink() void eat() Programmer int salary void implementApps() void beNerd() void drink() void eat()
    9. Overriding
      • Since programmer eats and drinks differently than humans (only Coke and Pizza) the eat and drink methods are overriden in Programmer!
    10. Abstract Class
      • Abstract class is a class which you cannot instantiate (create objects)
      • You can inherit abstract class and create objects from the inherited class, if it is concrete one
      • Abstract class in C++ has abstract methods, that do not have implementations
      • These methods forces derived classes to implement those methods
    11. Example <<abstract>> Mammal string name void makesound() {abstract} Elephant int trunkLength makesound()
    12. Example <<abstract>> Figure int x, y double calculateArea() {abstract} Circle double radius double calculateArea() Rect double length, height double calculateArea()
    13. Exercises
    14. INHERITANCE IN C++
    15. Declaring Inheritance
      • class Circle : public Figure
      • {
      • }
    16. Declaring Inheritance
      • class Figure
      • {
      • public:
      • int x, y;
      • };
      • class Circle : public Figure
      • {
      • public:
      • int radius;
      • };
      • int main()
      • {
      • Circle a;
      • a.x = 0;
      • a.y = 0;
      • a.radius = 10;
      • }
    17. Encapsulation
      • class Figure
      • {
      • protected :
      • int x, y;
      • };
      • class Circle : public Figure
      • {
      • public:
      • int radius;
      • };
      • int main()
      • {
      • Circle a;
      • a.x = 0;
      • a.y = 0;
      • a.radius = 10;
      • }
      example.cpp: In function ‘int main()’: example.cpp:5: error: ‘int Figure::x’ is protected example.cpp:17: error: within this context example.cpp:5: error: ‘int Figure::y’ is protected example.cpp:18: error: within this context
    18. Encapsulation
      • class Figure
      • {
      • protected:
      • int x_, y_;
      • };
      • class Circle : public Figure
      • {
      • private:
      • int radius_;
      • public:
      • Circle(int x, int y, int radius);
      • };
      • Circle::Circle(int x, int y, int radius)
      • {
      • x_ = x;
      • y_ = y;
      • radius_ = radius;
      • }
      • int main()
      • {
      • Circle a(0,0,10);
      • }
    19. Encapsulation
      • class Figure
      • {
      • private :
      • int x_, y_;
      • };
      • class Circle : public Figure
      • {
      • private:
      • int radius_;
      • public:
      • Circle(int x, int y, int radius);
      • };
      • Circle::Circle(int x, int y, int radius)
      • {
      • x_ = x;
      • y_ = y;
      • radius_ = radius;
      • }
      • int main()
      • {
      • Circle a(0,0,10);
      • }
      example.cpp: In constructor ‘Circle::Circle(int, int, int)’: example.cpp:5: error: ‘int Figure::x_’ is private example.cpp:18: error: within this context example.cpp:5: error: ‘int Figure::y_’ is private example.cpp:19: error: within this context
    20. Encapsulation
      • class Figure
      • {
      • private:
      • int x_, y_;
      • public:
      • void SetX(int x);
      • void SetY(int y);
      • };
      • void Figure::SetX(int x)
      • {
      • x_ = x;
      • }
      • void Figure::SetY(int y)
      • {
      • y_ = y;
      • }
      • class Circle : public Figure
      • {
      • private:
      • int radius_;
      • public:
      • Circle(int x, int y, int radius);
      • };
      • Circle::Circle(int x, int y, int radius)
      • {
      • SetX(x);
      • SetY(y);
      • this->radius_ = radius;
      • }
      • int main()
      • {
      • Circle a(0,0,10);
      • }
    21. What is the result?
      • class Figure
      • {
      • public:
      • Figure() {
      • cout << &quot;Figure Constructor &quot;;
      • }
      • ~Figure() {
      • cout << &quot;Figure Destructor &quot;;
      • }
      • };
      • class Circle : public Figure
      • {
      • public:
      • Circle() {
      • cout << &quot;Circle Constructor &quot;;
      • }
      • ~Circle() {
      • cout << &quot;Circle Destructor &quot;;
      • }
      • };
      • int main()
      • {
      • Circle a;
      • }
    22. Inheritance and Constructors
      • When creating a object from derived class, also the member values of the base class must be initialized
      • Base constructor is called before the derived classes constructor
      • Destructors vice versa.
    23. Calling the Base Classes constructor
      • class Figure
      • {
      • public:
      • Figure() {
      • cout << &quot;Figure Constructor &quot;;
      • }
      • ~Figure() {
      • cout << &quot;Figure Destructor &quot;;
      • }
      • };
      • class Circle : public Figure
      • {
      • public:
      • Circle() : Figure() {
      • cout << &quot;Circle Constructor &quot;;
      • }
      • ~Circle() {
      • cout << &quot;Circle Destructor &quot;;
      • }
      • };
      • int main()
      • {
      • Circle a;
      • }
    24. Calling the Base Classes constructor
      • class Figure
      • {
      • private:
      • int x_, y_;
      • public:
      • Figure(int x, int y) : x_(x), y_(y) {
      • cout << &quot;Figure Constructor &quot;;
      • }
      • ~Figure() {
      • cout << &quot;Figure Destructor &quot;;
      • }
      • };
    25. Calling the Base Classes constructor
      • class Circle : public Figure
      • {
      • private:
      • double radius_;
      • public:
      • Circle(int x, int y, int radius) : Figure(x, y),
      • radius_(radius)
      • {
      • cout << &quot;Circle Constructor &quot;;
      • }
      • ~Circle() {
      • cout << &quot;Circle Destructor &quot;;
      • }
      • };
      • int main()
      • {
      • Circle a(0,0,5);
      • }
    26. Abstract Class
      • In C++, Abstract class is a class that has one abstract method
      • Abstract method is a method without implementation.
      • Abstract method is created by reserverd word &quot;virtual&quot;
    27. Example of Abstract class
      • class Figure
      • {
      • private:
      • int x_, y_;
      • public:
      • Figure(int x, int y) : x_(x), y_(y) {
      • cout << &quot;Figure Constructor &quot;;
      • }
      • ~Figure() {
      • cout << &quot;Figure Destructor &quot;;
      • }
      • virtual double calculateArea() = 0;
      • };
    28. Example of Abstract class
      • class Circle : public Figure
      • {
      • private:
      • double radius_;
      • public:
      • Circle(int x, int y, int radius) : Figure(x, y),
      • radius_(radius)
      • {
      • cout << &quot;Circle Constructor &quot;;
      • }
      • ~Circle() {
      • cout << &quot;Circle Destructor &quot;;
      • }
      • double calculateArea() {
      • return 3.14 * radius_ * radius_;
      • }
      • };
    29. Example of Abstract class
      • int main()
      • {
      • Circle a(0,0,5);
      • cout << a.calculateArea() << endl;
      • // This Does not work, since figure is abstract:
      • // Figure f(0,0);
      • }

    + Tampere University of Applied SciencesTampere University of Applied Sciences, 2 years ago

    custom

    2310 views, 0 favs, 1 embeds more stats

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2310
      • 2238 on SlideShare
      • 72 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 39
    Most viewed embeds
    • 72 views on http://php.tpu.fi

    more

    All embeds
    • 72 views on http://php.tpu.fi

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories