猿问

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

今天学继承私有成员的时候,书上写父类的私有成员是无法被子类继承的。子类构造函数时要先调用父类的构造函数,那么当父类的构造函数有对父类的私有成员初始化时,子类创建一个对象,传入一个对应的原本该继承的私有成员的参数,那么这个参数有什么用?自己写了段代码测试下,结果这个参数改变了原有父类的私有成员。怎么会子类的参数反而改变了父类的成员?

#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;

}


这是结果 哪个大神告诉我下 怎么回事



慕仰1766564
浏览 3003回答 2
2回答

慕瓜9220888

https://www.imooc.com/wenda/detail/355822https://www.imooc.com/wenda/detail/339522https://www.imooc.com/wenda/detail/352675https://www.imooc.com/wenda/detail/353486https://www.imooc.com/wenda/detail/353684https://www.imooc.com/wenda/detail/334368https://www.imooc.com/wenda/detail/336314https://www.imooc.com/wenda/detail/344235https://www.imooc.com/wenda/detail/345023https://www.imooc.com/wenda/detail/333670https://www.imooc.com/wenda/detail/338345https://www.imooc.com/wenda/detail/339839https://www.imooc.com/wenda/detail/341350https://www.imooc.com/wenda/detail/343290https://www.imooc.com/wenda/detail/356247https://www.imooc.com/wenda/detail/351707https://www.imooc.com/wenda/detail/355235https://www.imooc.com/wenda/detail/353114https://www.imooc.com/wenda/detail/333151https://www.imooc.com/wenda/detail/333399https://www.imooc.com/wenda/detail/345197https://www.imooc.com/wenda/detail/352361https://www.imooc.com/wenda/detail/351999https://www.imooc.com/wenda/detail/342863https://www.imooc.com/wenda/detail/344139https://www.imooc.com/wenda/detail/345784https://www.imooc.com/wenda/detail/350515https://www.imooc.com/wenda/detail/342172https://www.imooc.com/wenda/detail/343107https://www.imooc.com/wenda/detail/343349https://www.imooc.com/wenda/detail/344234https://www.imooc.com/wenda/detail/347293https://www.imooc.com/wenda/detail/348094https://www.imooc.com/wenda/detail/351999https://www.imooc.com/wenda/detail/352361https://www.imooc.com/wenda/detail/336670https://www.imooc.com/wenda/detail/341863https://www.imooc.com/wenda/detail/354245https://www.imooc.com/wenda/detail/332268https://www.imooc.com/wenda/detail/341536https://www.imooc.com/wenda/detail/342890https://www.imooc.com/wenda/detail/347566https://www.imooc.com/wenda/detail/350606https://www.imooc.com/wenda/detail/332448https://www.imooc.com/wenda/detail/342858https://www.imooc.com/wenda/detail/346826https://www.imooc.com/wenda/detail/348093https://www.imooc.com/wenda/detail/350761https://www.imooc.com/wenda/detail/332906https://www.imooc.com/wenda/detail/351959https://www.imooc.com/wenda/detail/343346https://www.imooc.com/wenda/detail/353743https://www.imooc.com/wenda/detail/340539https://www.imooc.com/wenda/detail/353240https://www.imooc.com/wenda/detail/337657https://www.imooc.com/wenda/detail/341926https://www.imooc.com/wenda/detail/355654https://www.imooc.com/wenda/detail/343418https://www.imooc.com/wenda/detail/347367https://www.imooc.com/wenda/detail/349215https://www.imooc.com/wenda/detail/349351https://www.imooc.com/wenda/detail/350374https://www.imooc.com/wenda/detail/352010https://www.imooc.com/wenda/detail/354592https://www.imooc.com/wenda/detail/333948https://www.imooc.com/wenda/detail/354710https://www.imooc.com/wenda/detail/349533https://www.imooc.com/wenda/detail/345916https://www.imooc.com/wenda/detail/347424https://www.imooc.com/wenda/detail/348290https://www.imooc.com/wenda/detail/350536https://www.imooc.com/wenda/detail/351119https://www.imooc.com/wenda/detail/338789https://www.imooc.com/wenda/detail/342171https://www.imooc.com/wenda/detail/350169https://www.imooc.com/wenda/detail/349600https://www.imooc.com/wenda/detail/339599https://www.imooc.com/wenda/detail/341349https://www.imooc.com/wenda/detail/350575https://www.imooc.com/wenda/detail/350970https://www.imooc.com/wenda/detail/340470https://www.imooc.com/wenda/detail/351783https://www.imooc.com/wenda/detail/338743https://www.imooc.com/wenda/detail/340470https://www.imooc.com/wenda/detail/341349https://www.imooc.com/wenda/detail/356096https://www.imooc.com/wenda/detail/349600https://www.imooc.com/wenda/detail/332629https://www.imooc.com/wenda/detail/337439https://www.imooc.com/wenda/detail/341986https://www.imooc.com/wenda/detail/343289https://www.imooc.com/wenda/detail/350369https://www.imooc.com/wenda/detail/337859https://www.imooc.com/wenda/detail/343881https://www.imooc.com/wenda/detail/353769https://www.imooc.com/wenda/detail/340588https://www.imooc.com/wenda/detail/341617https://www.imooc.com/wenda/detail/342006https://www.imooc.com/wenda/detail/344903https://www.imooc.com/wenda/detail/356205https://www.imooc.com/wenda/detail/348133https://www.imooc.com/wenda/detail/348835https://www.imooc.com/wenda/detail/334948https://www.imooc.com/wenda/detail/346910https://www.imooc.com/wenda/detail/340567https://www.imooc.com/wenda/detail/346332https://www.imooc.com/wenda/detail/353000https://www.imooc.com/wenda/detail/355305https://www.imooc.com/wenda/detail/340152https://www.imooc.com/wenda/detail/340956https://www.imooc.com/wenda/detail/353051https://www.imooc.com/wenda/detail/337159https://www.imooc.com/wenda/detail/342085https://www.imooc.com/wenda/detail/343759https://www.imooc.com/wenda/detail/349151https://www.imooc.com/wenda/detail/350050https://www.imooc.com/wenda/detail/351519https://www.imooc.com/wenda/detail/354597https://www.imooc.com/wenda/detail/346021https://www.imooc.com/wenda/detail/347841https://www.imooc.com/wenda/detail/349922https://www.imooc.com/wenda/detail/352899https://www.imooc.com/wenda/detail/345536https://www.imooc.com/wenda/detail/355568https://www.imooc.com/wenda/detail/349334https://www.imooc.com/wenda/detail/354938https://www.imooc.com/wenda/detail/351494https://www.imooc.com/wenda/detail/351742https://www.imooc.com/wenda/detail/353550https://www.imooc.com/wenda/detail/354203https://www.imooc.com/wenda/detail/333904https://www.imooc.com/wenda/detail/355085https://www.imooc.com/wenda/detail/345405https://www.imooc.com/wenda/detail/354597https://www.imooc.com/wenda/detail/346021https://www.imooc.com/wenda/detail/347841https://www.imooc.com/wenda/detail/351494https://www.imooc.com/wenda/detail/352899https://www.imooc.com/wenda/detail/353550https://www.imooc.com/wenda/detail/354203https://www.imooc.com/wenda/detail/354938https://www.imooc.com/wenda/detail/332293https://www.imooc.com/wenda/detail/353068https://www.imooc.com/wenda/detail/354874https://www.imooc.com/wenda/detail/353397https://www.imooc.com/wenda/detail/341857https://www.imooc.com/wenda/detail/351897https://www.imooc.com/wenda/detail/332225https://www.imooc.com/wenda/detail/339699https://www.imooc.com/wenda/detail/340836https://www.imooc.com/wenda/detail/350094https://www.imooc.com/wenda/detail/351574https://www.imooc.com/wenda/detail/352613https://www.imooc.com/wenda/detail/355195https://www.imooc.com/wenda/detail/332167https://www.imooc.com/wenda/detail/332410https://www.imooc.com/wenda/detail/332905https://www.imooc.com/wenda/detail/333134https://www.imooc.com/wenda/detail/334223https://www.imooc.com/wenda/detail/336101https://www.imooc.com/wenda/detail/347403https://www.imooc.com/wenda/detail/343119https://www.imooc.com/wenda/detail/339261https://www.imooc.com/wenda/detail/339507https://www.imooc.com/wenda/detail/355561https://www.imooc.com/wenda/detail/333007https://www.imooc.com/wenda/detail/334248https://www.imooc.com/wenda/detail/343212https://www.imooc.com/wenda/detail/338062https://www.imooc.com/wenda/detail/354873https://www.imooc.com/wenda/detail/346615https://www.imooc.com/wenda/detail/349641https://www.imooc.com/wenda/detail/350516https://www.imooc.com/wenda/detail/353412https://www.imooc.com/wenda/detail/353493https://www.imooc.com/wenda/detail/333474https://www.imooc.com/wenda/detail/337696https://www.imooc.com/wenda/detail/337733https://www.imooc.com/wenda/detail/343039https://www.imooc.com/wenda/detail/345855

慕仰1766564

好像突然明白了。。
随时随地看视频慕课网APP
我要回答