class Student
{
private:
char m_name[30]; //this data member is wat i wanna access
protected:
constchar* GetName(void); //i know i need a method tat a method from
//the derived class calls to access a student obj's name
//the pt is tat TeachersAssistant objs have access to student obj names
//i thought putting the method under protected wuld still allow derived
//class to access the method, but i think ive misconceptions abt
//accessiblilty in c++. wuld appreciate someone pting me in the right
//direction. and tell me wats wrg wif my code.
public:
Student(constchar* name);
};
Student::Student(constchar* name)
{
std::strcpy(m_name, name);
}
const*char Student::GetName(void)
{
return m_name;
}
class TeachersAssistant : public Student
{
public:
TeachersAssistant(constchar* name);
constchar* GetName(Student student); //this method takes a
//Student obj arguement & return the student's m_name
};
TeachersAssistant::TeachersAssistant(constchar* name) : Student(name)
{
}
constchar* TeachersAssistant::GetName(Student student)
{
return student.GetName();
}
int main()
{
Student studentA = Student("Paul");
TeachersAssistant studentB = TeachersAssistant("Ron");
char output_name[30] = studentB.GetName(studentA);//this is wat i wanna do
return 0;
}
thks for the reply Zhuge, appreciate a lot, but it doesnt answer my qns. wat i want to do is to have my derived class(TeachersAssistant) objs be the only ones that can access the m_name of Student objs. even if another class eg. Parent( not a sub class of student) has a GetName method, it still cannot access the names of student objs. im sensing i need to use protected attributes but i dun know wat im not seeing and doing correctly. i cannot just park the GetName method of the Student class under public becoz that wuld allow any non extended class to access a student obj's name. so wat shuld i be doing then? wat is wrg wif the way i code? i really have limited knowledge in classes and wuld read up on it more, but in the mean time, i'd appreciate some help with wat i wanna achieve here. thks for ur time.
Make m_name a protected member and use private access inheritance, then m_name will be private in the derived class. If m_name is private it will always remain private.