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(){}
virtualvoid do_work()
{
}
};
Should do_work return a value? Should it be abstract?
class Student : public Person
{
private:
float GPA;
public:
Student();
~Student();
virtualvoid 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;
}
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.