Class member variables/functions

"Use the Employer and PersonalInfo classes as member variables of a Person class. Modify the menu interface and input algorithms to accept data for the Employer and Personal Info classes via functions in the Person class."

I have 2 questions.
1. How would I use the employer and personalinfo classes as member variables in Person class?
2. Person.h lines 23-26 and lines 28-31. Lines 23-26 I want to refer to the PersonalInfo class. Lines 28-31 I want to refer to the Employer class. But I want them both to be called through Person. Is that possible? I may (probably) be doing this wrong...

Employer.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
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#ifndef EMPLOYER_H
#define EMPLOYER_H

class Employer
{
private:
    string firstName;
    string lastName;
    string phoneNum;
    string email;
public:
    Employer()
    {
    }
    
    Employer(string fnameIn, string lnameIn, string phoneIn, string emailIn)
            :firstName(fnameIn), lastName(lnameIn), phoneNum(phoneIn), email(emailIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getTelephone();
    string getEmail();
};

#endif /* EMPLOYER_H */  


Employer.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
42
43
#include "Employer.h"
#include <string>
using namespace std;

void Employer::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Employer::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Employer::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

void Employer::setEmail(string emailIn)
{
    email = emailIn;
}

string Employer::getFirstName()
{
return firstName;
}

string Employer::getLastName()
{
return lastName;
}

string Employer::getTelephone()
{
return phoneNum;
}

string Employer::getEmail()
{
return email;
}


PersonalInfo.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
26
27
28
29
30
31
32
33
34
35
36
37
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#ifndef PERSONALINFO_H
#define PERSONALINFO_H

class PersonalInfo
{
private:
    string firstName;
    string lastName;
    string age;
    string phoneNum;
public:
    PersonalInfo()
    {
    }
    
