Answer the question
In order to leave comments, you need to log in
Where are base class private data stored when accessed through a derived class (C++)?
Let's say we have a base class:
class A
{
public:
int geti();
void seti(int);
private:
int i;
};
int A::geti()
{
return i;
}
void A::seti(int n)
{
i = n;
}
class B: public A
{
//содержимое В не важно
};
B ob;
ob.seti(10);
cout<<ob.geti();
Answer the question
In order to leave comments, you need to log in
since i is a private member of the base class, it does not exist in the derived class
Unless redefined in the derived class, members of a base class are also considered to be members of the derived class. The base class members are said to be inherited by the derived class.
Implementation details vary by compiler and platform. The standard guarantees only that the compiler will swear at an attempt to access an internal class member from outside. How the variable itself will be stored is not specified by the standard.
Access modifiers do not affect the presentation of data at all. Roughly speaking, if in some code we replace everything private with public, nothing will change as a result of compilation. These modifiers only limit what can be used in C++ code.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question