Computer Programming II Lecture 7 Polymorphism - Polymorphism is a powerful feature of the object oriented programming language C++. - Polymorphism is the ability to use an operator or function in different ways. Polymorphism gives different meanings to the operators or functions. - Polymorphism refers to codes, operations or objects that behave differently in different contexts. Polymorphism Types of Polymorphism: - In C++, polymorphism is supported both at run time and at compile time. - Operator and function overloading are examples of compile-time polymorphism. As powerful as operator and function overloading are, they cannot perform all the tasks required by a true, object-oriented language. Therefore, C++ also allows run-time polymorphism through the use of derived classes and virtual functions. Polymorphism 1- Virtual function: - A virtual function is a function that is declared as virtual in a base class and redefined in one or more derived classes. Thus, each derived class can have its own version of a virtual function. - When a virtual function is redefined in a derived class, it is said to be overridden. Polymorphism Note : - You declare a virtual function as virtual inside a base class by preceding its declaration with the keyword virtual. When a virtual function is redefined by a derived class, the keyword virtual need not be repeated (although it is not an error to do so). - A class that includes a virtual function is called a polymorphic class. This term also applies to a class that inherits a base class containing a virtual function. Example 1: The following program demonstrates a virtual function: Example 1: This program produces the following output: Example 1: Let’s examine the program in detail to understand how it works. In base, the function who( ) is declared as virtual. This means that the function can be redefined by a derived class. Inside both first_d and second_d, who( ) is redefined relative to each class. Inside main( ), four variables are declared: base_obj, which is an object of type base; p, which is a pointer to base objects; and first_obj and second_obj, which are objects of the two derived classes. Example 1: Next, p is assigned the address of base_obj, and the who( ) function is called. Since who( ) is declared as virtual, C++ determines, at run time, which version of who( ) is referred to by the type of object pointed to by p. In this case, p points to an object of type base, so it is the version of who( ) declared in base that is executed. Next, p is assigned the address of first_obj. Recall that a base class pointer can refer to an object of any derived class. Now, when who( ) is called, C++ again checks to see what type of object is pointed to by p and, based on that type, determines which version of who( ) to call. Since p points to an object of type first_d, that version of who( ) is used. Likewise, when p is assigned the address of second_obj, the version of who( ) declared inside second_d is executed Example 1: A virtual function can be called normally, with the standard object, dot-operator syntax. This means that in the preceding example, it would not be syntactically incorrect to access who( ) by using this statement: first_obj.who(); However, calling a virtual function in this manner ignores its polymorphic attributes. It is only when a virtual function is accessed through a base class pointer that run-time polymorphism is achieved. Polymorphism Virtual Functions Are Inherited: Once a function is declared as virtual, it stays virtual no matter how many layers of derived classes it may pass through. For example, if second_d is derived from first_d instead of base, as shown in the next example, then who( ) is still virtual and the proper version is still correctly selected: Polymorphism Example 2: Example 2: Polymorphism Pure Virtual Function and abstract base class: - A pure virtual function is a function declared in a base class that has no definition relative to the base. As a result, any derived type must define its own version—it cannot simply use the version defined in the base. To declare a pure virtual function, use this general form: virtual type func-name(parameter-list) = 0; - A class that contains at least one pure virtual function is called an abstract base class. Polymorphism An abstract base CPolygon class could look like this: Polymorphism Polymorphism 2- Function overloading: - In C++ two different functions can have the same name if their parameter types or number are different. That means that you can give the same name to more than one function if they have either a different number of parameters or different types in their parameters. - You can not overload function declarations that differ only by return type. CONCEPT: Two or more functions may have the same name, as long as their parameter lists are different. Function overloading: Example 1: Following is the example where same function print( ) is being used to print different data types: Polymorphism The differences between virtual function and function overloading: In fact, the two processes are fundamentally different. First, an overloaded function must differ in its type and/or number of parameters, while a redefined virtual function must have exactly the same type and number of parameters. In fact, the prototypes for a virtual function and its redefinitions must be exactly the same. If the prototypes differ, then the function is simply considered to be overloaded, and its virtual nature is lost. Polymorphism Another restriction is that a virtual function must be a member, not a friend, of the class for which it is defined. However, a virtual function can be a friend of another class. **Also, it is permissible for destructor functions to be virtual, but this is not so for constructors. Polymorphism 1. How do you show the declaration of a virtual constructor? There is no virtual constructor. 2- If a base class declares a function to be virtual, and a derived class does not use the term virtual when overriding that class, is it still virtual when inherited by a third-generation class? Yes, the virtuality is inherited and cannot be turned off. Polymorphism 3. Show the declaration of a virtual function taking an integer parameter and returning void. virtual void SomeFunction(int); 4. Show the declaration of a class Square, which derives from Rectangle, which in turn derives from Shape. class Square : public Rectangle { }; 5. Show the declaration for a class JetPlane, which inherits from Rocket and Airplane. class JetPlane : public Rocket, public Airplane Polymorphism 6. What is wrong with this code? class Shape() { public: Shape(); virtual ~Shape(); virtual Shape(const Shape&); }; You can't declare a copy constructor to be virtual.