lundi 6 mars 2017

Why is the output is coming out to be zero for my friend operation overloaded code

When I am trying to run this code I am getting the output to be zero which isn't expected, according to what I have defined I should get a -1 rather than a 0,I have tried changing the values of x,I have tried looking up for the garbage value if any. What you guys think?

#include<iostream>
using namespace std;
class nf{
int x;
public:
nf()
{
    x=1;
}
nf(int a){
    a=x;
}
friend nf operator ++(nf n1)
 {
 n1.x=-n1.x;
 }
 void display()
 {
  cout<<x;
 }
 };
int main()
{
nf obj(1),obj2;
obj2=++obj;
obj2.display();
}

Aucun commentaire:

Enregistrer un commentaire