1 / 323

C++ Training Datascope Lawrence D’Antonio

C++ Training Datascope Lawrence D’Antonio. Lecture 8 An Overview of C++: What is Polymorphism? – Subtype Polymorphism and Inheritance. Do not multiply objects without necessity W. Occam. What is polymorphism?. Parametric. Universal. Subtype. Polymorphism. Overloading. Ad-hoc.

jolie
Download Presentation

C++ Training Datascope Lawrence D’Antonio

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C++ TrainingDatascopeLawrence D’Antonio Lecture 8 An Overview of C++: What is Polymorphism? – Subtype Polymorphism and Inheritance

  2. Do not multiply objects without necessity W. Occam

  3. What is polymorphism? Parametric Universal Subtype Polymorphism Overloading Ad-hoc Coercion Different types of objects respond to the same message and use the appropriate method.

  4. Subtype polymorphism • Subtype (or inclusion) polymorphism allows objects of a given type to be substituted for by objects of a subtype.

  5. What is inheritance? • One class (derived/child) relies on the definition of another class (base/parent). • Single vs. multiple inheritance • A method of sharing code or sharing interface among classes. • Language may define a class tree (with single root): Java, Smalltalk • Language may define a class forest: C++

  6. What does a child inherit? • A child class inherits all of the features of the parent except for the following: • Constructors/destructor • The assignment operator • Friend functions and classes

  7. Access and Inheritance • The following table gives for each type of inheritance, for a member of given access in the parent class what access that member has in the child class.

  8. Forms of inheritance • Single: Derived class has one parent • Multiple: Derived class has more than one parent. • Interface: Defines how a class may be implemented. • Mixin: A fragment of a class that is meant to be composed with another.

  9. Three views of inheritance • Code sharing – one defined structure can be incorporated into another. • Logical relationship – one class has the relationship of generalization or specialization with respect to the other. • Type – a subtype can be created given the definition of a supertype.

  10. Reasons for using inheritance • Code reuse – The child inherits features from the parent, hence the code doesn’t need to be rewritten. • Concept reuse – The child overrides features of the parent. No code is shared, but the semantics of the method is maintained.

  11. Inheritance in different languages • C++ class B: public A {}; • C# class B: A {} • CLOS (defclass B (A) () )

  12. Inheritance part 2 • Java class B extends A {} • Object Pascal type B = object(A) … end;

  13. Inheritance part 3 • Python class B(A): def __init__ (self): … • Ruby class B < A … end

  14. Inheritance part 4 • Eiffel class B inherit A … end • Ada type B is new A with …

  15. Inheritance part 5 • Objective C @interface B: A … @end • Dylan define class <B> (<A>) … end class <B>

  16. Direct vs. Indirect Base • A base class is direct if it is explicitly mentioned as a base class in the declaration of the child class. • An indirect base class is a base class that is not mentioned explicitly in the child class declaration, but is available to the child class through one of its base classes.

  17. Example • Direct base: class A is a direct base of class B class A {}; class B: public A {};

  18. Example • Indirect base: class A is an indirect base of class C. class A {}; class B: public A {}; class C: public B {};

  19. Example • In this example, class A is both a direct and indirect base of class C. class A {}; class B: public A {}; class C: public A, public B {};

  20. Constructors/Destructors • The child class either must directly call the parent’s constructor or else the parent must have a default constructor. • Base classes are initialized in the order of declaration. • Destructors are called in reverse order of the constructors.

  21. Example class A { public: A() {} }; class B: public A { public: B() {} };

  22. This example is valid because class A has a default constructor. The constructor for class B will, in effect, call this constructor.

  23. Example class A { public: A(int) {} }; class B: public A { public: B() {} };

  24. This is illegal. Since class A has a constructor A(int), this means that A will not have a default constructor written for it. Thus A’s constructor is not called by B’s constructor. This is then an error.

  25. Example class A { public: A(int) {} }; class B: public A { public: B(): A(5) {} };

  26. This is legal. B’s constructor explicitly calls A’s constructor.

  27. Order of Initialization • Virtual base classes in the order of declaration. • Nonvirtual base classes in the order of declaration. • Class members are initialized in order of declaration. • The body of the constructor is then executed.

  28. Order of Destruction • The destructor for a class object is called before destructors for members and bases are called. • Destructors for nonstatic members are called before destructors for base classes are called. • Destructors for nonvirtual bases are called before destructors for virtual base classes.

  29. Example class Employee { std::string first_name, last_name, ssn; public: //… }; class Manager: public Employee { std::set<Employee*> group; public: short level; };

  30. Employee: first name: last name: ssn: Manager: first name: last name: ssn: group: level:

  31. Is-A Relationship • A Manager is a type of Employee. So any expression accepting a valid pointer or reference to a base object may accept a pointer or reference to a child object.

  32. Example void f(const Employee & re, const Manager &rm) { std::list<Employee *> le; le.push_front(&re); le.push_front(&rm); }

  33. Explanation • Manager “is-an” Employee. • Manager* can be used as an Employee* (likewise for references). • Employee* cannot be used as a Manager* without a cast.

  34. Is this legal? class Employee { std::string first_name, last_name, ssn; }; class Manager: public Employee { std::set<Employee*> group; public: short level; }; void foo(Manager &m, Employee &e) { Employee *pe = &m; Manager *pm = &e; pm->level = 2; pm = static_cast<Manager*>(pe); pm->level = 2; } main() { Manager m; Employee e; foo(m,e); };

  35. Yes and no. Employee *pe = &m; //OK, Manager is Employee Manager *pm = &e; //Not legal, Employee is not a Manager pm->level = 2; //Not legal, e doesn’t have a level pm = static_cast<Manager*>(pe); //Works because pe points to Manager m pm->level = 2; //Works because pm points to Manager m that has a //level.

  36. Overriding • Base class class Employee { std::string first_name, last_name; public: void print() const; }; void Employee::print() const { std::cout << "Employee\n"; }

  37. Derived class • class Manager: public Employee { • std::set<Employee*> group; • short level; • public: • void print() const; • }; • void Manager::print() const • { • Employee::print(); • std::cout << "Manager\n"; • }

  38. What’s the output? Employee e; Manager m; Employee *pe1 = &e; Employee *pe2 = &m; Manager *pm = &m; pe1->print(); pe2->print(); pm->print(); pm->Employee::print();

  39. Output Employee //pe1->print() Employee //pe2->print() Employee //pm->print() Manager //pm->print() Employee //pm->Employee::print()

  40. What’s the output? class A { public: A(int x) { std::cout << "A::x = " << x << '\n'; } }; class B: public A { public: B(int x): A(x-1) { std::cout << "B::x = " << x << '\n'; } }; class C: public A, public B { public: C(int x): A(x+1), B(x-1) { std::cout << "C::x = " << x << '\n'; } }; C c(5);

  41. Output A::x = 6 //C::A A::x = 3 //C::B::A B::x = 4 //C::B C::x = 5 //C

  42. Slicing • In copying from a child to a parent object, only the data of the parent subobject of the child is copied.

  43. Slicing Example class A { int x; public: A(int a): x(a) {} }; class B: public A { int y; public: B(int a, int b): A(a), y(b) {} }; main () { B b(3,4); A a = b; a = b; }

  44. What happened? A a = b; //Calls A(const A&), //only copies b.x a = b; //Calls A::operator=(const A&) //only copies b.x

  45. Class Hierarchy class Employee {}; //Base class class Manager: public Employee {}; class Director: public Manager {}; class Temporary {}; //Base class class Secretary: public Employee {}; class TempSec: public Temporary, public Secretary {}; class Consultant: public Temporary, public Manager {};

  46. Temporary Employee Secretary Manager TempSec Consultant Director

  47. Class Hierarchy Structure • A class hierarchy forms a DAG (directed acyclic graph). • The graph need not be a tree. For example, in the graph below there are two paths from D to A. A B C D

  48. Type fields • A simple, but usually inadequate method to create polymorphism is to use type fields. • A type field is a data type (usually an enum) that is placed in the base class for functions to use.

  49. Type field example struct Employee { enum E_type {E,M}; E_type type; Employee(): type(E) {} }; struct Manager: public Employee { Manager() { type = M; } };

More Related