how to access private members from derived class

Hi,

I'm very new C++, i juz touched on Classes.

How do i access the data member of a base class using a class method of a derived class?

Example of wat i want to do:

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
class Student
{
   private:
      char m_name[30]; //this data member is wat i wanna access
   protected:
      const char* 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(const char* name);
};

Student::Student(const char* name)
{
   std::strcpy(m_name, name);
}
const*char Student::GetName(void)
{
   return m_name;
}

class TeachersAssistant : public Student
{
   public:
      TeachersAssistant(const char* name);
      const char* GetName(Student student); //this method takes a
      //Student obj arguement & return the student's m_name    
};

TeachersAssistant::TeachersAssistant(const char* name) : Student(name)
{

}

const char* 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;
}

Making it protected let's the class access its own inherited members. Other classes' private and protected members are still off-limits.
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.
Topic archived. No new replies allowed.