+ 3
Can derived class use its base class's private member?
I have a doubt that if derived class can't own its base class's private members,whether the base class's constructor can initialize the private members.
7 ответов
+ 4
You can try it yourself?
#include <iostream>
class A {
private: int a;
protected: int b;
public: A():a(3),b(5) {}
};
class B : public A {
public: B() {
std::cout << b;
//std::cout << a; -- error
}
};
int main() { B obj; }
+ 2
Instead of using private, try using the access modifier word, "protected". Using protected keeps all other code from accessing it just like private, but it allows any inherited class to access it.
https://www.sololearn.com/discuss/1226303/?ref=app
+ 1
No derived class can't acess the the private data member .
Only friend function can access the Private data of a Class..!👍
0
Private members are exclusively owned by the class and cannot be used by any other class including derived classes.
0
o
0
o
0
o