Write a program on overloading operartor++

Operator Overloading:
We know that operator is a symbol that performs some operations on one or more operands. The ability to overload operators is one of the C++’s most powerful features++ allows us to provide new definitions to some built-in operators. We can give several meaning to an operator, depending upon the types of arguments used. This capability to overload an operator is called operator overloading.
Operators are overloaded by creating operator functions.an operator function consists of a function definition, similar to a normal function except that the function name now becomes the keyword OPERATOR followed by the symbol being overloaded. 
The general form or syntax of an operator function is
Return type class name:: operator <operator symbol>(argument list)
{
//Body of the function
}

Where return type is the data type of the value return after the specific operation, operator symbol is the operator being overloaded, that is preceded by the keyword operator, and Operator function is usually a member function or a friend function. An operator that deals with only one operand is called as a unary operator. Some of the commonly used unary operators are ++,--,!,~ and unary minus. 
Overloading increment and decrement operators: The increment operator (++) and the decrement operator ( _ _ ) are unary operators since they act on only one operand. Both the increment and decrement operators have two forms. They are the prefix(i.e do the operation and then use the content of the variable.) and postfix (i.e use the content of the variable and then do the operation)notation. The general syntax form or the syntax of the prefix and postfix operator functions are
//prefix increment
return type operator++()
{
// Body of the prefix operator
}
//postfix increment
Return operator++(int x)
{
//body of the postfix operator
}

Write a program in C language is on overloading operartor++.
#include<iostream.h>
#include<conio.h>
class pen
{
private:
int count;
public:
pen()
{
count=0;
}
pen(int p)
{
count=p;
}
void showcount()
{
cout<<” the value of count is “<<count;
}
pen operator++()
{
return pen(++count);
}
pen operator++(int)
{
return pen(++count);
}
};
void main()
{
pen p1,p2;
clrscr();
cout<<”\n for object p1:”;
++p1;
p1.showcount();
p1++;
p1++;
p1.showcount();
cout<<”\n for object p2:”;
++p2;
++p2;
p2.showcount();
p2.showcount();
getch();
}
The output of the above program is:
for object p1:
the value of count is 1
the value of count is 3
for object p2:
the value of count is 2
the value of count is 3


Write a program in C language for overloading operator++ and operator— using friend functions.

#include<iostream.h>
#include<conio.h>
class book
{
private:
int x,y;
public:
book(int a,int b)
{
x=a;
y=b;
}
void showdata()
{
cout<<” \n value of x :”<<x<<endl;
cout<<”\n value of y:”<<y<<endl;
}
friend book operator ++(book &abc);
friend book operator --(book &abc);
};
book operator ++(book &abc)
{
abc.x++;
abc.y++;
return abc;
}
book operator –(book &abc)
{
abc.x--;
abc.y--;
return abc;
}
void main()
{
book b1(5,25);
clrscr();
b1.showdata();
++b1;
b1.showdata();
--b1;
b1.showdata();
getch();
}


The output of the above program is:
the value of x:5
the value of y:25
the value of x:6
the value of y:26
the value of x:5
the value of y:25


Thanks
Mukesh Rajput
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: