is the syntax used below is correct?

class zz
{
private :
string s;
//constructor
private:
friend zz operator-(zz z);
//assume definition of operator<<
};

//constructor

//operator<<

zz operator-(zz z)
{
string s;
s=z.s+"hello";
return zz(s);
}
int main()
{
zz a("fsd");
cout<<(-a);
}

i am new to c++ but just enthusiastic

Well, for one you are obfuscating your code to an extreme extent by not giving meaningful variable names. friend zz operator-(zz z); Secondly, the - operator is a binary operator. It accepts two operands, the left and the right. (-a) does nothing since you are using a binary operand as a unary operand (--a) which would decrement a if you overload properly, otherwise it decrements the pointer.

So which are you trying to accomplish, the binary operator( a-b ) or the unary operator( --a )?

The code -a is a unary operator, so he isn't trying to do anything weird.

The argument to your operator function should be a reference: zz operator - ( zz &z ) Otherwise it looks fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.