I have constructed the person.cpp while the main.cpp and person.h were given to me. My person.cpp is does not produce the exact results i was looking for. Can someone help me with this?
#include"person.h"
int main(){
Person p1;
p1.setName("Bob");
p1.setAge(50);
Person p2;
p2.setName("Susan");
p2.setAge(40);
//Exercise the copy constructor
Person p3(p1);
//Create a person and use the assignment operator
Person p5;
p5 = p1;
//Similar to above, but slightly different
Person p4=p2;
//Ditto.
Person p6;
p6 = p2;
p1.print();
p3.print();
p5.print();
p2.print();
p4.print();
p6.print();
}
When i run it i get
1 2 3 4 5 6
Name: Bob Age: 50
Name: Bob Age: 50
Name: Bob Age: 50
Name: Susan Age: 40
Name: Susan Age: 40
Name: Susan Age: 40
I can't really see where the 52,48,42 and 38 comes from all you do in the copy ctor and operator is assignment and i can't see you using those values in main or add/subtract from the current age. Are you sure it should be like that?
Well, looking just at Bob (since Susan is as we all know, a cow)...
p3 needs to be +2 to age, and uses the copy constructor.
p5 needs to be -2 and uses the operator= method.
If you adjust these methods, then you can get the desired result.
Though I've got to say... it's a strange assignment. Doesn't really fit with the point of such functions to have them change things. But if that's what you want to do.