Class problems

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
#include <iostream>
using namespace std;

class Person
{
	public:								//constructor always have same name as class
	Person();							//constructor with 0 input
	Person(string pname, int page);		//constructor with 2 input;
	string get_name() const;
	int get_age() const;

	private:
	string name;
	int age; /* 0 if unknown */
};

Person::Person(std::string pname,int page)
{
	name = pname;
	age = page;
}

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

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

class PEmployee
{
    public:
    PEmployee();
    PEmployee(string employee_name, double initial_salary);
    void set_salary(double new_salary);
    double get_salary() const;
    string get_name() const;
    private:
    Person person_data;
    double salary;
};

PEmployee :: PEmployee()
{}

PEmployee::PEmployee(string employee_name, double initial_salary)
{
    name = employee_name ;
    salary = initial_salary ;
}

void PEmployee::set_salary(double new_salary)
{
    salary = new_salary;
}

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

double PEmployee::get_salary() const
{
    return salary;
}


int main()
{	
    PEmployee f("Patrick", 1000.00);
    cout << f.get_name() << " earns a salary of "
    << f.get_salary() << endl;
    return 0;
}


when I compile this cpp, it shows some error:
51|error: 'name' was not declared in this scope|
62|error: 'name' was not declared in this scope|

I wish I can know where am I wrong as I keep comparing this to other cpp with class..

closed account (1vRz3TCk)
PEmployee does not have a member call name.
I guess 'PEmployee' should be derived from 'Person'. Line 33: class PEmployee : public Person
how to correct it?
inherit the Person class.so u can access the Person class name.
closed account (zb0S216C)
Person::name is private to all external and non-friend functions. Therefore, Person::name can only be accessed by the members of Person. If you swap private with protected within Person, any derived classes will be able to access those members directly. To do this, you would do something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Person
{
    // Other members...
    protected:
    string name;
    int age;
};

class PEmployee : private Person
{
    // Other members...
    inline string GetName( ) const
    {
        return this->name;
    }
    private:
    double salary;
};

Here, only PEmployee can access the protected members of Person. When PEmployee derives from Person, all of Person's members became members of PEmployee. However, since PEmployee is privately derived from Person, only the members of PEmployee can access the protected members of Person; no external functions or variables can access the protected members of Person.

Wazzak
Last edited on
Framework:
I tried to modify my code similar to the provided but one more error occur..

error: 'const class PEmployee' has no member named 'name'|
It should be class PEmployee : public Person in this case
shacktar, thanks, that works fine for me =)
Topic archived. No new replies allowed.