Operator returnig values
May 16, 2015 at 12:11am UTC
Hi all.
I am studying c++. I wrote this simple code trying to define an operator in my class. But I expected to see the output "I am 22 years old" and I am reading "I am 18 yars old".
Am I doing something wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include "stdafx.h"
#include "math.h"
#include "stdio.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class person
{
public :
int id_number;
int age;
person()
{
id_number = 0;
age = 18;
}
person operator = (person N)
{
person ret;
ret.age = N.age;
ret.id_number = N.id_number;
return ret;
}
};
int main()
{
person Tom;
Tom.id_number = 1234;
Tom.age = 22;
person myself;
myself = Tom;
cout<< "I am " << myself.age <<" years old." ;
getchar();
return 0;
}
May 16, 2015 at 2:15am UTC
Use your operator member as follows:
1 2 3 4 5
person& operator =(person& N){
aga= N.aga;
id_number= N.id_number;
return *this ;
}
Topic archived. No new replies allowed.