Inheritance?

I'm trying to write 3 child classes for my parent class Employer. Those 3 child classes are Sales, Service, and Manufacturing. I want the child classes to be used, such as if the user inputs that the type of employee is a sales employee, I want variable emptype to output Sales, same for the other 2 child classes. I tried it but I got myself confused.. Here is my code so far. Sales.h, Service.h, and Manufacturing.h currently has no code inside yet, so I did not include them.


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

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

    Employer(string emptype, string fnameIn, string lnameIn, unsigned ageIn, string phoneIn)
	:pers(emptypeIn, fnameIn, lnameIn, ageIn, phoneIn){}
	// take note of this one! It will be used in Person
    Employer(string emptype, fnameIn, string lnameIn, unsigned ageIn, string phoneIn, string mail)
	:pers(emptypeIn, fnameIn, lnameIn, ageIn, phoneIn), email{ mail } {}
        
    //set functions           
    void setEmail(string emailIn);
    
    //get functions
    string getEmail()const;
};

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

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

void Employer::setEmpType(string emptypeIn)
{
	email = emptypeIn;
}

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

string Employer::getEmpType()const
{
	return emptype;
}


Sales.cpp
1
2
3
4
5
6
7
8
#include <cstdlib>
#include "Sales.h"
using namespace std;

void Employer::setEmptype(string emptypeIn)
{
	emptype = "Sales";
}


Service.cpp
1
2
3
4
5
6
7
8
#include <cstdlib>
#include "Service.h"
using namespace std;

void Employer::setEmptype(string emptypeIn)
{
	emptype = "Service";
}


Manufacturing.cpp
1
2
3
4
5
6
7
8
#include <cstdlib>
#include "Manufacturing.h"
using namespace std;

void Employer::setEmptype(string emptypeIn)
{
	emptype = "Manufacturing";
}


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
#ifndef PERSON_H
#define PERSON_H
#include "PersonalInfo.h"
#include "Employer.h"
#include "Sales.h"
#include "Service.h"
#include "Manufacturing.h"
using namespace std;
class Person
{
private:
    PersonalInfo info;
    Employer employer;
public:
    Person(){}
    Person(string emptypeIn, string fnameIn, string lnameIn, unsigned ageIn, string phoneIn)
	:info(emptypeIn, fnameIn, lnameIn, ageIn, phoneIn){}
	
    Person(string emptypeIn, fnameIn, string lnameIn, unsigned ageIn, string phoneIn, string email)
	:employer(emptypeIn, fnameIn, lnameIn, ageIn, phoneIn, email) {}

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

    //get functions
    string getFirstName()const { return info.getFirstName(); }
    string getLastName()const { return info.getLastName(); }
    unsigned getAge() const { return info.getAge(); }
    string getTelephone()const { return info.getTelephone(); }
    string getEmail()const { return employer.getEmail(); }
};
ostream &operator<<(ostream &os, const Person &person);
#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
#include "Person.h"
using namespace std;

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

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

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

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

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

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

    if (person.getEmail() == "") 
    {
	os << "";
    }
    else 
    {
	os << "Email     : " << person.getEmail();
    }	
    os<< endl;
    return os;
}


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
#ifndef PERSONALINFO_H
#define PERSONALINFO_H

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

#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
#include "PersonalInfo.h"
using namespace std;
void PersonalInfo::setFirstName(string nameIn)
{
    firstName = nameIn;
}

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

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

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

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

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

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

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


People.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
#ifndef PEOPLE_H
#define PEOPLE_H
#include "Person.h"
#include <vector>
using namespace std;

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

    int getPeopleCount() const { return persons.size(); }
    int getCount() const { count; }
    
    void addPersons();
    void showPersons();
    void menu();
};

#endif // !PEOPLE_H 


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

void People::addPersons()
{
    int entries = 0;
    cout << "How many records do you want to input?: ";
    cin >> entries;
    while (entries < 10) 
    {
	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));
	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;
    }
}

void People::showPersons()
{
    if (count == 0) 
    {
	cout << "There are no entries yet!" << endl;
    }
    else 
    {
	cout << "Person on record : " << count << 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 : ";
	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;
        }

	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
1
2
3
4
5
6
7
8
#include "People.h"

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

The syntax for inheritance is this:

class Employer : public Person

The relationship: Employer is a Person

1
2
3
4
5
class Person
{
private:
    PersonalInfo info;
    Employer employer;
The relationship: Person has a Employer. I would think that does not sound correct.

With 'is a' and 'has a' you can find out whether you need composite (the latter) or inheritance.
Topic archived. No new replies allowed.