Unit 3 - Half
Unit 3 - Half
Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek
term, means the ability to take more than one form. An operation may
exhibit different behaviours in different instances. The behaviour depends
upon the types of data used in the operation. For example, consider the
operation of addition. For two numbers, the operation will generate a sum. If
the operands are strings, then the operation would produce a third string by
concatenation. The process of making an operator to exhibit different
behaviours in different instances is known as operator overloading.
Figure illustrates that a single function name can be used to handle different
number and different types of arguments. This is something similar to a
particular word having several different meanings depending on the context.
Using a single function
name to perform d types
of tasks is known
function overloading.
Polymorphism plays an
important role in allowing
objects having different
internal structures to
share the same external
interface. This means that
a general class of
operations.
In compile time polymorphism two are more same function name is defined
in class but parameter should be different in each function (as stated in
example). When function is called appropriate prototype is matched and
when prototype matches then function get called. This is also a concept of
overloading.
#include<iostream.h>
#include<conio.h>
class test
{
public:
void sum(int a,int b)
{
cout<<"compile time polymorphism-function overloading\n\n";
cout<<" Enter the value of a:";
cin>>a;
cout<<" Enter the value of b:";
cin>>b;
cout<<" Sum is: "<< a+b<<"\n";
}
void sum(int a,int b, int c)
{
cout<<" Enter the value of a:";
cin>>a;
cout<<" Enter the value of b:";
cin>>b;
cout<<" Enter the value of c:";
cin>>c;
cout<<" Sum is: "<< a+b+c<<"\n\n";
}
};
void main()
{
int x,y,z,i,j;
clrscr();
test t;
t.sum(i,j);
t.sum(x,y,z);
In run time polymorphism when same function name is defined in base class
and derived class, this is also known as overriding concept. The function of
base class is preceded with keyword virtual to differentiate the function call.
When we call function from main then we use pointer to call function. A
function associated with a polymorphic reference depends on the dynamic
type of that reference. At run-time the code matching the object under
current reference will be called.
#include<iostream.h>
#include<conio.h>
class Base
{
public:
int i,j;
virtual void show()
{
cout<<" I am class Base \n";
cout<<"enter the value of i: ";
cin>>i;
cout<<"enter the value of j: ";
cin>>j;
cout<<"Answer is: "<<i+j;
}
};
void main()
{
clrscr();
Base b;
Base *p,*q;
p=&b;
p->show();
cout<<"\n\n";
Derived d;
q=&d;
q->show();
getch();
}
Function overloading
As stated earlier overloading refers to the use of the same thing for
different purposes. C++ also permits overloading of functions. This means
that we can use the same function name to create functions that perform a
variety of different tasks. This is known as function polymorphism in OOP.
// Declarations
// Function calls
A function call first matches the prototype having the same number and type
of arguments and then calls the appropriate function for execution, A best
match must be unique. The function selection involves the following steps;
1. The compiler first tries to find an exact match in which the types of actual
arguments are the same, and use that function.
2. If an exact match is not found, the compiler uses the integral promotions
to the actual arguments, such as, char to int, float to double to find a
match.
3. When either of them fails the compiler tries to use the built-in conversions
to the actual arguments and then uses the function whose match is
unique. If the conversion is possible to have multiple matches, then the
compiler will generate an error message. Suppose we use the following two
functions;
A function call such as square(10) will cause error because int argument
can be converted to either long or double, thereby creating an ambiguous
situation as to which version of square( ) should be used.
4. If all of the steps fail, then the compiler will try the user-defined
conversions combination with integral promotions and built-in
conversions to find a unique match, User-defined conversions are often
used in handling objects.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
#define pi 3.14
class fn
{
public:
void area(int);
void area(int,int);
void fn::area(int a)
{
cout<<"Area of Circle:";
cout<<pi*a*a;
}
void fn::area(int a,int b)
{
cout<<"Area of rectangle:";
cout<<a*b;
}
void main()
{
int a,b,r;
clrscr();
fn obj;
cout<<"Function Overloading\n\n";
cout<<"Enter Radius of the Circle:";
cin>>r;
obj.area(r);
cout<<"Enter 1st Sides of the Rectangle:";
cin>>a;
cout<<"Enter 2nd Sides of the Rectangle:";
cin>>b;
obj.area(a,b);
cout<<"Enter 1st Sides of the Triangle:";
cin>>a;
cout<<"Enter 2nd Sides of the Triangle:";
cin>>b;
obj.area(0.5,a,b);
getch();
}
Operator overloading
Overloading means assigning different meanings to an operation, depending
on the context. Operator overloading is one of the important technique that
has enhanced the power of extensibility of C++. Operator overloading
provides a flexible option for the creation of new definitions for most of the
operator. We can almost create a new language of our own by the creative
where return type is the type of value returned by the specified operation
and op is the operator being overloaded. The op is preceded by the keyword
operator. operator op is the function name.
1. Create a class that defines the data type that is to be used in the
overloading operation.
2. Declare the operator function operator op( ) in the public part of the
class.
3. Define the operator function to implement the required operations.
#include<iostream.h>
#include<stdlib.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inches;
public:
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
void displayDistance()
{
cout<<"Entered feet value is: " << feet<<”\n”;
cout<<"Entered inches value is: " << inches<<”\n\n”;
}
Distance operator-()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
};
void main()
{
Distance D1(11, 10), D2(-5, 11);
clrscr();
-D1;
D1.displayDistance();
-D2;
D2.displayDistance();
getch();
}