    PersonalInfo(string fnameIn, string lnameIn, string ageIn, string phoneIn)
            :firstName(fnameIn), lastName(lnameIn), age(ageIn), phoneNum(phoneIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setAge(string ageIn);
    void setTelephone(string phoneIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getAge();
    string getTelephone();
};

#endif /* PERSONALINFO_H */  


PersonalInfo.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
42
43
#include "PersonalInfo.h"
#include <string>
using namespace std;

void PersonalInfo::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void PersonalInfo::setLastName(string nameIn)
{
    lastName = nameIn;
}

void PersonalInfo::setAge(string ageIn)
{
    age = ageIn;
}

void PersonalInfo::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

string PersonalInfo::getFirstName()
{
return firstName;
}

string PersonalInfo::getLastName()
{
return lastName;
}

string PersonalInfo::getAge()
{
return age;
}

string PersonalInfo::getTelephone()
{
return phoneNum;
}


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <cstdlib>
#include <iostream>
#include <string>
#include "Employer.h"
#include "PersonalInfo.h"
using namespace std;

#ifndef PERSON_H
#define PERSON_H

class Person
{
private:
    string firstName;
    string lastName;
    string age;
    string phoneNum;
    string email;
public:
    Person()
    {
    }
    Person(string fnameIn, string lnameIn, string ageIn, string phoneIn)
            :firstName(fnameIn), lastName(lnameIn), age(ageIn), phoneNum(phoneIn)
    {
    }
    
    Person(string fnameIn, string lnameIn, string phoneIn, string emailIn)
            :firstName(fnameIn), lastName(lnameIn), phoneNum(phoneIn), email(emailIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setAge(string ageIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getAge();
    string getTelephone();
    string getEmail();
};

#endif /* PERSON_H */  


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "Person.h"
#include "Employer.h"
#include "PersonalInfo.h"
#include <string>
using namespace std;

void Person::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Person::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Person::setAge(string ageIn)
{
    age = ageIn;
}

void Person::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

void string Person::setEmail(string emailIn)
{
    email = emailIn;
}

string Person::getFirstName()
{
return firstName;
}

string Person::getLastName()
{
return lastName;
}

string Person::getAge()
{
return age;
}

string Person::getTelephone()
{
return phoneNum;
}

string Person::getEmail()
{
return email;
}


main.cpp just to show what all these classes are going into.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include "Person.h"
using namespace std;

class Recordsbook
{
    vector<Person> book;
    int x;
    Person current;
public:
    void Menu()
    {
        int choice = 0;
        cout << "---Main Menu---\n";
        cout << "1. Input information\n";
        cout << "2. Display records\n";
        cout << "3. Exit\n";
        try
        {
            cin >> choice;
            if(!(choice>=1 && choice<=3))
                throw out_of_range("Your choice is out of range.");
        }
        catch(out_of_range&)
        {
            cerr << "An error occurred. Please enter a valid menu number.\n";
            cin >> choice;
        }
        switch (choice)
        {
            case 1:
                InputInfo();
                break;
            case 2:
                Display();
                break;
            case 3:
                Exit();
                break;
        }
    }
    void InputInfo()
    {
        string temp;
        cout << "How many records would you like to input?";
        cin >> x;
        while(x<10)
        {
            cout << "You must input at least 10 records. Please input a bigger value.\n";
            cin >> x;
        }
        for(int i = 0; i<x; i++)
            {
                cout << "What is the first name? ";
                cin >> temp;
                current.setFirstName(temp);
                cout << "What is the last name? ";
                cin >> temp;
                current.setLastName(temp);
                cout << "What is the age? ";
                cin >> temp;
                current.setAge(temp);
                cout << "What is the telephone number? ";
                cin >> temp;
                current.setTelephone(temp);
            }
        Menu();
    };
    void Display()
    {
        int i = 0;
        for(i=0; i<x; i++)
            {
                cout<< "First name: ";
                cout<<current.getFirstName();
                cout<<"\n";
                cout<< "Last name: ";
                cout<<current.getLastName();
                cout<<"\n";
                cout<< "Age: ";
                cout<<current.getAge();
                cout<<"\n";
                cout<< "Telephone number: ";
                cout<<current.getTelephone();
                cout<<"\n";
                cout<<"\n";
            }
        Menu();
    };
    void Exit()
    {
        cout << "Closing...";
    };
} Recordsbook;

int main()
{
    Recordsbook.Menu();
}

Last edited on
I think they mean something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Employer {
private:
    string email;
    // etc.
};

class PersonalInfo {
private:
    string firstName;
    string lastName;
    unsigned age;		// note that age is not a string
    string phoneNum;
    // etc;
};


class Person {
public:
    PersonalInfo info;
    Employer employer;
};


Hmm. It compiles now. But. Is there a way that I can get the constructors in Person to refer to the info in PersonalInfo and Employer?
Or is that just not possible? Because it feels like I'm not even using Employer or PersonalInfo...

Employer.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
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#ifndef EMPLOYER_H
#define EMPLOYER_H

class Employer
{
    friend class Person;
private:
    string firstName;
    string lastName;
    string phoneNum;
    string email;
public:
    Employer(string fnameIn, string lnameIn, string phoneIn, string emailIn)
            :firstName(fnameIn), lastName(lnameIn), phoneNum(phoneIn), email(emailIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getTelephone();
    string getEmail();
};

#endif /* EMPLOYER_H */  

Employer.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
42
43
#include "Employer.h"
#include <string>
using namespace std;

void Employer::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Employer::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Employer::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

void Employer::setEmail(string emailIn)
{
    email = emailIn;
}

string Employer::getFirstName()
{
return firstName;
}

string Employer::getLastName()
{
return lastName;
}

string Employer::getTelephone()
{
return phoneNum;
}

string Employer::getEmail()
{
return email;
}


PersonalInfo.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
26
27
28
29
30
31
32
33
34
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#ifndef PERSONALINFO_H
#define PERSONALINFO_H

class PersonalInfo
{
    friend class Person;
private:
    string firstName;
    string lastName;
    string age;
    string phoneNum;
public:
    PersonalInfo(string fnameIn, string lnameIn, string ageIn, string phoneIn)
            :firstName(fnameIn), lastName(lnameIn), age(ageIn), phoneNum(phoneIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setAge(string ageIn);
    void setTelephone(string phoneIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getAge();
    string getTelephone();
};

#endif /* PERSONALINFO_H */  


PersonalInfo.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
42
43
#include "PersonalInfo.h"
#include <string>
using namespace std;

void PersonalInfo::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void PersonalInfo::setLastName(string nameIn)
{
    lastName = nameIn;
}

void PersonalInfo::setAge(string ageIn)
{
    age = ageIn;
}

void PersonalInfo::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

string PersonalInfo::getFirstName()
{
return firstName;
}

string PersonalInfo::getLastName()
{
return lastName;
}

string PersonalInfo::getAge()
{
return age;
}

string PersonalInfo::getTelephone()
{
return phoneNum;
}


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <string>
#include "Employer.h"
#include "PersonalInfo.h"
using namespace std;

#ifndef PERSON_H
#define PERSON_H

class Person
{
    friend class Employer;
    friend class PersonalInfo;
private:
    string firstName;
    string lastName;
    string phoneNum;
    string age;
    string email;
public:
    Person()
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setAge(string ageIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getAge();
    string getTelephone();
    string getEmail();
};

#endif /* PERSON_H */  


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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "Person.h"
#include "Employer.h"
#include "PersonalInfo.h"
#include <string>
using namespace std;

void Person::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Person::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Person::setAge(string ageIn)
{
    age = ageIn;
}

void Person::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

void Person::setEmail(string emailIn)
{
    email = emailIn;
}

string Person::getFirstName()
{
return firstName;
}

string Person::getLastName()
{
return lastName;
}

string Person::getAge()
{
return age;
}

string Person::getTelephone()
{
return phoneNum;
}

string Person::getEmail()
{
return email;
}


Also noticed while compiling that when outputting the files, it only outputs whatever was last input....
Last edited on
Did you see my post? You didn't seem to take my advice.
Like this @dhayden? It gives me that error message.
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <cstdlib>
#include <iostream>
#include <string>
#include "Employer.h"
#include "PersonalInfo.h"
using namespace std;

#ifndef PERSON_H
#define PERSON_H

class Person
{
private:
    string firstName;
    string lastName;
    string age;
    string phoneNum;
    string email;
public:
    PersonalInfo info;
    Employer employer;
    
    Person()
    {
    }
    Person(string fnameIn, string lnameIn, string ageIn, string phoneIn)
            :firstName(fnameIn), lastName(lnameIn), age(ageIn), phoneNum(phoneIn)
    {
    }
    
    Person(string fnameIn, string lnameIn, string phoneIn, string emailIn)
            :firstName(fnameIn), lastName(lnameIn), phoneNum(phoneIn), email(emailIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setAge(string ageIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getAge();
    string getTelephone();
    string getEmail();
};

#endif /* 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
cd 'C:\Users\Mouse\Documents\NetBeansProjects\Mpierce_Mod2Asmt1_071617_2'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/mpierce_mod2asmt1_071617_2.exe
make[2]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/Person.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/Person.o.d" -o build/Debug/Cygwin-Windows/Person.o Person.cpp
In file included from Person.cpp:1:0:
Person.h:31:5: error: 'Person::Person(std::string, std::string, std::string, std::string)' cannot be overloaded
     Person(string fnameIn, string lnameIn, string phoneIn, string emailIn)
     ^~~~~~
Person.h:26:5: error: with 'Person::Person(std::string, std::string, std::string, std::string)'
     Person(string fnameIn, string lnameIn, string ageIn, string phoneIn)
     ^~~~~~
make[2]: *** [nbproject/Makefile-Debug.mk:77: build/Debug/Cygwin-Windows/Person.o] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
make[1]: *** [nbproject/Makefile-Debug.mk:63: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod2Asmt1_071617_2'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

BUILD FAILED (exit value 2, total time: 3s)
Last edited on
Employer.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
26
27
28
29
30
31
32
33
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#ifndef EMPLOYER_H
#define EMPLOYER_H

class Employer
{
private:
    string firstName;
    string lastName;
    string phoneNum;
    string email;
public:
    Employer()
    {
    }
    
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getTelephone();
    string getEmail();
};

#endif /* EMPLOYER_H */  


Employer.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
42
43
#include "Employer.h"
#include <string>
using namespace std;

void Employer::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Employer::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Employer::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

void Employer::setEmail(string emailIn)
{
    email = emailIn;
}

string Employer::getFirstName()
{
return firstName;
}

string Employer::getLastName()
{
return lastName;
}

string Employer::getTelephone()
{
return phoneNum;
}

string Employer::getEmail()
{
return email;
}


PersonalInfo.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
26
27
28
29
30
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

#ifndef PERSONALINFO_H
#define PERSONALINFO_H

class PersonalInfo
{
private:
    string firstName;
    string lastName;
    string phoneNum;
public:
    PersonalInfo()
    {
    }
    
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setTelephone(string phoneIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getTelephone();
};

#endif /* PERSONALINFO_H */  


PersonalInfo.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
#include "PersonalInfo.h"
#include <string>
using namespace std;

void PersonalInfo::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void PersonalInfo::setLastName(string nameIn)
{
    lastName = nameIn;
}

void PersonalInfo::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

string PersonalInfo::getFirstName()
{
return firstName;
}

string PersonalInfo::getLastName()
{
return lastName;
}

string PersonalInfo::getTelephone()
{
return phoneNum;
}


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <cstdlib>
#include <iostream>
#include <string>
#include "Employer.h"
#include "PersonalInfo.h"
using namespace std;

#ifndef PERSON_H
#define PERSON_H

class Person
{
    friend class Employer;
    friend class PersonalInfo;
private:
    string firstName;
    string lastName;
    string phoneNum;
    string email;
public:
    PersonalInfo info;
    Employer employer;
    Person()
    {
    }
    Person(string fnameIn, string lnameIn, string phoneIn, string emailIn)
            :firstName(fnameIn), lastName(lnameIn), phoneNum(phoneIn), email(emailIn)
    {
    }
    //set functions
    void setFirstName(string nameIn);
    void setLastName(string nameIn);
    void setTelephone(string phoneIn);
    void setEmail(string emailIn);
    //get functions
    string getFirstName();
    string getLastName();
    string getTelephone();
    string getEmail();
};

#endif /* PERSON_H */ 


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
42
43
44
45
#include "Person.h"
#include "Employer.h"
#include "PersonalInfo.h"
#include <string>
using namespace std;

void Person::setFirstName(string nameIn)
{
    firstName = nameIn;
}

void Person::setLastName(string nameIn)
{
    lastName = nameIn;
}

void Person::setTelephone(string phoneIn)
{
    phoneNum = phoneIn;
}

void Person::setEmail(string emailIn)
{
    email = emailIn;
}

string Person::getFirstName()
{
return firstName;
}

string Person::getLastName()
{
return lastName;
}

string Person::getTelephone()
{
return phoneNum;
}

string Person::getEmail()
{
return email;
}


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include "Person.h"
using namespace std;

class Recordsbook
{
    vector<Person> book;
    int x;
    Person current;
public:
    void Menu()
    {
        int choice = 0;
        cout << "---Main Menu---\n";
        cout << "1. Input information\n";
        cout << "2. Display records\n";
        cout << "3. Exit\n";
        try
        {
            cin >> choice;
            if(!(choice>=1 && choice<=3))
                throw out_of_range("Your choice is out of range.");
        }
        catch(out_of_range&)
        {
            cerr << "An error occurred. Please enter a valid menu number.\n";
            cin >> choice;
        }
        switch (choice)
        {
            case 1:
                InputInfo();
                break;
            case 2:
                Display();
                break;
            case 3:
                Exit();
                break;
        }
    }
    void InputInfo()
    {
        string temp;
        cout << "How many records would you like to input?";
        cin >> x;
        while(x<10)
        {
            cout << "You must input at least 10 records. Please input a bigger value.\n";
            cin >> x;
        }
        for(int i = 0; i<x; i++)
            {
                cout << "What is the first name? ";
                cin >> temp;
                current.setFirstName(temp);
                cout << "What is the last name? ";
                cin >> temp;
                current.setLastName(temp);
                cout << "What is the telephone number? ";
                cin >> temp;
                current.setTelephone(temp);
                cout << "What is the email? ";
                cin >> temp;
                current.setEmail(temp);
            }
        Menu();
    };
    void Display()
    {
        int i = 0;
        for(i=0; i<x; i++)
            {
                cout<< "First name: ";
                cout<<current.getFirstName();
                cout<<"\n";
                cout<< "Last name: ";
                cout<<current.getLastName();
                cout<<"\n";
                cout<< "Telephone number: ";
                cout<<current.getTelephone();
                cout<<"\n";
                cout<< "Email: ";
                cout<<current.getEmail();
                cout<<"\n";
                cout<<"\n";
            }
        Menu();
    };
    void Exit()
    {
        cout << "Closing...";
    };
} Recordsbook;

int main()
{
    Recordsbook.Menu();
}



How would I make the for loop in void Display() in main.cpp print out the whole vector?
Last edited on
Use the Employer and PersonalInfo classes as member variables of a Person class

I think you're missing the point of what this means. Since the PersonalInfo class has first name, last name and phone number, you don't need to duplicate those members in the Person class. The only members that seem to be needed in the person class are a PersonalInfo and an Employer. That's what I showed in my original post.

It looks like you've given part of a larger assignment. Can you post the full assignment, including whatever they say about the PersonalInfo and Employer classes? Right now you also have duplicate info in PersonalInfo and Employer.
"Modify the Vector address book program you created in the previous module by creating a Person Class, an Employer Class and a Personal Info Class. Use the Employer and PersonalInfo classes as member variables of a Person class. Modify the Vector class to hold People objects instead of Record objects. Modify the menu interface and input algorithms to accept data for the Employer and Personal Info classes via functions in the Person class. Modify the Output to print all the information in each Person object, associated Employer object, and associated Personal Info object. Make sure to separate the implementation of your classes from the header files. You should have a .cpp file and a .h file for each class you create."

That's the entire assignment. They don't even say what should be in employer or personalinfo.. (The Records class mentioned is very similar to my personalinfo class.) So, in Person.h, all I need is employer employer and personalinfo info and that's it? What would go in Person.cpp then..?
Last edited on
@Griffinflam21,
Yes, it seems as if you have provided more information about your problem. Without asking more question, I assume that Employer gets information from PersonalInfo (I see that the only extra stuff for Employee is email)

I made something like this
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
	blongho, 2017-07-23
	Problem from http://www.cplusplus.com/forum/beginner/219213/
	Trying to find a fix
*/
// PersonalInfo.h
#ifndef PERSONALINFO_H
#define PERSONALINFO_H

#include <iostream>
#include <string>

class PersonalInfo
{
private:
	std::string firstName;
	std::string lastName;
	unsigned age;
	std::string phoneNum;
public:
	PersonalInfo(){}
	PersonalInfo(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn)
		:firstName{fnameIn}, lastName{lnameIn}, age{ageIn}, phoneNum{phoneIn}
	{}
	//set functions
	void setFirstName(std::string nameIn);
	void setLastName(std::string nameIn);
	void setAge(unsigned ageIn);
	void setTelephone(std::string phoneIn);
	//get functions
	std::string getFirstName() const;
	std::string getLastName() const;
	unsigned getAge() const;
	std::string getTelephone() const ;
};

#endif /* PERSONALINFO_H */  

// PersonalInfo.cpp
#include "PersonalInfo.h"

void PersonalInfo::setFirstName(std::string nameIn)
{
	firstName = nameIn;
}

void PersonalInfo::setLastName(std::string nameIn)
{
	lastName = nameIn;
}

void PersonalInfo::setAge(unsigned ageIn)
{
	age = ageIn;
}

void PersonalInfo::setTelephone(std::string phoneIn)
{
	phoneNum = phoneIn;
}

std::string PersonalInfo::getFirstName() const
{
	return firstName;
}

std::string PersonalInfo::getLastName() const
{
	return lastName;
}

unsigned PersonalInfo::getAge() const
{
	return age;
}

std::string PersonalInfo::getTelephone() const
{
	return phoneNum;
}

// Employer.h
#ifndef EMPLOYER_H
#define EMPLOYER_H
#include "PersonalInfo.h"

class Employer
{
private:
   std::string email;
   PersonalInfo pers;
public:
    Employer(){}

	Employer(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn)
		:pers(fnameIn, lnameIn, ageIn, phoneIn){}

	// take note of this one! It will be used in Person
	Employer(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn, std::string mail)
		:pers(fnameIn, lnameIn, ageIn, phoneIn), email{ mail } {}
    
    //set functions
	/*
		if you must have all the functions from personalInfo here, it is easy e.g
		void setFirstName(string fname){pers.setFirstName(fname);}
		strint getFirstName(){return pers.getFirstName();}
                
               see how I did it in Person.cpp
	*/
    void setEmail(std::string emailIn);
    //get functions
   
    std::string getEmail()const;
};

#endif /* EMPLOYER_H */  

// Employer.cpp
#include "Employer.h"


void Employer::setEmail(std::string emailIn)
{
	email = emailIn;
}

std::string Employer::getEmail()const
{
	return email;
}

// Person.h
#ifndef PERSON_H
#define PERSON_H
#include "PersonalInfo.h"
#include "Employer.h"

class Person
{
private:
	PersonalInfo info;
	Employer employer;
public:
	Person(){}
	Person(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn)
		:info(fnameIn, lnameIn, ageIn, phoneIn){}
	
	// constructor to get info for employer
	Person(std::string fnameIn, std::string lnameIn, unsigned ageIn, std::string phoneIn, std::string email)
		:employer(fnameIn, lnameIn, ageIn, phoneIn, email) {}

	//set functions
	void setFirstName(std::string nameIn);
	void setLastName(std::string nameIn);
	void setAge(unsigned ageIn);
	void setTelephone(std::string phoneIn);
	void setEmail(std::string emailIn);

	//get functions
	std::string getFirstName()const { return info.getFirstName(); }
	std::string getLastName()const { return info.getLastName(); }
	unsigned getAge() const { return info.getAge(); }
	std::string getTelephone()const { return info.getTelephone(); }
	std::string getEmail()const { return employer.getEmail(); }
};
std::ostream &operator<<(std::ostream &os, const Person &person);
#endif /* PERSON_H */  

// Person.cpp
#include "Person.h"

void Person::setFirstName(std::string nameIn)
{
	info.setFirstName(nameIn);
}

void Person::setLastName(std::string nameIn)
{
	info.setLastName(nameIn);
}

void Person::setAge(unsigned ageIn)
{
	info.setAge(ageIn);
}

void Person::setTelephone(std::string phoneIn)
{
	info.setTelephone(phoneIn);
}

void Person::setEmail(std::string emailIn)
{
	employer.setEmail(emailIn);
}

// overload Person object so it will be easy to print
std::ostream & operator<<(std::ostream & os, const Person & person)
{
	os << "First Name : " << person.getFirstName() << std::endl
		<< "Last Name  : " << person.getLastName() << std::endl
		<< "Age        : " << person.getAge() << std::endl
		<< "Telephone  : " << person.getTelephone() << std::endl;

	// if person doesnt have an email, print empty line
	if (person.getEmail() == "") {
		os << "";
	}
	else {
		os << "Email      : " << person.getEmail() << std::endl;
	}
		
	os<< std::endl;
	return os;
}

//>"You should have a .cpp file and a .h file for each class you create."
// make another class for records (People, I call it)
#ifndef PEOPLE_H
#define PEOPLE_H
#include "Person.h"
#include <vector>

class People
{
private:
	std::vector<Person> persons;
public:
	People(): persons{}{}
	~People() {};

	int getPeopleCount() const { return persons.size(); }

	void addPersons();
	void showPersons();
	void menu();
};

#endif // !PEOPLE_H

// People.cpp
#include "People.h"
using std::cout;
using std::endl;
using std::cin;
using std::getline;
using std::vector;
using std::string;


void People::addPersons()
{
	int entries = 0;
	cout << "How many records do you want to input?: ";
	cin >> entries;

	// value of entries must be atleast	10: For demo, i will use two
	while (entries < 2) {
		cout << "You must input at least 10 records. Please input a bigger value.\n";
		cin >> entries;
	}

	cin.get();

	string temp;
	Person tmpPerson;
	for (int i = 0; i< entries; i++){
		cout << endl << "Enter data for person #" << 1 + i << endl;
		cout << "What is the first name? ";
		getline(cin, temp);
		tmpPerson.setFirstName(temp);

		cout << "What is the last name? ";
		getline(cin, temp);
		tmpPerson.setLastName(temp);

		cout << "What is the age? ";
		getline(cin, temp);
		tmpPerson.setAge(stoi(temp));  // convert string to int
		
		cout << "What is the telephone number? ";
		getline(cin, temp);
		tmpPerson.setTelephone(temp);

		cout << "What is your email? ";
		getline(cin, temp);
		tmpPerson.setEmail(temp);

		persons.push_back(tmpPerson);

		cout << endl;
	}
}

void People::showPersons()
{
	if (getPeopleCount() == 0) {
		cout << "There are no entries yet!" << endl;
	}
	else {
		cout << "Person on record : " << getPeopleCount() << endl;
		cout << "------------------------------------" << endl;
		for (auto &person : persons){
			cout << person << endl;
		}
	}
}
void People::menu()
{
	bool again = true;
	do {
		int choice = 0;
		cout << "---Main Menu---\n";
		cout << "1. Input information\n";
		cout << "2. Display records\n";
		cout << "3. Exit\n";

		cout << "Your choice : ";
		cin >> choice;

		cin.get();

		switch (choice){
		case 1:
			addPersons();
			again = true;
			break;
		case 2:
			showPersons();
			again = true;
			break;
		case 3:
			cout << "Exiting program..." << endl;
			again = false;
			break;
		}
	} while (again);
	
}

// main.cpp
#include "People.h"

int main()
{
	People people;
	people.menu();
	return 0;
}

I don't know if you have covered inheritance/polymorphism. The classes could have been way simpler as dhayden pointed out.

Note: There are no checks for age! You enter something that stoi cannot convert to int will crash the program.

Hope you get a better idea now.
Last edited on
I haven't learned about inheritance or polymorphism yet. I think I'm learning about that next week..
But wow thank you! I will definitely look that over. This is my second time using the whole .h and .cpp separated files thing, let alone using functions in one that is calling info from the others...so I'm not familiar with it. Along with the assignment not specifying what is supposed to go in PersonalInfo and Employer, kind of has me really lost about how to do the whole assignment properly.

Also, stoi?

The original Record class uses record number, first name, last name, age, and telephone number. I added email in because....I don't remember why, I think I was trying to make Employer a separate type of contact from PersonalInfo.

Edit:
@blongho
Tried your code, but it fails to compile. This is what it says
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
cd 'C:\Users\Mouse\Documents\NetBeansProjects\Mpierce_Mod3Asmt_072317'
C:\cygwin64\bin\make.exe -f Makefile CONF=Debug
"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make[1]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod3Asmt_072317'
"/usr/bin/make"  -f nbproject/Makefile-Debug.mk dist/Debug/Cygwin-Windows/mpierce_mod3asmt_072317.exe
make[2]: Entering directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod3Asmt_072317'
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/Employer.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/Employer.o.d" -o build/Debug/Cygwin-Windows/Employer.o Employer.cpp
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/Person.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/Person.o.d" -o build/Debug/Cygwin-Windows/Person.o Person.cpp
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/PersonalInfo.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/PersonalInfo.o.d" -o build/Debug/Cygwin-Windows/PersonalInfo.o PersonalInfo.cpp
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/Record.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/Record.o.d" -o build/Debug/Cygwin-Windows/Record.o Record.cpp
mkdir -p build/Debug/Cygwin-Windows
rm -f "build/Debug/Cygwin-Windows/main.o.d"
g++    -c -g -MMD -MP -MF "build/Debug/Cygwin-Windows/main.o.d" -o build/Debug/Cygwin-Windows/main.o main.cpp
mkdir -p dist/Debug/Cygwin-Windows
g++     -o dist/Debug/Cygwin-Windows/mpierce_mod3asmt_072317 build/Debug/Cygwin-Windows/Employer.o build/Debug/Cygwin-Windows/Person.o build/Debug/Cygwin-Windows/PersonalInfo.o build/Debug/Cygwin-Windows/Record.o build/Debug/Cygwin-Windows/main.o 
build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod3Asmt_072317/main.cpp:6: undefined reference to `People::menu()'
/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod3Asmt_072317/main.cpp:6:(.text+0x27): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `People::menu()'
collect2: error: ld returned 1 exit status
make[2]: *** [nbproject/Makefile-Debug.mk:67: dist/Debug/Cygwin-Windows/mpierce_mod3asmt_072317.exe] Error 1
make[2]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod3Asmt_072317'
make[1]: *** [nbproject/Makefile-Debug.mk:63: .build-conf] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/Mouse/Documents/NetBeansProjects/Mpierce_Mod3Asmt_072317'
make: *** [nbproject/Makefile-impl.mk:40: .build-impl] Error 2

BUILD FAILED (exit value 2, total time: 22s) 
Last edited on
Well, i am very sorry if the program doesn't compile. I am using Microsoft Visual Studio (VS) 2017 and the program compiles hitch-free. No warnings!

You might want to check if your compiler can work with C++ 11.

IMHO, i can can decipher what is wrong that you don't get the program running.

This is my second time using the whole .h and .cpp separated files thing
It is not complicated once you do it from the second time. In PersonalInfo.h
since you already have
1
2
#include <iostream>
#include <string> 

Adding #include "PersonalInfo.h" means all the includes in the .h files are "carried" there too. It is useless taking them there again.

stoi is an in-built function in C++ that converts string to interger. Read more here http://www.cplusplus.com/reference/string/stoi/

The original Record class uses record number, first name, last name, age, and telephone number.

This means the class will be something like this
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#ifndef PEOPLE_H
#define PEOPLE_H
#include "Person.h"
#include <vector>

class People
{
private:
	std::vector<Person> persons;
	int count;
public:
	People() : persons{}, count{ 0 } {}
	~People() {};

	int getPeopleCount() const { return persons.size(); }

	int getCount() const {return count; }
	void addPersons();
	void showPersons();
	void menu();
};

#endif // !PEOPLE_H

//And then modify these  in People.cpp
void People::addPersons()
{
	int entries = 0;
	cout << "How many records do you want to input?: ";
	cin >> entries;

	// value of entries must be atleast	10: For demo, i will use two
	while (entries < 2) {
		cout << "You must input at least 10 records. Please input a bigger value.\n";
		cin >> entries;
	}

	cin.get();

	string temp;
	Person tmpPerson;
	for (int i = 0; i< entries; i++) {
		cout << endl << "Enter data for person #" << 1 + i << endl;
		cout << "What is the first name? ";
		getline(cin, temp);
		tmpPerson.setFirstName(temp);

		cout << "What is the last name? ";
		getline(cin, temp);
		tmpPerson.setLastName(temp);

		cout << "What is the age? ";
		//getline(cin, temp);
		//tmpPerson.setAge(stoi(temp));  // convert string to int

		//if stoi is a problem with your compiler
		int age{ 0 };
		cin >> age;
		tmpPerson.setAge(age);

		cin.get();

		cout << "What is the telephone number? ";
		getline(cin, temp);
		tmpPerson.setTelephone(temp);

		cout << "What is your email? ";
		getline(cin, temp);
		tmpPerson.setEmail(temp);

		persons.push_back(tmpPerson);

		++count; // increment count
		cout << endl;
	}
}

// use count here. Count is actually useless since person.size() == count
void People::showPersons()
{
	if (count == 0) { // if(persons.size() == 0) OR if(person.empty()) works too
		cout << "There are no entries yet!" << endl;
	}
	else {
		cout << "Person on record : " << count << endl;
		cout << "------------------------------------" << endl;
		for (auto &person : persons) {
			cout << person << endl;
		}
	}
}


It seems as if there is much information lacking in your program. If Employer has nothing addition, then i don't see the use here.

I am however guessing that, you are reading a book and this exercise is to make you wonder like assembling a plane.

This
Modify the Vector class to hold People objects instead of Record objects.

That is why i changed the class from Records to People

And this
Modify the menu interface and input algorithms to accept data for the Employer and Personal Info classes via functions in the Person class. Modify the Output to print all the information in each Person object, associated Employer object, and associated Personal Info object.


I think requires inheritance. If you have not covered that, then i guess you wait.

My solution(with assumption that you don't need inheritance) does not fullfil this last criterion.

Maybe our elders in the house can correct us. I am also an upper beginner(whatever that means).

Last edited on
The professor didn't really go into enough detail with the instructions, sadly. I don't understand why I even need the employer class or what goes in it, but the assignment says to have an employer class so.. :/ I asked, but no reply.

I'm using NetBeans. And yes, it does work with C++ 11 but I've never had this error before.

It is still giving the same errors. For some reason, not during compiling, it shows errors in People.cpp. There are quite a few but I think the most important one is 'Cannot find include file "People.h"'. That might fix all the other problems.
And 'Unable to resolve identifier size in People.h....
I've been working on it. I have one question.
1
2
3
4
5
6
7
8
9
10
11
12
13
void People::showPersons()
{
	if (count == 0) { // if(persons.size() == 0) OR if(person.empty()) works too
		cout << "There are no entries yet!" << endl;
	}
	else {
		cout << "Person on record : " << count << endl;
		cout << "------------------------------------" << endl;
		for (auto &person : persons) {
			cout << person << endl;
		}
	}
}


I don't understand the "for( auto &person : persons) part
could you explain that? What's different between person and persons, what does the : do?



EDIT:
I fixed the error....somehow! And it works! Thanks everyone for the help. Honestly never would have gotten this done without y'alls help. Much, much appreciated.
Last edited on
Glad you had it working. "Fixing" such kind of problem is part of programming.

The for(auto &person: persons) is a C++11 "cleaner" way for looping through a vector(and some std containers that have iterator). It is called range-for loop.
Read it here http://en.cppreference.com/w/cpp/language/range-for

Diff btn person and persons?
persons is a vector containing many person object. See People class.

for(auto &person: persons) could still be for(auto &stone: persons). I used person there so the loop can look as understandable as possible.

That said, the same range-for loop could be written as(which is ALSO correct) as
1
2
3
for (unsigned i = 0; i <count;i++){/*count is preferrably persons.size()*/
     cout<<persons[i]<< endl;
}


Hope you get a clearer mind now.
Good luck.
Last edited on
Topic archived. No new replies allowed.