+ 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.

21st Jun 2018, 12:34 AM
燕传航
燕传航 - avatar
7 odpowiedzi
+ 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; }
21st Jun 2018, 4:58 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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
21st Jun 2018, 1:33 AM
Zeke Williams
Zeke Williams - avatar
+ 1
No derived class can't acess the the private data member . Only friend function can access the Private data of a Class..!👍
21st Jun 2018, 4:11 PM
Omar
Omar - avatar
0
Private members are exclusively owned by the class and cannot be used by any other class including derived classes.
21st Jun 2018, 7:55 PM
Mayank verma
Mayank verma - avatar
0
o
1st Jul 2018, 4:53 AM
Dylan Brantner
Dylan Brantner - avatar
0
o
1st Jul 2018, 4:53 AM
Dylan Brantner
Dylan Brantner - avatar
0
o
1st Jul 2018, 4:53 AM
Dylan Brantner
Dylan Brantner - avatar