Oop DS U3
Oop DS U3
Polymorphism 1
Unit 3.Polymorphism
3.1 Concepts of Polymorphism
3.2 Compile time and Run time Polymorphism
3.3 Overloading and Overriding
Concepts, difference and application
3.4 Concepts of friend function
3.5 Concepts of virtual function and pure virtual function
Types of Polymorphism
Function Overloading When there are multiple functions with the same name but different
parameters, these functions are said to be overloaded. Functions can be overloaded by changing
the number of arguments and/or the type of arguments.
class A
{
public:
void display(int a)
{
cout<<a<<endl;
}
void display(double b)
{
cout<<b<<endl;
}
void display(char *name)
{
cout<<name<<endl;
}
};
void main()
{
A a1;
clrscr();
a1.display(1);
a1.display(1.2);
a1.display("abc");
getch();
}
Output
1
1.2
abc
Operator Overloading
C++ allows us to add two variables of user-defined types using the same syntax that is applied
to the basic types. This means that C++ has the ability to provide operators with a special
meaning for a specific data type. This mechanism of giving a special meaning to an operator
for a user-defined type is known as operator overloading.
Where return type is the type of value returned by function and op is the operator being
overloaded. The op is preceded by the keyword operator. operator op is the function name.
Operator function must be either member function (non static) or friend functions.
Example 3.2 Program for Unary (-) and Binary (+) Operator Overloading Using a Member
Function
#include <iostream.h>
#include <conio.h>
class data
{
private:
int a,b;
public:
data()
{
a=0;
b=0;
}
data(int x,int y)
{
a=x;
b=y;
}
data operator+(data d2)//binary operator +
{
data d;
d.a=a+d2.a;
d.b=b+d2.b;
return(d);
}
void operator-()//unary - operator
{
a=-a;
b=-b;
}
void display()
{
cout<<"a= "<<a<<endl<<"b= "<<b<<endl;
}
};
void main()
Unit 3.Polymorphism 5
{
clrscr();
data d1(-1,-1),d2(2,2),d3;
d1.operator-();
//-d1;
d1.display();
d3=d1.operator+(d2);
//d3=d1+d2;
d3.display();
getch();
}
Output
1
1
3
3
Example 3.3 Program for Unary (-) and Binary (+) Operator Overloading Using a Friend
Function
#include <iostream.h>
#include <conio.h>
#include <string.h>
class data
{
private:
int a,b;
public:
data()
{
a=0;
b=0;
}
data(int x,int y)
{
a=x;
b=y;
}
{
d1.a=-d1.a;
d1.b=-d1.b;
}
void display()
{
cout<<a<<b<<endl;
}
};
void main()
{
clrscr();
data d3,d1(-1,-1),d2(2,2);
operator-(d1);
//-d1;
d1.display();
d2.display();
d3=operator+(d1,d2);
//d3=d1+d2;
d3.display();
getch();
}
Output
12
34
46
Example 3.4 Write a program to find the mean of numbers using a friend function.
#include<iostream.h>
#include<conio.h>
class A
{
public:
A()
{
cout<<"enter a and b";
cin>>a>>b;
}
private:
int a,b;
friend float mean(A);
};
In C++ we can pass class’s objects as arguments and also return them from a function the same
way we pass and return other variables.
Example: Write a program to add two time objects and display the sum using the friend
function.
//object as argument and return
//read and add time
#include<iostream.h>
#include<conio.h>
Unit 3.Polymorphism 8
class time
{
int hours;
int min;
public:
void gettime(int h,int m)
{
hours=h;
min=m;
}
void puttime()
{
cout<<"\n\n\t Hours :- "<<hours;
cout<<"\n\n\t Minutes :- "<<min;
}
friend time sum(time,time);
};
time sum(time t1,time t2)
{
time t;
t.min=t1.min+t2.min;
t.hours=t.min/60;
t.min=t.min%60;
t.hours=t.hours+t1.hours+t2.hours;
return(t);
}
void main()
{
time t1,t2,t3;
clrscr();
t1.gettime(1,40);
t2.gettime(1,30);
t3=sum(t1,t2);
cout<<"\t\t\t TIME T1 :- ";t1.puttime();
cout<<"\n\t\t\t TIME T2 :- ";t2.puttime();
cout<<"\n\t\t\t THE ADDITION OF TWO TIMES ";
cout<<"\n\n\t\t\t TIME T3 :- ";t3.puttime();
getch();
class base
{
public:
virtual void print()
{
cout << "print base class" << endl;
}
void show()
{
cout << "show base class" << endl;
}
};
class derived : public base
{
public:
void print()
{
cout << "print derived class" << endl;
}
void show()
{
cout << "show derived class" << endl;
}
};
int main()
{
base *bptr;
derived d;
bptr = &d;
clrscr();
Unit 3.Polymorphism 10
A virtual function is a member function declared within a base class and is redefined
(overridden) by a derived class. When you refer to a derived class object using a pointer or a
reference to the base class, you can call a virtual function for that object and execute the
derived class’s version of the function.
Example virtual void print( ){cout<<"hello";}
1. Write a program to find the area of a circle and a rectangle using function overloading.
#include <iostream.h>
#include <conio.h>
class test
{
public:
void area(double r)
{
cout<<"area of circle is "<<3.14*r*r<<endl;
}
void area(float l,float b)
{
cout<<"area of rect is "<<l*b<<endl;
}
};
void main()
{
test t;
clrscr();
Unit 3.Polymorphism 11
t.area(10);
t.area(1.5,1.5);
getch();
}
void main()
{
clrscr();
char s1[10],s2[10];
cin>>s1>>s2;
data d1(s1),d2(s2),d3;
//d3=d1.operator+(d2);
Unit 3.Polymorphism 12
d3=d1+d2;
d3.show();
if(d1==d2)
cout<<"\n same";
else
cout<<"\n not same";
getch();
}
};
void main()
{
clrscr();
data d1,d2;
if(d1>d2)
{
d1.show();
cout<<" is large";
}
else
{
d2.show();
cout<<" is large";
Unit 3.Polymorphism 13
}
getch();
}
class data
{
private:
int a[5];
public:
void get();
data operator+(data);
void display();
};
void data::get()
{
for(int i=0;i<5;i++)
cin>>a[i];
}
void main()
{
clrscr();
data d,d1,d2;
d1.get();
d2.get();
clrscr();
d=d1+d2;
d.display();
getch();
}
Unit 3.Polymorphism 14
5.Create a base class shape, declare two data members dim1 and dim2 and declare- calculate area
function as virtual function. Derive two class rectangle and triangle from the shape class that
override calculate area function.
#include<iostream.h>
#include<conio.h>
class shape
{
protected:
int d1,d2;
public:
virtual void area()=0;
};
class rect:public shape
{
public:
rect(int x,int y)
{
d1=x;
d2=y;
}
void area()
{
cout<<"\n area of rectangle"<<d1*d2;
}
};
class triangle:public shape
{
public:
triangle(int x,int y)
{
d1=x;
d2=y;
}
void area()
{
cout<<"\n area of triangle "<<0.5*d1*d2;
}
};
void main()
{
clrscr();
shape *s1;
rect r(10,10);
triangle t(20,20);
s1=&r;
Unit 3.Polymorphism 15
s1->area();
s1=&t;
s1->area();
getch();
Output
area of rectangle 100
area of triangle 200
#include<iostream.h>
#include<conio.h>
#include<string.h>
class media
{
protected:
char title[50];
float price;
public:
media(char *s,float a)
{
strcpy(title,s);
price=a;
}
void main()
{
char *t;
float p,pt;
int pg;
clrscr();
//Book detail
cout<<"\n enter book detail\n";
cout<<"Title ";cin>>t;
cout<<"Prices ";cin>>p;
cout<<"Pages ";cin>>pg;
book bk(t,p,pg);
//Tape detail
cout<<"\n enter tape detail\n";
cout<<"Title ";cin>>t;
cout<<"Prices ";cin>>p;
cout<<"Play time ";cin>>pt;
tape tp(t,p,pt);
clrscr();
media *list[2];
list[0]=&bk;
list[1]=&tp;
getch();
}