为了账号安全,请及时绑定邮箱和手机立即绑定

C++子类继承父类的所有成员?私有是不继承还是被隐藏?

C++子类继承父类的所有成员?私有是不继承还是被隐藏?

C++
慕仰1766564 2017-02-27 21:02:58
今天学继承私有成员的时候,书上写父类的私有成员是无法被子类继承的。子类构造函数时要先调用父类的构造函数,那么当父类的构造函数有对父类的私有成员初始化时,子类创建一个对象,传入一个对应的原本该继承的私有成员的参数,那么这个参数有什么用?自己写了段代码测试下,结果这个参数改变了原有父类的私有成员。怎么会子类的参数反而改变了父类的成员?#include <stdio.h>class base{private: int baseprivate;public: base(int a) {        baseprivate=a; printf("父类构造...\n"); } ~base() { printf("父类析构...\n"); } void print() { printf("baseprivate=%d\n",baseprivate); }};class child :public base{public: child(int a,int b):base(b) {        childprivate=a; printf("子类构造...\n"); } ~child() { printf("子类析构...\n"); }private: int childprivate;};int main(){ base  obj1(1); obj1.print(); printf("父类创建完成//////////\n");    child obj2(1,2); obj2.print();    printf("子类创建完成//////////\n"); return 0;}这是结果 哪个大神告诉我下 怎么回事
查看完整描述

2 回答

?
慕瓜9220888

TA贡献868条经验 获得超0个赞

https://www.imooc.com/wenda/detail/355822

https://www.imooc.com/wenda/detail/339522

https://www.imooc.com/wenda/detail/352675

https://www.imooc.com/wenda/detail/353486

https://www.imooc.com/wenda/detail/353684

https://www.imooc.com/wenda/detail/334368

https://www.imooc.com/wenda/detail/336314

https://www.imooc.com/wenda/detail/344235

https://www.imooc.com/wenda/detail/345023

https://www.imooc.com/wenda/detail/333670

https://www.imooc.com/wenda/detail/338345

https://www.imooc.com/wenda/detail/339839

https://www.imooc.com/wenda/detail/341350

https://www.imooc.com/wenda/detail/343290

https://www.imooc.com/wenda/detail/356247

https://www.imooc.com/wenda/detail/351707

https://www.imooc.com/wenda/detail/355235

https://www.imooc.com/wenda/detail/353114

https://www.imooc.com/wenda/detail/333151

https://www.imooc.com/wenda/detail/333399

https://www.imooc.com/wenda/detail/345197

https://www.imooc.com/wenda/detail/352361

https://www.imooc.com/wenda/detail/351999

https://www.imooc.com/wenda/detail/342863

https://www.imooc.com/wenda/detail/344139

https://www.imooc.com/wenda/detail/345784

https://www.imooc.com/wenda/detail/350515

https://www.imooc.com/wenda/detail/342172

https://www.imooc.com/wenda/detail/343107

https://www.imooc.com/wenda/detail/343349

https://www.imooc.com/wenda/detail/344234

https://www.imooc.com/wenda/detail/347293

https://www.imooc.com/wenda/detail/348094

https://www.imooc.com/wenda/detail/351999

https://www.imooc.com/wenda/detail/352361

https://www.imooc.com/wenda/detail/336670

https://www.imooc.com/wenda/detail/341863

https://www.imooc.com/wenda/detail/354245

https://www.imooc.com/wenda/detail/332268

https://www.imooc.com/wenda/detail/341536

https://www.imooc.com/wenda/detail/342890

https://www.imooc.com/wenda/detail/347566

https://www.imooc.com/wenda/detail/350606

https://www.imooc.com/wenda/detail/332448

https://www.imooc.com/wenda/detail/342858

https://www.imooc.com/wenda/detail/346826

https://www.imooc.com/wenda/detail/348093

https://www.imooc.com/wenda/detail/350761

https://www.imooc.com/wenda/detail/332906

https://www.imooc.com/wenda/detail/351959

https://www.imooc.com/wenda/detail/343346

https://www.imooc.com/wenda/detail/353743

https://www.imooc.com/wenda/detail/340539

https://www.imooc.com/wenda/detail/353240

https://www.imooc.com/wenda/detail/337657

https://www.imooc.com/wenda/detail/341926

https://www.imooc.com/wenda/detail/355654

https://www.imooc.com/wenda/detail/343418

https://www.imooc.com/wenda/detail/347367

https://www.imooc.com/wenda/detail/349215

https://www.imooc.com/wenda/detail/349351

https://www.imooc.com/wenda/detail/350374

https://www.imooc.com/wenda/detail/352010

https://www.imooc.com/wenda/detail/354592

https://www.imooc.com/wenda/detail/333948

https://www.imooc.com/wenda/detail/354710

https://www.imooc.com/wenda/detail/349533

https://www.imooc.com/wenda/detail/345916

https://www.imooc.com/wenda/detail/347424

https://www.imooc.com/wenda/detail/348290

https://www.imooc.com/wenda/detail/350536

https://www.imooc.com/wenda/detail/351119

https://www.imooc.com/wenda/detail/338789

https://www.imooc.com/wenda/detail/342171

https://www.imooc.com/wenda/detail/350169

https://www.imooc.com/wenda/detail/349600

https://www.imooc.com/wenda/detail/339599

https://www.imooc.com/wenda/detail/341349

