
- OOAD Tutorial
- OOAD - Home
- OOAD - Object Oriented Paradigm
- OOAD - Object Oriented Model
- OOAD - Object Oriented System
- OOAD - Object Oriented Principles
- OOAD - Object Oriented Analysis
- OOAD - Dynamic Modelling
- OOAD - Functional Modelling
- OOAD - UML Analysis Model
- OOAD - UML Basic Notations
- OOAD - UML Structural Diagrams
- OOAD - UML Behavioural Diagrams
- OOAD - Object Oriented Design
- OOAD - Implementation Strategies
- OOAD - Testing & Quality Assurance
- OOAD Useful Resources
- OOAD - Quick Guide
- OOAD - Useful Resources
OOAD Princhiples Q/A #4
Question:What do you mean by polymorphism? How Polymorphism is achieved at compile time and run time? Give an example of the polymorphism.
Answer:
Polymorphism
Polymorphism refers to the ability to take more than one form. Polymorphism is the concept by which the same message or data can be sent to objects of several different classes. In the following example, same message is send to objects of different classes and the objects behave differently. Polymorphism is often considered the most powerful feature of an OOP language. In C++, polymorphism is implemented using the operator/function overloading. It is the ability of operator or a function to take more than one form. Polymorphism is classified in to two categories:

Compile time polymorphism
Run time polymorphism
Compile time polymorphism
In compile time polymorphism, the compiler is able to choose the appropriate function for a particular call at the compile time itself. It is also called static binding or early binding which means that the code associated with the function call is linked at compile time. In compile time polymorphism ,there are two types of overloading:
operator overloading
function overloading
Operator overloading
The general syntax for defining operator overloading is:
return_type classname :: operator op (argument list) { Body of function }
Here return_type is the type of value returned by the operation. op is the operator being overloaded. operator is the function name. Body of the function contains set of instructions to be performed by operator function.
Program example to explain operator overloading:
// to overload unary minus'-' operator #include <iostream> using namespace std; class Counter { int a, b,c; public: void inputdata (int x, int y,int z); void display ( ); Counter operator–(const Counter& b ); }; void Counter::inputdata(int x, int y, int z) { a = x; b = y; c = z; } void Counter::display( ) { cout <<"x ="<<a<<endl; cout <<"y ="<<b<<endl; cout <<"z ="<<c<<endl; } Counter Counter::operator–(const Counter& b ) { Counter counter; counter.a = this->a - b.a; counter.b = this->b - b.b; counter.c = this->b - b.c; return counter; } int main() { Counter obj1; Counter obj2; Counter obj3; cout<<"obj1:"<<endl; obj1.inputdata (50,60, -70); cout<<"obj2:"<<endl; obj2.inputdata (40,50, -60); obj2.display( ): obj3 = obj1-obj2: cout<<"obj3:"<<endl; obj3.display( ); return 1; }Output:
Obj 1 : X =50 Y= 60 Z= -70 Obj 2: X =40 Y =50 Z =-60 Obj 3: X =10 Y =10 Z =-10
function overloading
Function overloading is a concept where several function declarations are specified with a same name that perform similar tasks, but on different data types. Such functions are said to be overloaded. C++ allows functions to have the same name . These functions can only be distinguished by their number and type of arguments.
Example:
int add (int a, int b); int add (int a, int b, int c); float add (float a, float b);
The function add (int a, int b) which takes two integer inputs is different from the functions add (int a ,int b, int c) which takes three integer inputs and add (float a, float b ) which takes two float inputs. When an overloaded function is called, the C++ compiler selects the proper function by examining the number , types and order of the arguments in the call. Thus an overloaded function performs different activities depending on the kind of data sent to it. Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby make the program to run faster.
Program example to explain function overloading
// program using concept of function overloading #include<iostream> #include<math> using namespace std; float area (float a, float b, float c) { float s, ar; s = (a+b+c)/2 ; ar = sqrt (s*(s-a)* (s-b)*(s-c)); return (ar); } float area (float a, float b) { return (a*b); } float area (float a ) { return (3.14*a*a); } int main ( ) { int choice ; float s1,s2,s3, ar; do { cout << Area/n"; cout <<"1.Rectangle\n"; cout <<"2.triangle\n"; cout <<"3.circle\n"; cout <<"4.exit\n"; cout <<enter your choice:"; cin>> choice ; switch (choice) { case 1: cout<<"enter length and breadth\n"; cin >>s1>>s2; ar =area (s1,s2); break; case 2: cout<<"enter three sides\n"; cin >>s1>>s2>>s3; ar =area (s1,s2,s3); break; case 3: cout<<"enter radius\n"; cin >>s1; ar =area (s1); break; case 4: break; default :cout <<"wrong choice"; } cout<<"the area is "<<ar <<endl; } while (choice!=4); return 1; }Output:
Area 1. Rectangle 2. triangle 3. circle 4. Exit Enter your choice :1 Enter length and breadth 10 20 The area is 200 Area 1. Rectangle 2. triangle 3. circle 4. Exit Enter your choice :2 Enter three sides 3 4 5 The area is 6 Area 1. Rectangle 2. triangle 3. circle 4. Exit Enter your choice :3 Enter radius 10 The area is 314 Area 1. Rectangle 2. triangle 3. circle 4. Exit Enter your choice : 4
Run time polymorphism
If the appropriate member function is selected during the running program then it is called run time polymorphism . It is implemented using virtual function. Runtime polymorphism is also called as dynamic binding or late binding. Virtual functions: Virtual functions are special type of member functions which are defined in base class and are redefined in derived classes. The general syntax to declare a virtual function is
class classname // base class of C++ virtual function { public : virtual void memberfunctionname ( ) // this denotes the c++virtual function { : } };
Program to explain virtual function
# include<iostream> using namespace std; class vehicle//base class of C++ virtual function { public: virtual void make ( ) // C++ virtual function { cout<<" member function of base class vehicle accessed" <<endl; } }; class fourwheeler : public vehicle { public: void make ( ) { cout <<" virtual member function of derived class fourwheeler accessed"<< endl; } }; int main ( ) { vehicle *a, *b; a = new vehicle ( ); a-> make ( ); b = new fourwheeler ( ); b-> make ( ); return 1; }Output:
Member function of base class vehicle accessed virtual member function of derived class fourwheeler accessed.
In the above example , it is evidenced that after declaring the member functions make ( ) as virtual inside the base class vehicle, class fourwheeler is derived from the base class vechile. In the derived class , the new implementation for virtual function make ( ) is placed. Here, if the member function has not been declared as virtual, the base class member function is always called because linking takes place during compile time and is therefore static.In this example , the member function is declared virtual and the address is bounded only during run time, making it dynamic binding and thus the derived class member is called. To achieve the concept of dynamic binding in C++, the compiler creates a v-table each time a virtual function is declared. This V – table contains classes and pointers to the functions from each of the objects of the derived class. This is used by the compiler whenever a virtual function is needed.