Professional Documents
Culture Documents
Programming in C++ CST-152 Unit-I Polymorphism Chapter-6
Programming in C++ CST-152 Unit-I Polymorphism Chapter-6
CST-152
UNIT-I
Polymorphism
Chapter-6
Course Objectives
• To gain knowledge of polymorphism and its various types.
• To understand the concept of function overloading and
operator overloading .
• In this type of overloading we define two or more functions with same name and same
number of parameters, but the type of parameter is different. For example in this program,
we have two sum() function, first one gets two integer arguments and second one gets two
double arguments.
int sum(int x,int y)
{
cout<< x+y;
}
double sum(double x,double y)
{
cout << x+y;
}
int main()
{
sum (10,20);
sum(10.5,20.5);
}
• You can also overload Relational operator like == , != , >= , <= etc. to compare two user-
defined object.
Example
class time
{ int hr,min,sec;
public: time()
{ hr=0, min=0; sec=0;
}
time(int h,int m, int s)
{ hr=h, min=m; sec=s;
}
friend bool operator==(time &t1, time &t2); //overloading '==' operator }; bool
operator== (time &t1, time &t2) //operator function
{ return ( t1.hr == t2.hr && t1.min == t2.min && t1.sec == t2.sec );
}