https://www.imooc.com/wenda/detail/350575

https://www.imooc.com/wenda/detail/350970

https://www.imooc.com/wenda/detail/340470

https://www.imooc.com/wenda/detail/351783

https://www.imooc.com/wenda/detail/338743

https://www.imooc.com/wenda/detail/340470

https://www.imooc.com/wenda/detail/341349

https://www.imooc.com/wenda/detail/356096

https://www.imooc.com/wenda/detail/349600

https://www.imooc.com/wenda/detail/332629

https://www.imooc.com/wenda/detail/337439

https://www.imooc.com/wenda/detail/341986

https://www.imooc.com/wenda/detail/343289

https://www.imooc.com/wenda/detail/350369

https://www.imooc.com/wenda/detail/337859

https://www.imooc.com/wenda/detail/343881

https://www.imooc.com/wenda/detail/353769

https://www.imooc.com/wenda/detail/340588

https://www.imooc.com/wenda/detail/341617

https://www.imooc.com/wenda/detail/342006

https://www.imooc.com/wenda/detail/344903

https://www.imooc.com/wenda/detail/356205

https://www.imooc.com/wenda/detail/348133

https://www.imooc.com/wenda/detail/348835

https://www.imooc.com/wenda/detail/334948

https://www.imooc.com/wenda/detail/346910

https://www.imooc.com/wenda/detail/340567

https://www.imooc.com/wenda/detail/346332

https://www.imooc.com/wenda/detail/353000

https://www.imooc.com/wenda/detail/355305

https://www.imooc.com/wenda/detail/340152

https://www.imooc.com/wenda/detail/340956

https://www.imooc.com/wenda/detail/353051

https://www.imooc.com/wenda/detail/337159

https://www.imooc.com/wenda/detail/342085

https://www.imooc.com/wenda/detail/343759

https://www.imooc.com/wenda/detail/349151

https://www.imooc.com/wenda/detail/350050

https://www.imooc.com/wenda/detail/351519

https://www.imooc.com/wenda/detail/354597

https://www.imooc.com/wenda/detail/346021

https://www.imooc.com/wenda/detail/347841

https://www.imooc.com/wenda/detail/349922

https://www.imooc.com/wenda/detail/352899

https://www.imooc.com/wenda/detail/345536

https://www.imooc.com/wenda/detail/355568

https://www.imooc.com/wenda/detail/349334

https://www.imooc.com/wenda/detail/354938

https://www.imooc.com/wenda/detail/351494

https://www.imooc.com/wenda/detail/351742

https://www.imooc.com/wenda/detail/353550

https://www.imooc.com/wenda/detail/354203

https://www.imooc.com/wenda/detail/333904

https://www.imooc.com/wenda/detail/355085

https://www.imooc.com/wenda/detail/345405

https://www.imooc.com/wenda/detail/354597

https://www.imooc.com/wenda/detail/346021

https://www.imooc.com/wenda/detail/347841

https://www.imooc.com/wenda/detail/351494

https://www.imooc.com/wenda/detail/352899

https://www.imooc.com/wenda/detail/353550

https://www.imooc.com/wenda/detail/354203

https://www.imooc.com/wenda/detail/354938

https://www.imooc.com/wenda/detail/332293

https://www.imooc.com/wenda/detail/353068

https://www.imooc.com/wenda/detail/354874

https://www.imooc.com/wenda/detail/353397

https://www.imooc.com/wenda/detail/341857

https://www.imooc.com/wenda/detail/351897

https://www.imooc.com/wenda/detail/332225

https://www.imooc.com/wenda/detail/339699

https://www.imooc.com/wenda/detail/340836

https://www.imooc.com/wenda/detail/350094

https://www.imooc.com/wenda/detail/351574

https://www.imooc.com/wenda/detail/352613

https://www.imooc.com/wenda/detail/355195

https://www.imooc.com/wenda/detail/332167

https://www.imooc.com/wenda/detail/332410

https://www.imooc.com/wenda/detail/332905

https://www.imooc.com/wenda/detail/333134

https://www.imooc.com/wenda/detail/334223

https://www.imooc.com/wenda/detail/336101

https://www.imooc.com/wenda/detail/347403

https://www.imooc.com/wenda/detail/343119

https://www.imooc.com/wenda/detail/339261

https://www.imooc.com/wenda/detail/339507

https://www.imooc.com/wenda/detail/355561

https://www.imooc.com/wenda/detail/333007

https://www.imooc.com/wenda/detail/334248

https://www.imooc.com/wenda/detail/343212

https://www.imooc.com/wenda/detail/338062

https://www.imooc.com/wenda/detail/354873

https://www.imooc.com/wenda/detail/346615

https://www.imooc.com/wenda/detail/349641

https://www.imooc.com/wenda/detail/350516

https://www.imooc.com/wenda/detail/353412

https://www.imooc.com/wenda/detail/353493

https://www.imooc.com/wenda/detail/333474

https://www.imooc.com/wenda/detail/337696

https://www.imooc.com/wenda/detail/337733

https://www.imooc.com/wenda/detail/343039

https://www.imooc.com/wenda/detail/345855

查看完整回答
反对 回复 2021-10-21
?
慕仰1766564

TA贡献2条经验 获得超0个赞

好像突然明白了。。

查看完整回答
反对 回复 2017-02-27
  • 2 回答
  • 0 关注
  • 3002 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信