Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,855 questions

51,776 answers

573 users

How to implement the prefix ++ Increment operator overloading in C++

2 Answers

0 votes
#include <iostream>

class OperatorOverloading
{
	private:
		int n;
	public:
		OperatorOverloading() : n(0) {  }
		void operator ++()
		{
			n++;
		}
		void Display()
		{
			std::cout << "n = " << n << std::endl;
		}
};

int main()
{
	OperatorOverloading obj;

	obj.Display();
	
	++obj;

	obj.Display();

	return 0;
}



/*
run:

n = 0
n = 1

*/

 



answered Jun 6, 2017 by avibootz
0 votes
#include <iostream>

class OperatorOverloading
{
	private:
		int n;
	public:
		OperatorOverloading() : n(0) {  }

		OperatorOverloading operator ++()
		{
			OperatorOverloading tmp;
			n++;
			tmp.n = n;

			return tmp;
		}
		void Display()
		{
			std::cout << "n = " << n << std::endl;
		}
};

int main()
{
	OperatorOverloading objA, objB;

	objA.Display();
	objB.Display();

	objB = ++objA;

	objA.Display();
	objB.Display();

	return 0;
}



/*
run:

n = 0
n = 0
n = 1
n = 1

*/

 



answered Jun 6, 2017 by avibootz

Related questions

1 answer 205 views
1 answer 209 views
2 answers 228 views
1 answer 190 views
1 answer 134 views
134 views asked Dec 2, 2022 by avibootz
1 answer 208 views
...