Presentation is loading. Please wait.

Presentation is loading. Please wait.

Inheritance: Polymorphism and Virtual Functions

Similar presentations


Presentation on theme: "Inheritance: Polymorphism and Virtual Functions"— Presentation transcript:

1 Inheritance: Polymorphism and Virtual Functions
Lecture 27 Fri, Mar 26, 2004 2/22/2019 Inheritance

2 Topics Access to inherited data members Inherited member functions
Polymorphism Virtual functions Abstract classes 2/22/2019 Inheritance

3 Access to Inherited Members
Three Access Modes Public Protected Private Base-class members have their usual member-access mode. The inheritance relation itself has a separate access mode. 2/22/2019 Inheritance

4 Access to Inherited Members
Derived-class members have access to public and protected base-class members. Non-members have access to public base-class members only. Base member access public protected private Inheritance type no access 2/22/2019 Inheritance

5 Polymorphism A function that specifies a base-class object in its parameter list may accept a derived-class object in its place. This phenomenon is called polymorphism. Polymorphism works because the derived-class object IS-A base-class object. We have already seen this used in the constructors. 2/22/2019 Inheritance

6 Polymorphism and Passing by Value
If a function passes the base-class object by value, then the derived-class object is considered to be an object of the base class. This happens because the base-class copy constructor was used to create the local object. The local object loses its derived-class data members and functions. 2/22/2019 Inheritance

7 Example: Polymorphism
int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

8 Demonstration Run the program FamilyTree.cpp to see what happens.
2/22/2019 Inheritance

9 Polymorphism and Passing by Reference
If the function passes the base-class object by reference, then the derived-class object is considered to be an object of the derived class. 2/22/2019 Inheritance

10 Example: Polymorphism
int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person& p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

11 Demonstration In the program FamilyTree.cpp, make the function parameter a reference parameter. Run the program to see what happens. 2/22/2019 Inheritance

12 Virtual Functions When base class and a derived class have distinct functions of the same name, how does the compiler know which one to invoke? If the base-class function is virtual, then The computer will invoke the member function of that name that is closest to the class of the invoking object. Write the keyword virtual at the beginning of the function prototype. 2/22/2019 Inheritance

13 Example: Virtual Functions
class Person { virtual void Output(ostream& out) const {out << name << ‘ ‘ << sex;} }; 2/22/2019 Inheritance

14 Example: Virtual Functions
int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person& p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

15 Demonstration In the file person.h, make Output() a virtual function.
Run the program FamilyTree.cpp to see what happens 2/22/2019 Inheritance

16 Virtual Functions and Value Parameters
What happens when the function is virtual and the parameter is a value parameter? 2/22/2019 Inheritance

17 Example: Virtual Functions
int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

18 Demonstration In the program FamilyTree.cpp, make the function parameter a value parameter. Run the program to see what happens. 2/22/2019 Inheritance

19 Pure Virtual Functions
A function may be designated as a pure virtual function. Write virtual function(parameters) = 0; The function is not instantiated at this level in the class hierarchy. The function must be instantiated in the derived classes. 2/22/2019 Inheritance

20 Pure Virtual Functions
This is done when the function must be implemented at some level in the hierarchy, but there is not enough information at the top level to implement it there. 2/22/2019 Inheritance

21 Abstract Classes An abstract class is a class that contains a pure virtual function. No object of an abstract class may be instantiated. Function parameters of an abstract class type must be passed by reference. 2/22/2019 Inheritance

22 Example: Pure Virtual Functions
class Person { virtual void Output(ostream& out) const = 0; }; 2/22/2019 Inheritance

23 Example: Pure Virtual Functions
int main() { Man m("Joe"); Woman w("Jane"); Describe(m); Describe(w); } void Describe(Person& p) cout << p << endl; // What will happen? return; 2/22/2019 Inheritance

24 Demonstration In the file person.h, make the Output() function pure virtual. In FamilyTree.cpp, make the function parameter a reference parameter. Run the program to see what happens. 2/22/2019 Inheritance

25 Demonstration In FamilyTree.cpp, make the function parameter a value parameter. Run the program to see what happens. 2/22/2019 Inheritance

26 Example: Abstract Class
Circles, squares, and triangles are shapes. Create a shape class as a base class. Each shape has an area and a perimeter. However, we cannot find the area or perimeter until we know the particular shape. Therefore, Shape should be an abstract class. 2/22/2019 Inheritance


Download ppt "Inheritance: Polymorphism and Virtual Functions"

Similar presentations


Ads by Google