C++ Inheritance

16,545 views

Published on

Published in: Education, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
16,545
On SlideShare
0
From Embeds
0
Number of Embeds
94
Actions
Shares
0
Downloads
808
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

C++ Inheritance

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

×