Successfully reported this slideshow.

C++: inheritance, composition, polymorphism

39,860 views

Published on

Published in: Education, Technology
  • ေအးပါကြာ :(
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi . this slide very useful for me . how can i Download this slide . please email to me ehsan.gha@gmail.com
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Unlike your other slide shows, this one is not downloadable. If you will allow a download, please give me the url. tldeglow@gmail.com. Thanks in advance.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • yap
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

C++: inheritance, composition, polymorphism

  1. 1. C++: Composition, Aggregation and Polymorphism Jussi Pohjolainen TAMK University of Applied Sciences
  2. 2. 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. Basic example <ul><li>Programmer inherites all the methods and attributes from Human </li></ul>Human string name void sleep() void drink() void eat() Programmer int salary void implementApps() void beNerd()
  4. 4. 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>
  5. 5. Example <<abstract>> Mammal string name void makesound() {abstract} Elephant int trunkLength makesound()
  6. 6. COMPOSITION AND AGGREGATION
  7. 7. Composition <ul><li>Relatioship between objects, where one object owns, or has the other object </li></ul><ul><li>Car has or owns Motor </li></ul><ul><li>When Car is build, it's motor is built also </li></ul><ul><li>When Car is destroyed it's motor is destroyed </li></ul>
  8. 8. UML notation
  9. 9. C++: Composition <ul><li>// Composition </li></ul><ul><li>class Car </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>Motor* motor; </li></ul><ul><li>public: </li></ul><ul><li>Car() { motor = new Motor(); } </li></ul><ul><li>~Car() { delete motor; } </li></ul><ul><li>}; </li></ul>
  10. 10. Aggregation <ul><li>In composition, when the owning object is destroyed, so are the contained objects </li></ul><ul><li>In aggregation this is not neccessarily true </li></ul><ul><li>Aggregation is a form of composition, where contained objects can still &quot;live&quot; after the owning object is released </li></ul><ul><li>Departments has professors, if department is closed, professors still live... </li></ul>
  11. 11. UML Notation
  12. 12. C++: Simple Aggregation <ul><li>// Aggregation </li></ul><ul><li>class Deparment </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>Professor* professor; </li></ul><ul><li>public: </li></ul><ul><li>Department(Professor* prof) : professor(prof) { } </li></ul><ul><li>~Department() { } </li></ul><ul><li>}; </li></ul>
  13. 13. One to Many?
  14. 14. C++: One to Many <ul><li>class Department </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>Professor* members[20]; </li></ul><ul><li>int numberOfMembers; </li></ul><ul><li>public: </li></ul><ul><li>Department(Professor* prof) { </li></ul><ul><li>members[0] = prof; </li></ul><ul><li>numberOfMembers = 1; </li></ul><ul><li>} </li></ul><ul><li>void addProfessor(Professor* prof) { </li></ul><ul><li>members[numberOfMembers] = prof; </li></ul><ul><li>numberOfMembers++; </li></ul><ul><li>} </li></ul><ul><li>~Department() { } </li></ul><ul><li>}; </li></ul>
  15. 15. VECTOR: DYNAMIC ARRAY
  16. 16. One to Many? <ul><li>In the previous code example department had 1 – 20 professors </li></ul><ul><li>What if we do not want to specify the maximum number? </li></ul><ul><li>What if we want that the deparment has 1 – x professors and the number can be determined during runtime? </li></ul>
  17. 17. Vector <ul><li>Vector is just like an array... </li></ul><ul><ul><li>...But it is dynamic array </li></ul></ul><ul><ul><li>This means that it can grow dynamically </li></ul></ul><ul><li>You do not have to specify the size for the array </li></ul><ul><li>Vector is a class from c++ standard library , just like string </li></ul><ul><ul><li>#include <vector> </li></ul></ul>
  18. 18. Vector Possibilities <ul><li>Add and remove elements </li></ul><ul><li>Iterate the elements </li></ul><ul><li>Access individual elements </li></ul>
  19. 19. Simple Example <ul><li>vector<int> numberArray; </li></ul><ul><li>int a = 1; </li></ul><ul><li>int b = 2; </li></ul><ul><li>int c = 3; </li></ul><ul><li>numberArray.push_back(1); </li></ul><ul><li>numberArray.push_back(2); </li></ul><ul><li>numberArray.push_back(3); </li></ul><ul><li>for( int i=0; i < numberArray.size(); i++ ) </li></ul><ul><li>{ </li></ul><ul><li>cout << numberArray.at(i) << endl; </li></ul><ul><li>} </li></ul>
  20. 20. When the elements are created dynamically <ul><li>vector< int* > numberArray; </li></ul><ul><li>int* a = new int(1); </li></ul><ul><li>int* b = new int(2); </li></ul><ul><li>int* c = new int(3); </li></ul><ul><li>numberArray.push_back(a); </li></ul><ul><li>numberArray.push_back(b); </li></ul><ul><li>numberArray.push_back(c); </li></ul><ul><li>for(int i=0; i<numberArray.size(); i++) </li></ul><ul><li>cout << *numberArray.at(i) << endl; </li></ul><ul><li>for(int i=0; i<numberArray.size(); i++) </li></ul><ul><li>delete numberArray.at(i); </li></ul>
  21. 21. When the elements are created dynamically – part 2 <ul><li>vector<int*> numberArray; </li></ul><ul><li>numberArray.push_back(new int(1)); </li></ul><ul><li>numberArray.push_back(new int(2)); </li></ul><ul><li>numberArray.push_back(new int(3)); </li></ul><ul><li>for(int i=0; i<numberArray.size(); i++) </li></ul><ul><li>cout << *numberArray.at(i) << endl; </li></ul><ul><li>for(int i=0; i<numberArray.size(); i++) </li></ul><ul><li>delete numberArray.at(i); </li></ul>
  22. 22. When vector is created dynamically <ul><li>vector<int>* numberArray = new vector<int>(); </li></ul><ul><li>numberArray->push_back(1); </li></ul><ul><li>numberArray->push_back(2); </li></ul><ul><li>numberArray->push_back(3); </li></ul><ul><li>for(int i=0; i<numberArray->size(); i++) </li></ul><ul><li>{ </li></ul><ul><li>cout << numberArray->at(i) << endl; </li></ul><ul><li>} </li></ul><ul><li>delete numberArray; </li></ul>
  23. 23. vector and elements dynamically <ul><li>vector<int*>* numberArray = new vector<int*>(); </li></ul><ul><li>numberArray->push_back( new int(1) ); </li></ul><ul><li>numberArray->push_back( new int(2) ); </li></ul><ul><li>numberArray->push_back( new int(3) ); </li></ul><ul><li>for(int i=0; i<numberArray->size(); i++) </li></ul><ul><li>cout << *numberArray->at(i) << endl; </li></ul><ul><li>for(int i=0; i<numberArray->size(); i++) </li></ul><ul><li>delete numberArray->at(i); </li></ul><ul><li>delete numberArray; </li></ul>
  24. 24. vector methods <ul><li>size </li></ul><ul><li>operator[] </li></ul><ul><li>push_back </li></ul><ul><li>pop_back </li></ul><ul><li>insert </li></ul><ul><li>erase </li></ul><ul><li>swap </li></ul><ul><li>See: </li></ul><ul><ul><li>http://www.cplusplus.com/reference/stl/vector/ </li></ul></ul>
  25. 25. C++: One to Many <ul><li>class Department </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>vector<Professor*>* members; </li></ul><ul><li>public: </li></ul><ul><li>Department(Professor* prof) { </li></ul><ul><li>members = new Vector<Professor*>(); </li></ul><ul><li>members->push_back(prof); </li></ul><ul><li>} </li></ul><ul><li>void addProfessor(Professor* prof) { </li></ul><ul><li>members->push_back(prof); </li></ul><ul><li>} </li></ul><ul><li>~Department() { delete members; } </li></ul><ul><li>}; </li></ul>
  26. 26. POLYMORPHISM
  27. 27. Introduction to Polymorphism <ul><li>// This creates a pointer a, which has a memory address </li></ul><ul><li>Dog* a; </li></ul><ul><li>// This creates a new object to dynamic memory and returns memory address of the object, which is stored in a </li></ul><ul><li>a = new Dog(); </li></ul>
  28. 28. Class Diagram
  29. 29. Pointer Type differs from Object Type? <ul><li>// This creates a pointer a, which has a memory address </li></ul><ul><li>Mammal* a; </li></ul><ul><li>// This creates a new object to dynamic memory and returns memory address of the object, which is stored in a </li></ul><ul><li>a = new Dog(); </li></ul>
  30. 30. Pointer type vs. Object type <ul><li>When declaring a new Pointer </li></ul><ul><ul><li>Mammal* pointer; </li></ul></ul><ul><li>This pointer can have reference to Mammal and all of it's derived classes! </li></ul><ul><li>So this is acceptible: </li></ul><ul><ul><li>Mammal* pointer = new Mammal(); </li></ul></ul><ul><ul><li>Mammal* pointer = new Dog(); </li></ul></ul><ul><ul><li>Mammal* pointer = new Human(); </li></ul></ul><ul><ul><li>Mammal* pointer = new Cat(); </li></ul></ul>
  31. 31. C++ <ul><li>#include <iostream> </li></ul><ul><li>using namespace std; </li></ul><ul><li>class Mammal { }; </li></ul><ul><li>class Human : public Mammal { }; </li></ul><ul><li>class Dog : public Mammal { }; </li></ul><ul><li>class Cat : public Mammal { }; </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Mammal* pointer1 = new Mammal(); </li></ul><ul><li>Mammal* pointer2 = new Human(); </li></ul><ul><li>Mammal* pointer3 = new Dog(); </li></ul><ul><li>Mammal* pointer4 = new Cat(); </li></ul><ul><li>// and remember to delete above: </li></ul><ul><li>delete pointer1; delete ... </li></ul>
  32. 32. Array <ul><li>class Mammal { }; </li></ul><ul><li>class Human : public Mammal { }; </li></ul><ul><li>class Dog : public Mammal { }; </li></ul><ul><li>class Cat : public Mammal { }; </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Mammal array[2]; </li></ul><ul><li>Dog musti; </li></ul><ul><li>Human pekka; </li></ul><ul><li>array[0] = musti; </li></ul><ul><li>array[1] = pekka; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  33. 33. vector <ul><li>class Mammal { }; </li></ul><ul><li>class Human : public Mammal { }; </li></ul><ul><li>class Dog : public Mammal { }; </li></ul><ul><li>class Cat : public Mammal { }; </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>vector<Mammal> dynamicArray; </li></ul><ul><li>Dog musti; </li></ul><ul><li>Human pekka; </li></ul><ul><li>dynamicArray.push_back(musti); </li></ul><ul><li>dynamicArray.push_back(pekka); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  34. 34. vector: Everything into dynamic memory <ul><li>vector<Mammal*>* dynamicArray = new vector<Mammal*>(); </li></ul><ul><li>dynamicArray->push_back( new Dog() ); </li></ul><ul><li>dynamicArray->push_back( new Human() ); </li></ul><ul><li>dynamicArray->push_back( new Mammal() ); </li></ul><ul><li>dynamicArray->push_back( new Cat() ); </li></ul><ul><li>for(int i=0; i < dynamicArray->size(); i++) </li></ul><ul><li>delete dynamicArray->at(i); </li></ul><ul><li>delete dynamicArray; </li></ul>
  35. 35. Problem? <ul><li>void function(Mammal* m) { </li></ul><ul><ul><li>// m may hold Dog, Cat or </li></ul></ul><ul><ul><li>// Human.. </li></ul></ul><ul><ul><li>m->Bark(); </li></ul></ul><ul><li>} </li></ul>
  36. 36. Casting <ul><li>void function(Mammal* m) { </li></ul><ul><ul><li>Dog* dog = dynamic_cast<Dog*>(m); </li></ul></ul><ul><ul><li>dog->Bark(); </li></ul></ul><ul><li>} </li></ul>
  37. 37. Type info <ul><li>#include <typeinfo> </li></ul><ul><li>void function(Mammal* m) { </li></ul><ul><ul><li>Dog* temp = new Dog(); </li></ul></ul><ul><ul><li>if(typeid(*temp) == typeid(*m)) { </li></ul></ul><ul><ul><li>delete temp; </li></ul></ul><ul><ul><li>Dog* dog= dynamic_cast<Dog*>(m); </li></ul></ul><ul><ul><li>dog->Bark(); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  38. 38. Class Diagram
  39. 39. Zoo-example <ul><li>class Zoo </li></ul><ul><li>{ </li></ul><ul><li>private: </li></ul><ul><li>vector<Animal*>* animals; </li></ul><ul><li>public: </li></ul><ul><li>Zoo() { </li></ul><ul><li>animals = new Vector<Animal*>(); </li></ul><ul><li>} </li></ul><ul><li>void addAnimal(Animal* animal) { </li></ul><ul><li>animals->push_back(animal); </li></ul><ul><li>} </li></ul><ul><li>~Zoo() { delete animals; } </li></ul><ul><li>}; </li></ul>
  40. 40. Inheritance Example (1/2) <ul><li>class Mammal { </li></ul><ul><li>protected: </li></ul><ul><li>string name_; </li></ul><ul><li>public: </li></ul><ul><li>void SetName(string name) { </li></ul><ul><li>name_ = name; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class Human : public Mammal { }; </li></ul><ul><li>class Dog : public Mammal { }; </li></ul><ul><li>class Cat : public Mammal { }; </li></ul>
  41. 41. Inheritance Example (2/2) <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Human pekka; </li></ul><ul><li>pekka.SetName(&quot;Pekka&quot;); </li></ul><ul><li>Dog musti; </li></ul><ul><li>musti.SetName(&quot;Musti&quot;); </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  42. 42. Class Diagram
  43. 43. C++ <ul><li>class Mammal { </li></ul><ul><li>protected: </li></ul><ul><li>string name_; </li></ul><ul><li>public: </li></ul><ul><li>void SetName(string name) { </li></ul><ul><li>name_ = name; </li></ul><ul><li>} </li></ul><ul><li>void PrintInformation() { </li></ul><ul><li>cout << &quot;Hello, I'm mammal and my name is &quot; << name_ << endl; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class Human : public Mammal { </li></ul><ul><li>public: </li></ul><ul><li>void PrintInformation() { </li></ul><ul><li>cout << &quot;Hello, I'm human and my name is &quot; << name_ << endl; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>
  44. 44. C++ <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Human* pekka = new Human(); </li></ul><ul><li>pekka->SetName(&quot;Pekka&quot;); </li></ul><ul><li>// prints </li></ul><ul><li>// Hello, I'm human and my name is Pekka </li></ul><ul><li>pekka->PrintInformation(); </li></ul><ul><li>delete pekka; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  45. 45. What about now? <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Mammal* pekka = new Human(); </li></ul><ul><li>pekka->SetName(&quot;Pekka&quot;); </li></ul><ul><li>// prints </li></ul><ul><li>// Hello, I'm mammal and my name is Pekka </li></ul><ul><li>pekka->PrintInformation(); </li></ul><ul><li>delete pekka </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  46. 46. Fix <ul><li>class Mammal { </li></ul><ul><li>protected: </li></ul><ul><li>string name_; </li></ul><ul><li>public: </li></ul><ul><li>void SetName(string name) { </li></ul><ul><li>name_ = name; </li></ul><ul><li>} </li></ul><ul><li>virtual void PrintInformation() { </li></ul><ul><li>cout << &quot;Hello, I'm mammal and my name is &quot; << name_ << endl; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class Human : public Mammal { </li></ul><ul><li>public: </li></ul><ul><li>void PrintInformation() { </li></ul><ul><li>cout << &quot;Hello, I'm human and my name is &quot; << name_ << endl; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>
  47. 47. What about now? <ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Mammal* pekka = new Human(); </li></ul><ul><li>pekka->SetName(&quot;Pekka&quot;); </li></ul><ul><li>// prints </li></ul><ul><li>// Hello, I'm human and my name is Pekka </li></ul><ul><li>pekka->PrintInformation(); </li></ul><ul><li>delete pekka </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  48. 48. Without Virtual <ul><li>A* p1 = new A(); </li></ul><ul><li>p1->method(); </li></ul><ul><li>B* p2 = new B(); </li></ul><ul><li>p2->method(); </li></ul><ul><li>A* p3 = new B(); </li></ul><ul><li>p3->method(); </li></ul>
  49. 49. With Virtual <ul><li>A* p1 = new A(); </li></ul><ul><li>p1->method(); </li></ul><ul><li>B* p2 = new B(); </li></ul><ul><li>p2->method(); </li></ul><ul><li>A* p3 = new B(); </li></ul><ul><li>p3->method(); </li></ul>
  50. 50. Virtual in Destructors <ul><li>class Mammal { </li></ul><ul><li>public: </li></ul><ul><li>Mammal() { </li></ul><ul><li>cout << &quot;Mammal Constructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>~Mammal() { </li></ul><ul><li>cout << &quot;Mammal Destructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class Human : public Mammal { </li></ul><ul><li>public: </li></ul><ul><li>Human() { </li></ul><ul><li>cout << &quot;Human Constructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>~Human() { </li></ul><ul><li>cout << &quot;Human Destructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Mammal* pekka = new Human(); delete pekka; Result: Mammal Constructor Human Constructor Mammal Destructor
  51. 51. Virtual in Destructors <ul><li>class Mammal { </li></ul><ul><li>public: </li></ul><ul><li>Mammal() { </li></ul><ul><li>cout << &quot;Mammal Constructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>virtual ~Mammal() { </li></ul><ul><li>cout << &quot;Mammal Destructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>class Human : public Mammal { </li></ul><ul><li>public: </li></ul><ul><li>Human() { </li></ul><ul><li>cout << &quot;Human Constructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>~Human() { </li></ul><ul><li>cout << &quot;Human Destructor &quot;; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul>Mammal* pekka = new Human(); delete pekka; Result: Mammal Constructor Human Constructor Human Destructor Mammal Destructor
  52. 52. Problem <ul><li>class Vehicle { }; </li></ul><ul><li>class Motor { }; </li></ul><ul><li>class Car : public Vehicle { </li></ul><ul><li>private: </li></ul><ul><li>Motor* motor; </li></ul><ul><li>public: </li></ul><ul><li>Car() { </li></ul><ul><li>motor = new Motor(); </li></ul><ul><li>} </li></ul><ul><li>~Car() { </li></ul><ul><li>delete motor; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Vehicle* datsun = new Car(); </li></ul><ul><li>delete datsun; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>
  53. 53. Solution <ul><li>class Vehicle { </li></ul><ul><li>public: </li></ul><ul><li>virtual ~Vehicle() { } </li></ul><ul><li>}; </li></ul><ul><li>class Motor { }; </li></ul><ul><li>class Car : public Vehicle { </li></ul><ul><li>private: </li></ul><ul><li>Motor* motor; </li></ul><ul><li>public: </li></ul><ul><li>Car() { </li></ul><ul><li>motor = new Motor(); </li></ul><ul><li>} </li></ul><ul><li>~Car() { </li></ul><ul><li>delete motor; </li></ul><ul><li>} </li></ul><ul><li>}; </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><li>Vehicle* datsun = new Car(); </li></ul><ul><li>delete datsun; </li></ul><ul><li>return 0; </li></ul><ul><li>} </li></ul>

×