Employee class help

The person header.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef PERSON_H
#define PERSON_H

#include <string>

class Person
{
public:
    Person();
    Person(std::string pname, int page);
    std::string get_name() const;
    int get_age() const;
protected:
    std::string name;
    int age;  // 0 of unknown
};

#endif 


The person class.
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
#include "person.h"
#include <string>
#include <iostream>

using namespace std;

Person::Person()
{
	age = 0;
}

Person::Person(string pname, int page)
{
	string name1, name2;
	cin >> name1;
	cin >> name2;
	pname = name1 + " " + name2; // gets the first and last name
	name = pname;
	cin >> page;
	age = page;
}

string Person::get_name() const
{
	return name;
}

int Person::get_age() const
{
	return age;
}


I can't modify this code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
// DO NOT MODIFY THIS FILE IN ANY WAY

#include <string>
#include "person.h"

class Employee
{
public:
    Employee();
    Employee(const std::string& employeeName, double initialSalary);
    void setSalary(double newSalary);
    double getSalary() const;
    std::string getName() const;
private:
    Person personData;
    double salary;
};


#endif 


The code here does not recognize the name previously declared in the person class. I am having trouble getting this class to recognize the "string name" previously declared in the person.
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 <string>
#include <iostream>
using namespace std;

#include "employee.h"

using namespace std;
// Add your implementation here
Employee::Employee()
{
	salary = 0;
}

Employee::Employee(const string& employeeName, double initialSalary)
{
	name = employeeName;
	salary = initialSalary;

}

void Employee::setSalary(double newSalary)
{
	salary = newSalary;
}

double Employee::getSalary() const
{
	return salary;
}

string Employee::getName() const
{
	return name;
}
I'm assuming your talking about the second constructor in the employee class. In order to be able to use the name instance variable of the person class you need to create a person object. If you intend for the employee class to have the instance variables of the person class you must use inheritance or pass the variable to one of the methods in the employee class.
Would this also work.
1
2
3
4
5
6
Employee::Employee() : personData(), salary(0.0) // I don't know what to put inside personData. I keep getting errors if I put in "".
{ }

Employee::Employee(const string& employeeName, double initialSalary)
	: personData(employeeName), salary(initialSalary) // personData has error here too.
{ }
First one should work, second one shouldn't.

Reason: The first constructor calls the default constructor you made:
1
2
3
4
Person::Person()
{
	age = 0;
}


Yet realize that in this constructor you haven't assigned std::string name. Therefore any calls to the name variable will be undefined.

The second constructor doesn't work because your calling a constructor:
Person(const std::string & variableName) which you have not made.
Last edited on
Topic archived. No new replies allowed.