Click here to Skip to main content
15,889,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
	int x;
public:
	sample();
	void display();
	friend void operator++(sample &ob2);
};


sample::sample()
{
	x=0;
}

void sample::display()
{
	cout<<x;
}

void operator++(sample &ob1)
{
	++ob1.x;
}

int main()

{
	sample ob1;
	ob1.display();
	++ob1;
	ob1.display();
	getch();
}


What I have tried:

i dont have any idea about the code .i have a lot of doubts hope you guys will clear
1)--- why used &ob2 inside friend operator overloding
2)---why used sample &ob1 inside void operator ++(sample &ob1)
3)-- why used ++ob1.x;
what is the need of .x in ++ob1.x
Posted
Updated 1-May-22 7:05am
v2
Comments
Richard MacCutchan 1-May-22 7:59am    
I have adjusted your code formatting to make it clearer.

The ampersand character (&) signifies that the parameter (ob1 and ob2) are to be passed by reference. And the .x is telling the code that the increment operator will actually increment the value of field x in the sample object.

See C++ Language Reference | Microsoft Docs[^].
 
Share this answer
 
Comments
coderlearner 1-May-22 7:17am    
why used &ob2 inside friend operator overloading
2) can you again tell the logic of using .x here
i am computer science beginner so hope you would describe more
simply
Richard MacCutchan 1-May-22 7:25am    
I already told you, it signifies the object is passed by reference, the same as in the operator++ definition. And the .x is a reference to the x field of a sample object.

If you really want to learn in proper detail then try one of the online tutorial sites such as Learn C++ – Skill up with our free tutorials[^] and C++ Tutorial[^]. You will not learn quickly by posting questions here.
coderlearner 1-May-22 7:20am    
whats the use reference variable .i am struggling to understand its use
Greg Utas 1-May-22 7:26am    
Leaving out the & will result in a copy of the object being passed to the function. The work will be done on the copy, not the original object. To have the work done on the invoker's object, the object must be passed by reference. That is, the function receives a reference (secretly a pointer) to the object, which allows the work to be done on the external object rather than a copy.
1. ob2 is the argument for the operator++ overload. It is a friend because it will be implemented outside the sample class. It will therefore have no implicit this pointer, so it has to provide one (ob2). It also wants to access the private member x, so it has to be a friend.
2. This is the same argument as above, except its name has changed to ob1. It is a reference to the object on which operator++ will be executed. Changing the name like this, for no good reason, is confusing. Maybe I'm missing something, but this is poor code. The operator++ overload could be implemented within the sample class.
3. ++ob1.x increments x within the object ob1. Leaving .x out would mean "increment" the entire object, which makes no sense. operator++ must do something to one (or more) specific data members.
 
Share this answer
 
v7
Comments
Greg Utas 1-May-22 7:34am    
Your code looks legal to me. I can't say whether it's complete, because that requires knowledge of what it's supposed to do. Unless you're specifically practicing the idea of implementing an operator overload outside a class, which can be useful for binary operators, I would just implement operator++ within the sample class. But its definition would then look different (no need for friend or the ob2 argument). It also needs to return a reference to the object. And there are two versions of operator++: prefix (which you have here) and postfix. Please read https://stackoverflow.com/questions/3846296/how-to-overload-the-operator-in-two-different-ways-for-postfix-a-and-prefix.
coderlearner 1-May-22 7:28am    
sir is my code incomplete
Greg Utas 1-May-22 7:41am    
I was wondering what the getch at the end of your code was for and then realized it's probably just to stop the program from exiting until you enter something on the keyboard.
coderlearner 1-May-22 7:45am    
Sir it was taught in my class to use getch if we use conio.h as a header file
Greg Utas 1-May-22 7:52am    
Code needs to be there for a reason. If you removed the getch, you could also remove the #include conio.h. But then your program would exit immediately after performing its work, so you wouldn't be able to inspect what it had done. The getch is basically there as a debugging breakpoint. First time I've seen this. :)
There's no obvious reason to use a friend declaration in this example, is there?
C++
class sample
{
public:
	sample():x(0) { };
	void display();
	void operator ++ () { ++x; }    // prefix
	void operator ++ (int) { x++; } // postfix
private:
	int x;
};
 
Share this answer
 
Comments
Greg Utas 1-May-22 11:21am    
True. But he posted a follow-up "code plz" question which revealed that this was for an exam that specifically requested him to demonstrate a unary operator being implemented as a friend. That question was subsequently closed for not being a proper question.
Quote:
sir is my code incomplete?
Yes it is. At least from a 'canonical' point of view: Postfix (and prefix as well) increment operators shouldn't return void.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900