person

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?

person.cpp
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 "person.h"
#include <iostream> 
#include <iomanip> 
#include <fstream> 
#include <string>

using namespace std; 

Person::Person() { 

}

Person::Person(const Person& n){
	
	name = n.name;
	age = n.age+2;

}

Person& Person::operator= (const Person& n){
	name = n.name;
	age = n.age-2;
}

void Person::setName(string name){
	this->name = name;
}
	
void Person::setAge(int age){
	this->age = age;
}

void Person::print(){
	cout << "Name: " << name; 
	if (name == "Bob")
	cout << "\tAge: " << age << endl;
	else 
	cout << " Age: " << age << endl;

}


person.h
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
#ifndef __PERSON_H__
#define __PERSON_H__

#include <iostream>
#include <string>

using namespace std;

class Person
{
    public:
        Person();
        //copy constructor
        Person (const Person&);
        //overloaded assignment operator
        Person& operator = (const Person&);
        void setName(string);
        void setAge(int);
        void print();
    private:
        string name;
        int age;
};

#endif 


main.cpp
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
#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


but i want it to come out as:
1
2
3
4
5
6
Name:Bob		Age:50
Name:Bob		Age:52
Name:Bob		Age:48
Name:Susan	Age:40
Name:Susan	Age:42
Name:Susan	Age:38
Last edited on
Why do you want two tabs for Bob?

You need to put a check in something like:

if ( name == "Bob" ) cout << "\t";
(edited to change newline char to tab)

in between printing the name and the age.

And to remove the space between "Name:" and the name, and "Age:" and the age, just remove the spaces inside the quotes of your cout statements.
Last edited on
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?
Last edited on
Oh, right... missed the numbers - if that's what you want different, you need to change them.

So, in your main you need to do:

1
2
p3.setAge(52);
p5.setAge(48);


Etc.
code has been fixed
Last edited on
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.
Ok i got it :D i fixed the code above and now it works :D thank you for all your help and guidance pax and Tamao :D
Topic archived. No new replies allowed.