The document explains the concepts of polymorphism in C++, focusing on compile-time and runtime polymorphism. It details the use of virtual functions for achieving runtime polymorphism and the behavior of pointers in relation to base and derived classes. Additionally, it outlines the differences between method overloading and overriding, emphasizing how these techniques facilitate flexible programming through dynamic and static binding.
2Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Introduction Virtual functions belongs to the branch of Runtime Polymorphism in C++ Polymorphism Runtime/Late BindingCompile Time/Early Binding Function Overloading Operator Overloading Virtual Functions / Function Overriding
3.
3Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Introduction Polymorphism is classified into 2 branches Compile Time Polymorphism/Early Binding/Static Binding Runtime Polymorphism/Late Binding/Dynamic Binding
4.
4Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in What is Binding? For every function call; compiler binds or links the call to one function definition. This linking can happen at 2 different time At the time of compiling program, or At Runtime
5.
5Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Compile Time Polymorphism Function Overloading is an example of Compile Time Polymorphism. This decision of binding among several functions is taken by considering formal arguments of the function, their data type and their sequence.
6.
6Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Example of compile time polymorphism void MyFunction(int i) { cout << “an int is passed”; } void MyFunction(char c) { cout << “a char is passed”; } int main() { MyFunction(10); MyFunction(‘x’); } an int is passed a char is passed Output
7.
7Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Runtime Polymorphism In late binding; call to a function is resolved at Runtime, the compiler determines the type of object at execution time and then binds the function call to a function definition. Late binding is also called as Dynamic Binding or Runtime Binding. Virtual Functions are example of Late Binding in C++ Runtime polymorphism is achieved using pointers.
8.
8Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Pointers behaviour in Polymorphism A base class pointer variable can hold address of derived class object, but it can access only members of base class.
9.
9Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Pointers behaviour in Polymorphism class base { public: void show() { cout << “Show from base”; } }; class derived : public base { public: void show() { cout << “Show from derived”; } }; Show from base Output int main() { base *ptr; derived ob; ptr = &ob; ptr -> show(); }
10.
10Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Pointers behaviour in Polymorphism You can see that even the pointer holds address of derived class object; it has called the base version of show() method. The problem is even a base class pointer holds address of derived type of object, it can access only members of base class, because the base pointer variable doesn’t have any idea about the structure of derived class. A base class can’t have any idea about what derived class has added to it.
11.
11Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword class base { public: virtual void show() { cout << “Show from base”; } }; class derived : public base { public: void show() { cout << “Show from derived”; } }; Show from derived Output int main() { base *ptr; derived ob; ptr = &ob; ptr -> show(); }
12.
12Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword Using virtual keyword with base class version of show function; late binding takes place and derived version of the function will be called, because base pointer points an derived type of object. We know that in runtime polymorphism the call to a function is resolved at runtime depending upon the type of object.
13.
13Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in How virtual function works? Base class pointer can point to derived class object. In this case, using base class pointer if we call some function which is in both classes, then base class function is invoked. But if we want to invoke derived class function using base class pointer, it can be achieved by defining the function as virtual in base class, this is how virtual functions support runtime polymorphism.
14.
14Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Virtual functions. A virtual function is a member function that is declared as virtual within a base class and redefined by a derived class. To create virtual function, precede the base version of function’s declaration with the keyword virtual. When a class containing virtual function is inherited, the derived class can redefine (Override) the virtual function to suit its own unique needs. The method name and type signature should be same for both base and derived version of function.
16Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Using virtual Keyword Area of Circle : 200.96 Area of Cylinder : 602.88 Output int main() { circle *ptr; circle CircleOb(8); cylinder CylOb(8, 4); ptr = &CircleOb; cout << endl << "Area of Circle : " << ptr -> area(); ptr = &CylOb; cout << endl << "Area of Cylinder : " << ptr -> area(); return 0; }
17.
17Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Why method overriding? Method overriding allows a derived class to provide a specific implementation of a method that is already provided by one of its base class. The implementation in the derived class overrides (replaces) the implementation in the base class by providing a method that has same name, same parameters (signature), and same return type as the method in the parent class has. Using a base class pointer variable, we can point to object of any child class, depending upon the type of object at runtime a particular method will be called if that method is made virtual in base class & overridden in derived class.
18.
18Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Method Overloading vs. Overriding? Overloading occurs when two or more methods in one class have the same method name but different parameters (signature), Overriding means having two methods with the same method name and parameters (method signature), one of the method is in the parent class and the other is in child class. Call to an Overloaded method is resolved at compile time, while call to an Overridden method is resolved at runtime depending upon the type of object.
19.
19Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Method Overloading vs. Overriding? Overloading Overriding Definition Methods having same name but each must have different number of parameters or parameters having different types & order. Sub class have method with same name and exactly the same number and type of parameters and same return type as super class method. Meaning More than one method shares the same name in the class but having different signature. Method of base class is re-defined in the derived class having same signature. Behaviour To Add/Extend more to method’s behaviour. To Change existing behaviour of method. Polymorphism Compile Time Run Time Inheritance Not Required Always Required Method Signature Must have different signature Must have same signature.
20.
20Rising Technologies, Jalna(MH). + 91 9423156065, www.RisingTechnologies.in Method Overloading vs. Overriding? Overloading Overriding Method Relationship Relationship is between methods of same class. Relationship is between methods of super class and sub class. No of Classes Does not require more than one class for overloading. Requires at least 2 classes for overriding. Example