Getting base class member data

I am having quite a lot of difficulty with the getting the base class data in an inherited class. There are two inherited classes, but for brevity I will only include the student. The idea is to get "do_work()" to display the personsName in and hours worked. Line 15 in Student class is what I am ultimately trying to get to display properly.

first the base class: person
1
2
3
4
5
6
7
8
9
10
11
12
13
class Person
{
private:
     string personsName;
     int personsAge;

public:
     Person(){}
     ~Person(){}
      virtual void do_work()
       {
       }
};


Should do_work return a value? Should it be abstract?

Now the inherited class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Student : public Person
{
private:

	float GPA;
public:
	Student();
	~Student();
        virtual void do_work()
        {
          int hoursWorked = 0;

	  hoursWorked = rand() % 40 + 1;

          cout<<personsName<<" did" << hoursWorked <<" of homework"<<endl;
        }
	virtual string getName()
        {
           return personsName;// how do I get PersonsName to be passed here?
        }

};


It is also worth adding that in main I have
1
2
3
4
5
6
7
8
9
10
   int main()
{
    Person* person;
    Student *student = new Student[3];
    Person* personPtr0 = &student[0];
    personPtr0->setName("Chad" );//student by default 
     personPtr0->printName();
    delete[] student;
}
Last edited on
If it's meant to be possible to get a personsName from a Person object, then the Person class should have a getName() function.
I figured it out, I think. Well I did get line 15 in student to display the right info at least!

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

class Student : public Person
{
private:
	string studentName;
	float GPA;
	int homeworkTime;
public:
	Student()
        {
	GPA = 0.0;//range is between 0.0 and 4.0
	//cout << "Student" << this << endl;
	studentName = "";
	homeworkTime = 0;
         }
	~Student(){}
	

	//virtuals
void Student::do_work()
{
	
	homeworkTime = rand() % 35 + 30;
	cout << studentName <<" did " << homeworkTime <<" hours of homework." <<endl; 
        
}

void Student::setName(string pName)
{
	studentName = pName;
}
	
};


I guess the reason why I can't have do_work() be abstract in my base class is because my inherited classes are arrays.
Last edited on
there is another way to let a derived class access data from its "parent" class:

the keyword "protected" instead of "private" makes exactly that possible

1
2
3
4
5
6
7
8
9
10
11
12
13
class Person
{
protected:
     string personsName;
     int personsAge;

public:
     Person(){}
     ~Person(){}
      virtual void do_work()
       {
       }
};


on the other hand a "string getName() const" function would make sence since you may want other objects to be able to ask the "person" for its name
Last edited on
Your model seems odd. Is a student's name different to their name as a person? You can do what you like with the code, but if the model of your classes makes sense it will be easier to reason about.
Topic archived. No new replies allowed.