Operator Overloading Programs.
Operator Overloading Programs.
=> Allow us to give additional meanings to the normal C++ operators like
+,-,++,--,+=,-=, etc.
=> Operator overloading is not only advantagious, but it also have some
dangerous effects, which we will discuss later. */
There are two types of overloading.
1. Overloading Unary Operators.
2. Overloading Binary Operators.
class counter
{
private:
unsigned int count;
public:
counter()
{
count=0;
}
unsigned int get_count()
{
return count;
}
void operator ++() // Operator ---> keyword.
{ // ++() is used as the function which will
++count; // will increament the value of count, each
} // time it is called.
};
// we also can use ++ operator for reducing
int main() // the value of the variable count.
{ // single operator like + and - can also
clrscr(); // be used.
counter c1,c2;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count()<<endl;
++c1;
++c1;
++c2;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count();
//return 0;
getch();
}
/*
The fuction, which contain the definition of the operator overloading
will increament or dicreament (increament in this case) the variable of
the class data type(here the class type is "counter" and the variables
c1 and c2 are also of the counter type.
if traditional data types like int,float and char etc are increamented
with the same operator, the operator will perform its default operation.*/
/*
In previous programe, we can't do the following.
c1=++c2;
the reason is that we have defined the overloading function with the void
return type. to make it possible, the return type of the overloading function
must be changed to the type of the returning value.
As here we assign the increamented value(with help of overloading operator)
to c1, which datatype is "counter", the class type, so we will have to change
the return type of the overloading function to the "counter" datatype. */
#include<iostream.h>
#include<conio.h>
class counter
{
private:
unsigned int count;
public:
counter():count(0)
{
}
};
int main()
{
clrscr();
counter c1,c2;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count()<<endl;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count();
getch();
}
counter temp;
temp.cout=count;
return temp;
these steps are performed only to return a value of the counter type to the
calling function. We can perform this operation using simple method, by
creating "Nameless Temporary Constructor".
#include<iostream.h>
#include<conio.h>
class counter
{
private:
unsigned int count;
public:
counter():count(0)
{
}
int main()
{
clrscr();
counter c1,c2;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count()<<endl;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count();
getch();
}
*/
#include<iostream.h>
#include<conio.h>
class counter
{
private:
unsigned int count;
public:
counter():count(0)
{
}
counter operator ++(int) // Here "int" is not any argument,it just used
{ // to indicate the compilor to create a postfix
return counter(count++); // notation for the variable "counter".
}
// an unnamed object of counter type is created
// and passed to the calling function.
};
int main()
{
clrscr();
counter c1,c2;
cout<<"c1= "<<c1.get_count()<<endl;
cout<<"c2= "<<c2.get_count()<<endl;
getch();
}
BINARY OPERTORS: those operators, which operates on two inputs are called
binary operators.
example: +,-,*,/ and so on.
#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet;
float inches;
public:
distance(): feet(0),inches(0.0)
{
}
int main()
{
distance dist1,dist3,dist4;
dist1.getdist();
cout<<"dist1="; //
dist1.showdist(); // these lines are just used to
//
cout<<"dist2="; // display the results,
dist2.showdist(); //
// stored in different objects, with help
cout<<"dist3="; //
dist3.showdist(); // of show function.
//
cout<<"dist4="; //
dist4.showdist(); //
//
return 0;
}
// += overloading programe.
#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet;
float inches;
public:
distance():feet(0),inches(0.0)
{
}
void getdist()
{
cout<<"Enter feet:"<<endl; cin>>feet;
cout<<"Enter inches:"<<endl; cin>>inches;
}
void showdist() const
{
cout<<feet<<"-"<<inches<<endl;
}
int main()
{
distance dist1;
dist1.getdist();
distance dist2(6,2.5);
dist1 += dist2;
cout<<"after addition"<<endl<<endl;
cout<<"dist1: ";
dist1.showdist();
cout<<endl;
getch();
}
=> Here we are overloading the less than (<> comparison operator, and the
same process is followed for overloading all the operators.
=> this is not possible in traditional way to compare two objects with these
operators, like
dist1 < dist2
After performing the following operational code to overload the
< operator,it becomes possible.
*/
#include<iostream.h>
#include<conio.h>
class distance
{
private:
int feet;
float inches;
public:
distance():feet(0),inches(0.0)
{
}
void getdist()
{
cout<<"Enter feet:"<<endl; cin>>feet;
cout<<"Enter inches:"<<endl; cin>>inches;
}
return ret;
}
int main()
{
distance dist1;
dist1.getdist();
distance dist2(6,2.5);
getch();
}
*/
// Concatinating Strings.
#include<iostream.h>
#include<conio.h>
#include<string.h> //..... Libraries, which contain the definition of
#include<stdlib.h> // strcpy(), strlen() functions,used in programe.
class string
{
private:
enum //.... it is an enumerated data type, which counts down the
{ // the memory loacations used in this datatype.
sz=80
};
char str[sz]; // A string of 80 characters is declared.
public:
string() // Constructor Declaration.
{
strcpy(str,"");
}
void display() const // A constant function, which disply the string "str".
{
cout<<str<<endl;
}
void main()
{
string s1="\n Salam "; // initialization of first string.
string s2=" and Eid Mubarak"; // initialization of second string.
string s3;
s1.display();
s2.display();
//s3.display(); // S3 is empty string, becoz nothing is assigned to it till now.