Chapter 15 Inheritance Polymorphism and Virtual Functions Copyright
![Chapter 15: Inheritance, Polymorphism, and Virtual Functions Copyright © 2009 Pearson Education, Inc. Copyright Chapter 15: Inheritance, Polymorphism, and Virtual Functions Copyright © 2009 Pearson Education, Inc. Copyright](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-1.jpg)
![15. 1 • What Is Inheritance? Copyright © 2009 Pearson Education, Inc. Publishing as 15. 1 • What Is Inheritance? Copyright © 2009 Pearson Education, Inc. Publishing as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-2.jpg)
![What Is Inheritance? • Provides a way to create a new class from an What Is Inheritance? • Provides a way to create a new class from an](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-3.jpg)
![Example: Insects Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4 Example: Insects Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-4.jpg)
![The "is a" Relationship • Inheritance establishes an "is a" relationship between classes. – The "is a" Relationship • Inheritance establishes an "is a" relationship between classes. –](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-5.jpg)
![Inheritance – Terminology and Notation • Base class (or parent) – inherited from • Inheritance – Terminology and Notation • Base class (or parent) – inherited from •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-6.jpg)
![Back to the ‘is a’ Relationship • An object of a derived class 'is Back to the ‘is a’ Relationship • An object of a derived class 'is](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-7.jpg)
![What Does a Child Have? An object of the derived class has: • all What Does a Child Have? An object of the derived class has: • all](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-8.jpg)
![15. 2 • Protected Members and Class Access Copyright © 2009 Pearson Education, Inc. 15. 2 • Protected Members and Class Access Copyright © 2009 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-9.jpg)
![Protected Members and Class Access • protected member access specification: like private, but accessible Protected Members and Class Access • protected member access specification: like private, but accessible](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-10.jpg)
![Class Access Specifiers 1) public – object of derived class can be treated as Class Access Specifiers 1) public – object of derived class can be treated as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-11.jpg)
![Inheritance vs. Access Base class members How inherited base class members appear in derived Inheritance vs. Access Base class members How inherited base class members appear in derived](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-12.jpg)
![More Inheritance vs. Access class Grade private members: char letter; float score; void calc. More Inheritance vs. Access class Grade private members: char letter; float score; void calc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-13.jpg)
![More Inheritance vs. Access (2) class Grade private members: char letter; float score; void More Inheritance vs. Access (2) class Grade private members: char letter; float score; void](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-14.jpg)
![More Inheritance vs. Access (3) class Grade private members: char letter; float score; void More Inheritance vs. Access (3) class Grade private members: char letter; float score; void](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-15.jpg)
![15. 3 • Constructors and Destructors in Base and Derived Classes Copyright © 2009 15. 3 • Constructors and Destructors in Base and Derived Classes Copyright © 2009](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-16.jpg)
![Constructors and Destructors in Base and Derived Classes • Derived classes can have their Constructors and Destructors in Base and Derived Classes • Derived classes can have their](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-17.jpg)
![Constructors and Destructors in Base and Derived Classes Copyright © 2009 Pearson Education, Inc. Constructors and Destructors in Base and Derived Classes Copyright © 2009 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-18.jpg)
![Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-19.jpg)
![Program 5 -14 (Continued) Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Program 5 -14 (Continued) Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-20.jpg)
![Passing Arguments to Base Class Constructor • Allows selection between multiple base class constructors Passing Arguments to Base Class Constructor • Allows selection between multiple base class constructors](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-21.jpg)
![Passing Arguments to Base Class Constructor derived class constructor base class constructor Square: : Passing Arguments to Base Class Constructor derived class constructor base class constructor Square: :](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-22.jpg)
![15. 4 • Redefining Base Class Functions Copyright © 2009 Pearson Education, Inc. Publishing 15. 4 • Redefining Base Class Functions Copyright © 2009 Pearson Education, Inc. Publishing](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-23.jpg)
![Redefining Base Class Functions • Redefining function: function in a derived class that has Redefining Base Class Functions • Redefining function: function in a derived class that has](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-24.jpg)
![Redefining Base Class Functions • Not the same as overloading – with overloading, parameter Redefining Base Class Functions • Not the same as overloading – with overloading, parameter](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-25.jpg)
![Base Class Note set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as Base Class Note set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-26.jpg)
![Derived Class Redefined set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as Derived Class Redefined set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-27.jpg)
![From Program 15 -6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley From Program 15 -6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-28.jpg)
![Problem with Redefining • Consider this situation: – Class Base. Class defines functions x() Problem with Redefining • Consider this situation: – Class Base. Class defines functions x()](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-29.jpg)
![Problem with Redefining Base. Class void X(); void Y(); Derived. Class D; D. X(); Problem with Redefining Base. Class void X(); void Y(); Derived. Class D; D. X();](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-30.jpg)
![15. 5 • Class Hierarchies Copyright © 2009 Pearson Education, Inc. Publishing as Pearson 15. 5 • Class Hierarchies Copyright © 2009 Pearson Education, Inc. Publishing as Pearson](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-31.jpg)
![Class Hierarchies • A base class can be derived from another base class. Copyright Class Hierarchies • A base class can be derived from another base class. Copyright](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-32.jpg)
![Class Hierarchies • Consider the Graded. Activity, Final. Exam, Pass. Fail. Activity, Pass. Fail. Class Hierarchies • Consider the Graded. Activity, Final. Exam, Pass. Fail. Activity, Pass. Fail.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-33.jpg)
![15. 6 • Polymorphism and Virtual Member Functions Copyright © 2009 Pearson Education, Inc. 15. 6 • Polymorphism and Virtual Member Functions Copyright © 2009 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-34.jpg)
![Polymorphism and Virtual Member Functions • Virtual member function: function in base class that Polymorphism and Virtual Member Functions • Virtual member function: function in base class that](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-35.jpg)
![Consider this function (from Program 15 -9) Because the parameter in the display. Grade Consider this function (from Program 15 -9) Because the parameter in the display. Grade](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-36.jpg)
![Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-37.jpg)
![As you can see from the example output, the get. Letter. Grade member function As you can see from the example output, the get. Letter. Grade member function](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-38.jpg)
![Static Binding • Program 15 -9 displays 'C' instead of 'P' because the call Static Binding • Program 15 -9 displays 'C' instead of 'P' because the call](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-39.jpg)
![Virtual Functions • A virtual function is dynamically bound to calls at runtime. • Virtual Functions • A virtual function is dynamically bound to calls at runtime. •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-40.jpg)
![Virtual Functions • To make a function virtual, place the virtual key word before Virtual Functions • To make a function virtual, place the virtual key word before](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-41.jpg)
![Updated Version of Graded. Activity The function is now virtual. The function also becomes Updated Version of Graded. Activity The function is now virtual. The function also becomes](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-42.jpg)
![If we recompile our program with the updated versions of the classes, we will If we recompile our program with the updated versions of the classes, we will](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-43.jpg)
![Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-44.jpg)
![Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-45.jpg)
![Polymorphism Requires References or Pointers • Polymorphic behavior is only possible when an object Polymorphism Requires References or Pointers • Polymorphic behavior is only possible when an object](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-46.jpg)
![Base Class Pointers • Can define a pointer to a base class object • Base Class Pointers • Can define a pointer to a base class object •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-47.jpg)
![Base Class Pointers • Base class pointers and references only know about members of Base Class Pointers • Base class pointers and references only know about members of](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-48.jpg)
![Redefining vs. Overriding • In C++, redefined functions are statically bound and overridden functions Redefining vs. Overriding • In C++, redefined functions are statically bound and overridden functions](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-49.jpg)
![Virtual Destructors • It's a good idea to make destructors virtual if the class Virtual Destructors • It's a good idea to make destructors virtual if the class](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-50.jpg)
![15. 7 • Abstract Base Classes and Pure Virtual Functions Copyright © 2009 Pearson 15. 7 • Abstract Base Classes and Pure Virtual Functions Copyright © 2009 Pearson](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-51.jpg)
![Abstract Base Classes and Pure Virtual Functions • Pure virtual function: a virtual member Abstract Base Classes and Pure Virtual Functions • Pure virtual function: a virtual member](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-52.jpg)
![Abstract Base Classes and Pure Virtual Functions • Abstract base class: class that can Abstract Base Classes and Pure Virtual Functions • Abstract base class: class that can](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-53.jpg)
![15. 8 • Multiple Inheritance Copyright © 2009 Pearson Education, Inc. Publishing as Pearson 15. 8 • Multiple Inheritance Copyright © 2009 Pearson Education, Inc. Publishing as Pearson](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-54.jpg)
![Multiple Inheritance • A derived class can have more than one base class • Multiple Inheritance • A derived class can have more than one base class •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-55.jpg)
![Multiple Inheritance • Arguments can be passed to both base classes' constructors: cube: : Multiple Inheritance • Arguments can be passed to both base classes' constructors: cube: :](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-56.jpg)
![Multiple Inheritance • Problem: what if base classes have member variables/functions with the same Multiple Inheritance • Problem: what if base classes have member variables/functions with the same](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-57.jpg)
- Slides: 57
![Chapter 15 Inheritance Polymorphism and Virtual Functions Copyright 2009 Pearson Education Inc Copyright Chapter 15: Inheritance, Polymorphism, and Virtual Functions Copyright © 2009 Pearson Education, Inc. Copyright](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-1.jpg)
Chapter 15: Inheritance, Polymorphism, and Virtual Functions Copyright © 2009 Pearson Education, Inc. Copyright 2009 Addison-Wesley Pearson Education, Publishing as©Pearson Inc. Publishing as Pearson Addison-Wesley
![15 1 What Is Inheritance Copyright 2009 Pearson Education Inc Publishing as 15. 1 • What Is Inheritance? Copyright © 2009 Pearson Education, Inc. Publishing as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-2.jpg)
15. 1 • What Is Inheritance? Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![What Is Inheritance Provides a way to create a new class from an What Is Inheritance? • Provides a way to create a new class from an](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-3.jpg)
What Is Inheritance? • Provides a way to create a new class from an existing class • The new class is a specialized version of the existing class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3
![Example Insects Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley 4 Example: Insects Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-4.jpg)
Example: Insects Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4
![The is a Relationship Inheritance establishes an is a relationship between classes The "is a" Relationship • Inheritance establishes an "is a" relationship between classes. –](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-5.jpg)
The "is a" Relationship • Inheritance establishes an "is a" relationship between classes. – A poodle is a dog – A car is a vehicle – A flower is a plant – A football player is an athlete Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5
![Inheritance Terminology and Notation Base class or parent inherited from Inheritance – Terminology and Notation • Base class (or parent) – inherited from •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-6.jpg)
Inheritance – Terminology and Notation • Base class (or parent) – inherited from • Derived class (or child) – inherits from the base class • Notation: class Student // base class {. . . }; class Under. Grad : public student { // derived class. . . }; Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 6
![Back to the is a Relationship An object of a derived class is Back to the ‘is a’ Relationship • An object of a derived class 'is](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-7.jpg)
Back to the ‘is a’ Relationship • An object of a derived class 'is a(n)' object of the base class • Example: – an Under. Grad is a Student – a Mammal is an Animal • A derived object has all of the characteristics of the base class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 7
![What Does a Child Have An object of the derived class has all What Does a Child Have? An object of the derived class has: • all](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-8.jpg)
What Does a Child Have? An object of the derived class has: • all members defined in child class • all members declared in parent class An object of the derived class can use: • all public members defined in child class • all public members defined in parent class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 8
![15 2 Protected Members and Class Access Copyright 2009 Pearson Education Inc 15. 2 • Protected Members and Class Access Copyright © 2009 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-9.jpg)
15. 2 • Protected Members and Class Access Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Protected Members and Class Access protected member access specification like private but accessible Protected Members and Class Access • protected member access specification: like private, but accessible](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-10.jpg)
Protected Members and Class Access • protected member access specification: like private, but accessible by objects of derived class • Class access specification: determines how private, protected, and public members of base class are inherited by the derived class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10
![Class Access Specifiers 1 public object of derived class can be treated as Class Access Specifiers 1) public – object of derived class can be treated as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-11.jpg)
Class Access Specifiers 1) public – object of derived class can be treated as object of base class (not viceversa) 2) protected – more restrictive than public, but allows derived classes to know details of parents 3) private – prevents objects of derived class from being treated as objects of base class. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 11
![Inheritance vs Access Base class members How inherited base class members appear in derived Inheritance vs. Access Base class members How inherited base class members appear in derived](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-12.jpg)
Inheritance vs. Access Base class members How inherited base class members appear in derived class x is inaccessible private: y private: z private: x protected: y public: z private base class private: x protected: y public: z protected base class x is inaccessible protected: y protected: z public base class x is inaccessible protected: y public: z private: x protected: y public: z Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12
![More Inheritance vs Access class Grade private members char letter float score void calc More Inheritance vs. Access class Grade private members: char letter; float score; void calc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-13.jpg)
More Inheritance vs. Access class Grade private members: char letter; float score; void calc. Grade(); public members: void set. Score(float); float get. Score(); char get. Letter(); When Test class inherits from Grade class using public class access, it looks like this: Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley class Test : public Grade private members: int num. Questions; float points. Each; int num. Missed; public members: Test(int, int); private members: int num. Questions: float points. Each; int num. Missed; public members: Test(int, int); void set. Score(float); float get. Score(); float get. Letter(); 13
![More Inheritance vs Access 2 class Grade private members char letter float score void More Inheritance vs. Access (2) class Grade private members: char letter; float score; void](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-14.jpg)
More Inheritance vs. Access (2) class Grade private members: char letter; float score; void calc. Grade(); public members: void set. Score(float); float get. Score(); char get. Letter(); When Test class inherits from Grade class using protected class access, it looks like this: Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley class Test : protected Grade private members: int num. Questions; float points. Each; int num. Missed; public members: Test(int, int); private members: int num. Questions: float points. Each; int num. Missed; public members: Test(int, int); protected members: void set. Score(float); float get. Score(); float get. Letter(); 14
![More Inheritance vs Access 3 class Grade private members char letter float score void More Inheritance vs. Access (3) class Grade private members: char letter; float score; void](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-15.jpg)
More Inheritance vs. Access (3) class Grade private members: char letter; float score; void calc. Grade(); public members: void set. Score(float); float get. Score(); char get. Letter(); When Test class inherits from Grade class using private class access, it looks like this: Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley class Test : private Grade private members: int num. Questions; float points. Each; int num. Missed; public members: Test(int, int); private members: int num. Questions: float points. Each; int num. Missed; void set. Score(float); float get. Score(); float get. Letter(); public members: Test(int, int); 15
![15 3 Constructors and Destructors in Base and Derived Classes Copyright 2009 15. 3 • Constructors and Destructors in Base and Derived Classes Copyright © 2009](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-16.jpg)
15. 3 • Constructors and Destructors in Base and Derived Classes Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Constructors and Destructors in Base and Derived Classes Derived classes can have their Constructors and Destructors in Base and Derived Classes • Derived classes can have their](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-17.jpg)
Constructors and Destructors in Base and Derived Classes • Derived classes can have their own constructors and destructors • When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor • When an object of a derived class is destroyed, its destructor is called first, then that of the base class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 17
![Constructors and Destructors in Base and Derived Classes Copyright 2009 Pearson Education Inc Constructors and Destructors in Base and Derived Classes Copyright © 2009 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-18.jpg)
Constructors and Destructors in Base and Derived Classes Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 18
![Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley 19 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-19.jpg)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 19
![Program 5 14 Continued Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley Program 5 -14 (Continued) Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-20.jpg)
Program 5 -14 (Continued) Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 20
![Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Passing Arguments to Base Class Constructor • Allows selection between multiple base class constructors](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-21.jpg)
Passing Arguments to Base Class Constructor • Allows selection between multiple base class constructors • Specify arguments to base constructor on derived constructor heading: Square: : Square(int side) : Rectangle(side, side) • Can also be done with inline constructors • Must be done if base class has no default constructor Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 21
![Passing Arguments to Base Class Constructor derived class constructor base class constructor Square Passing Arguments to Base Class Constructor derived class constructor base class constructor Square: :](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-22.jpg)
Passing Arguments to Base Class Constructor derived class constructor base class constructor Square: : Square(int side): Rectangle(side, side) derived constructor parameter Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley base constructor parameters 22
![15 4 Redefining Base Class Functions Copyright 2009 Pearson Education Inc Publishing 15. 4 • Redefining Base Class Functions Copyright © 2009 Pearson Education, Inc. Publishing](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-23.jpg)
15. 4 • Redefining Base Class Functions Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Redefining Base Class Functions Redefining function function in a derived class that has Redefining Base Class Functions • Redefining function: function in a derived class that has](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-24.jpg)
Redefining Base Class Functions • Redefining function: function in a derived class that has the same name and parameter list as a function in the base class • Typically used to replace a function in base class with different actions in derived class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 24
![Redefining Base Class Functions Not the same as overloading with overloading parameter Redefining Base Class Functions • Not the same as overloading – with overloading, parameter](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-25.jpg)
Redefining Base Class Functions • Not the same as overloading – with overloading, parameter lists must be different • Objects of base class use base class version of function; objects of derived class use derived class version of function Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 25
![Base Class Note set Score function Copyright 2009 Pearson Education Inc Publishing as Base Class Note set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-26.jpg)
Base Class Note set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 26
![Derived Class Redefined set Score function Copyright 2009 Pearson Education Inc Publishing as Derived Class Redefined set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-27.jpg)
Derived Class Redefined set. Score function Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 27
![From Program 15 6 Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley From Program 15 -6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-28.jpg)
From Program 15 -6 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 28
![Problem with Redefining Consider this situation Class Base Class defines functions x Problem with Redefining • Consider this situation: – Class Base. Class defines functions x()](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-29.jpg)
Problem with Redefining • Consider this situation: – Class Base. Class defines functions x() and y(). x() calls y(). – Class Derived. Class inherits from Base. Class and redefines function y(). – An object D of class Derived. Class is created and function x() is called. – When x() is called, which y() is used, the one defined in Base. Class or the redefined one in Derived. Class? Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 29
![Problem with Redefining Base Class void X void Y Derived Class D D X Problem with Redefining Base. Class void X(); void Y(); Derived. Class D; D. X();](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-30.jpg)
Problem with Redefining Base. Class void X(); void Y(); Derived. Class D; D. X(); Object D invokes function X() In Base. Class. Function X() invokes function Y() in Base. Class, not function Y() in Derived. Class, because function calls are bound at compile time. This is static binding. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 30
![15 5 Class Hierarchies Copyright 2009 Pearson Education Inc Publishing as Pearson 15. 5 • Class Hierarchies Copyright © 2009 Pearson Education, Inc. Publishing as Pearson](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-31.jpg)
15. 5 • Class Hierarchies Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Class Hierarchies A base class can be derived from another base class Copyright Class Hierarchies • A base class can be derived from another base class. Copyright](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-32.jpg)
Class Hierarchies • A base class can be derived from another base class. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 32
![Class Hierarchies Consider the Graded Activity Final Exam Pass Fail Activity Pass Fail Class Hierarchies • Consider the Graded. Activity, Final. Exam, Pass. Fail. Activity, Pass. Fail.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-33.jpg)
Class Hierarchies • Consider the Graded. Activity, Final. Exam, Pass. Fail. Activity, Pass. Fail. Exam hierarchy in Chapter 15. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 33
![15 6 Polymorphism and Virtual Member Functions Copyright 2009 Pearson Education Inc 15. 6 • Polymorphism and Virtual Member Functions Copyright © 2009 Pearson Education, Inc.](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-34.jpg)
15. 6 • Polymorphism and Virtual Member Functions Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Polymorphism and Virtual Member Functions Virtual member function function in base class that Polymorphism and Virtual Member Functions • Virtual member function: function in base class that](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-35.jpg)
Polymorphism and Virtual Member Functions • Virtual member function: function in base class that expects to be redefined in derived class • Function defined with key word virtual: virtual void Y() {. . . } • Supports dynamic binding: functions bound at run time to function that they call • Without virtual member functions, C++ uses static (compile time) binding Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 35
![Consider this function from Program 15 9 Because the parameter in the display Grade Consider this function (from Program 15 -9) Because the parameter in the display. Grade](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-36.jpg)
Consider this function (from Program 15 -9) Because the parameter in the display. Grade function is a Graded. Activity reference variable, it can reference any object that is derived from Graded. Activity. That means we can pass a Graded. Activity object, a Final. Exam object, a Pass. Fail. Exam object, or any other object that is derived from Graded. Activity. A problem occurs in Program 15 -9 however. . . Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 36
![Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley 37 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-37.jpg)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 37
![As you can see from the example output the get Letter Grade member function As you can see from the example output, the get. Letter. Grade member function](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-38.jpg)
As you can see from the example output, the get. Letter. Grade member function returned ‘C’ instead of ‘P’. This is because the Graded. Activity class’s get. Letter. Grade function was executed instead of the Pass. Fail. Activity class’s version of the function. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 38
![Static Binding Program 15 9 displays C instead of P because the call Static Binding • Program 15 -9 displays 'C' instead of 'P' because the call](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-39.jpg)
Static Binding • Program 15 -9 displays 'C' instead of 'P' because the call to the get. Letter. Grade function is statically bound (at compile time) with the Graded. Activity class's version of the function. • We can remedy this by making the function virtual. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 39
![Virtual Functions A virtual function is dynamically bound to calls at runtime Virtual Functions • A virtual function is dynamically bound to calls at runtime. •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-40.jpg)
Virtual Functions • A virtual function is dynamically bound to calls at runtime. • At runtime, C++ determines the type of object making the call, and binds the function to the appropriate version of the function. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 40
![Virtual Functions To make a function virtual place the virtual key word before Virtual Functions • To make a function virtual, place the virtual key word before](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-41.jpg)
Virtual Functions • To make a function virtual, place the virtual key word before the return type in the base class's declaration: virtual char get. Letter. Grade() const; • The compiler will not bind the function to calls. Instead, the program will bind them at runtime. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 41
![Updated Version of Graded Activity The function is now virtual The function also becomes Updated Version of Graded. Activity The function is now virtual. The function also becomes](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-42.jpg)
Updated Version of Graded. Activity The function is now virtual. The function also becomes virtual in all derived classes automatically! Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 42
![If we recompile our program with the updated versions of the classes we will If we recompile our program with the updated versions of the classes, we will](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-43.jpg)
If we recompile our program with the updated versions of the classes, we will get the right output, shown here: (See Program 15 -10 in the book. ) This type of behavior is known as polymorphism. The term polymorphism means the ability to take many forms. Program 15 -11 demonstrates polymorphism by passing objects of the Graded. Activity and Pass. Fail. Exam classes to the display. Grade function. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 43
![Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley 44 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-44.jpg)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 44
![Copyright 2009 Pearson Education Inc Publishing as Pearson AddisonWesley 45 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-45.jpg)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 45
![Polymorphism Requires References or Pointers Polymorphic behavior is only possible when an object Polymorphism Requires References or Pointers • Polymorphic behavior is only possible when an object](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-46.jpg)
Polymorphism Requires References or Pointers • Polymorphic behavior is only possible when an object is referenced by a reference variable or a pointer, as demonstrated in the display. Grade function. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 46
![Base Class Pointers Can define a pointer to a base class object Base Class Pointers • Can define a pointer to a base class object •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-47.jpg)
Base Class Pointers • Can define a pointer to a base class object • Can assign it the address of a derived class object Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 47
![Base Class Pointers Base class pointers and references only know about members of Base Class Pointers • Base class pointers and references only know about members of](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-48.jpg)
Base Class Pointers • Base class pointers and references only know about members of the base class – So, you can’t use a base class pointer to call a derived class function • Redefined functions in derived class will be ignored unless base class declares the function virtual Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 48
![Redefining vs Overriding In C redefined functions are statically bound and overridden functions Redefining vs. Overriding • In C++, redefined functions are statically bound and overridden functions](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-49.jpg)
Redefining vs. Overriding • In C++, redefined functions are statically bound and overridden functions are dynamically bound. • So, a virtual function is overridden, and a non-virtual function is redefined. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 49
![Virtual Destructors Its a good idea to make destructors virtual if the class Virtual Destructors • It's a good idea to make destructors virtual if the class](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-50.jpg)
Virtual Destructors • It's a good idea to make destructors virtual if the class could ever become a base class. • Otherwise, the compiler will perform static binding on the destructor if the class ever is derived from. • See Program 15 -14 for an example Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 50
![15 7 Abstract Base Classes and Pure Virtual Functions Copyright 2009 Pearson 15. 7 • Abstract Base Classes and Pure Virtual Functions Copyright © 2009 Pearson](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-51.jpg)
15. 7 • Abstract Base Classes and Pure Virtual Functions Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Abstract Base Classes and Pure Virtual Functions Pure virtual function a virtual member Abstract Base Classes and Pure Virtual Functions • Pure virtual function: a virtual member](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-52.jpg)
Abstract Base Classes and Pure Virtual Functions • Pure virtual function: a virtual member function that must be overridden in a derived class that has objects • Abstract base class contains at least one pure virtual function: virtual void Y() = 0; • The = 0 indicates a pure virtual function • Must have no function definition in the base class Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 52
![Abstract Base Classes and Pure Virtual Functions Abstract base class class that can Abstract Base Classes and Pure Virtual Functions • Abstract base class: class that can](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-53.jpg)
Abstract Base Classes and Pure Virtual Functions • Abstract base class: class that can have no objects. Serves as a basis for derived classes that may/will have objects • A class becomes an abstract base class when one or more of its member functions is a pure virtual function Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 53
![15 8 Multiple Inheritance Copyright 2009 Pearson Education Inc Publishing as Pearson 15. 8 • Multiple Inheritance Copyright © 2009 Pearson Education, Inc. Publishing as Pearson](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-54.jpg)
15. 8 • Multiple Inheritance Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley
![Multiple Inheritance A derived class can have more than one base class Multiple Inheritance • A derived class can have more than one base class •](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-55.jpg)
Multiple Inheritance • A derived class can have more than one base class • Each base class can have its own access specification in derived class's definition: class cube : public square, public rect. Solid; class square class rect. Solid class cube Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 55
![Multiple Inheritance Arguments can be passed to both base classes constructors cube Multiple Inheritance • Arguments can be passed to both base classes' constructors: cube: :](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-56.jpg)
Multiple Inheritance • Arguments can be passed to both base classes' constructors: cube: : cube(int side) : square(side), rect. Solid(side, side); • Base class constructors are called in order given in class declaration, not in order used in class constructor Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 56
![Multiple Inheritance Problem what if base classes have member variablesfunctions with the same Multiple Inheritance • Problem: what if base classes have member variables/functions with the same](https://slidetodoc.com/presentation_image_h/349b11867ae1c1769716ac283d406c3e/image-57.jpg)
Multiple Inheritance • Problem: what if base classes have member variables/functions with the same name? • Solutions: – Derived class redefines the multiply-defined function – Derived class invokes member function in a particular base class using scope resolution operator : : • Compiler errors occur if derived class uses base class function without one of these solutions Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 57
Encapsulation inheritance and polymorphism
Inheritance encapsulation
Encapsulation inheritance polymorphism
Encapsulation inheritance polymorphism
Abstraction encapsulation inheritance polymorphism
Polymophism
Has virtual functions and accessible non-virtual destructor
Chapter 11 complex inheritance and human heredity test
Complex patterns of inheritance
Chapter 16 the molecular basis of inheritance
Chromosomal theory of inheritance
Chapter 9 patterns of inheritance
Chapter 16 molecular basis of inheritance
Chapter 15: the chromosomal basis of inheritance
The chromosomal basis of inheritance chapter 15
Chapter 15 the chromosomal basis of inheritance
Chapter 11 section 1 basic patterns of human inheritance
Chapter 11 section 1 basic patterns of human inheritance
Bioflix dna replication
Chapter 9 patterns of inheritance
Chapter 15: the chromosomal basis of inheritance
Difference between allotropy and polymorphism
Transition and transversion
What are virtual functions in c++
Destructor c++
Physalia polymorphism
Gene polymorphism
Ad-hoc polymorphism
Polymorphism jav
Compile time polymorphism in c++
Subtype polymorphism
Polymorphism computer science
Cs4450
Definition of polymorphism
Polymorphism car example
Payroll system using polymorphism in java
Compile time polymorphism in c++
Polymorphism adalah
Polymorphism dynamic allocation
Bank account java program using inheritance
Java interface polymorphism
How does polymorphism promote extensibility
Crtp static polymorphism
John gurdon experiment
Application of restriction enzymes
Restriction fragment length polymorphism
Restriction fragment length polymorphism
Restriction fragment length polymorphism
Polymorphism meaning
Restriction fragment length polymorphism
Polymorphism in c
C polymorphism example
Pure polymorphism
Single nucleotide polymorphism (snp)
Polymorphism
Opkins
Polymorphism in genetics
Extends oop