Class/subclass variable question

I had this question in my assignment and I think variable "age" is public, since it was declared as public in Class A. However, I got the question wrong.
What is the answer for this question, and where can I find related topic about this, regarding how variables are inherited?
please help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
What will the variable "age" be in class B?

class A { 
int x; protected: 
int y;
 public: 
int age; 
}; 
class B : private A { 
string name;
public: void Print(){
cout << name << age;
 } 
}; 

A.public 
B.private 
C.protected 
D.None of these 
hilft wrote:
where can I find related topic about this, regarding how variables are inherited?

Good explanation here: http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
Because you are giving Class B private access to the parent class, Class A, ALL inherited variables and members will become private inside the derived class, Class B.


If you want the parent variables and members to be public inside of a derived class, it's best to use Public protection.

Protected is also useful, but poses some of the same problems as private if not careful
Topic archived. No new replies allowed.