Written by Sai Sandeep Thota on April 24, 2013 in C++ > Programming

C++ Program to Implement Prefix Increment using Operator Overloading

GET ALERTS:

Get our Latest updates delivered to your mailbox!

In this article we are going to perform Prefix Increment using the operator: ++. We have already discussed about operator overloading in our earlier article, you can read it here. In the below program all we are doing is defining a function called getx() and getting a value from main function and assigning it to our variable x. display() function is used to display the value of x.

operator++ is used for prefix increment of the variable x. We can also perform postfix increment by just putting a dummy argument in the operator++() function.

#include<conio.h>
#include<iostream.h>

class increment {
	public:
	int x;
	void getx(int a)
	{
		x=a;
	}
	void display()
	{
		cout<<"\nValue of X:"<<x;
	}
	void operator++();
};

void increment::operator++()
{
	x=++x;
}

void main()
{
	increment ob;
	clrscr();
	ob.getx(10);
	ob.display();
	++ob;
	ob.display();
	getch();
}

Output:
prefix-increment-operator-overloading

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: