您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 综合/其它 > C++程序设计第三版(谭浩强)第十一章习题答案
11.1题#includeiostreamusingnamespacestd;classStudent{public:voidget_value(){cinnumnamesex;}voiddisplay(){coutnum:numendl;coutname:nameendl;coutsex:sexendl;}private:intnum;charname[10];charsex;};classStudent1:publicStudent{public:voidget_value_1(){get_value();cinageaddr;}voiddisplay_1(){coutage:ageendl;//引用派生类的私有成员,正确。coutaddress:addrendl;}//引用派生类的私有成员,正确。private:intage;charaddr[30];};intmain(){Student1stud1;stud1.get_value_1();stud1.display();stud1.display_1();return0;}11.2题#includeiostreamusingnamespacestd;classStudent{public:voidget_value(){cinnumnamesex;}voiddisplay(){coutnum:numendl;coutname:nameendl;coutsex:sexendl;}private:intnum;charname[10];charsex;};classStudent1:privateStudent{public:voidget_value_1(){get_value();cinageaddr;}voiddisplay_1(){display();coutage:ageendl;//引用派生类的私有成员,正确。coutaddress:addrendl;}//引用派生类的私有成员,正确。private:intage;charaddr[30];};intmain(){Student1stud1;stud1.get_value_1();stud1.display_1();return0;}11.3题#includeiostreamusingnamespacestd;classStudent//声明基类{public://基类公用成员voidget_value();voiddisplay();protected://基类保护成员intnum;charname[10];charsex;};voidStudent::get_value(){cinnumnamesex;}voidStudent::display(){coutnum:numendl;coutname:nameendl;coutsex:sexendl;}classStudent1:protectedStudent//声明一个保护派生类{public:voidget_value_1();voiddisplay1();private:intage;charaddr[30];};voidStudent1::get_value_1(){get_value();cinageaddr;}voidStudent1::display1(){coutnum:numendl;//引用基类的保护成员coutname:nameendl;//引用基类的保护成员coutsex:sexendl;//引用基类的保护成员coutage:ageendl;//引用派生类的私有成员coutaddress:addrendl;//引用派生类的私有成员}intmain(){Student1stud1;//stud1是派生类student1类的对象stud1.get_value_1();//调用派生类对象stud1的公用成员函数stud1.display1();//调用派生类对象stud1的公用成员函数return0;}11.4题#includeiostreamusingnamespacestd;classStudent//声明基类{public://基类公用成员voidget_value();voiddisplay();protected://基类保护成员intnum;charname[10];charsex;};voidStudent::get_value(){cinnumnamesex;}voidStudent::display(){coutnum:numendl;coutname:nameendl;coutsex:sexendl;}classStudent1:publicStudent//声明一个公用派生类{public:voidget_value_1();voiddisplay1();private:intage;charaddr[30];};voidStudent1::get_value_1(){get_value();cinageaddr;}voidStudent1::display1(){coutnum:numendl;//引用基类的保护成员,合法coutname:nameendl;//引用基类的保护成员,合法coutsex:sexendl;//引用基类的保护成员,合法coutage:ageendl;//引用派生类的私有成员,合法coutaddress:addrendl;//引用派生类的私有成员,合法}intmain(){Student1stud1;//stud1是派生类student1类的对象stud1.get_value_1();//调用派生类对象stud1的公用成员函数get_value_1stud1.display1();//调用派生类对象stud1的公用成员函数display1return0;}11.4另一题#includeiostreamusingnamespacestd;classStudent//声明基类{public://基类公用成员voidget_value();voiddisplay();protected://基类保护成员intnum;charname[10];charsex;};voidStudent::get_value(){cinnumnamesex;}voidStudent::display(){coutnum:numendl;coutname:nameendl;coutsex:sexendl;}classStudent1:protectedStudent//声明一个公用派生类{public:voidget_value_1();voiddisplay1();private:intage;charaddr[30];};voidStudent1::get_value_1(){cinageaddr;}voidStudent1::display1(){coutage:ageendl;coutaddress:addrendl;}intmain(){Student1stud1;//stud1是派生类student1类的对象stud1.get_value();stud1.get_value_1();stud1.display();stud1.display1();//合法。display1是派生类中的公用成员函数return0;}11.5题classA//A为基类{public:voidf1();inti;protected:voidf2();intj;private:intk;};classB:publicA//B为A的公用派生类{public:voidf3();protected:intm;private:intn;};classC:publicB//C为B的公用派生类{public:voidf4();private:intp;};intmain(){Aa1;//a1是基类A的对象Bb1;//b1是派生类B的对象Cc1;//c1是派生类C的对象return0;}11.6题#includeiostreamusingnamespacestd;classA{public:voidf1();protected:voidf2();private:inti;};classB:publicA{public:voidf3();intk;private:intm;};classC:protectedB{public:voidf4();protected:intn;private:intp;};classD:privateC{public:voidf5();protected:intq;private:intr;};intmain(){Aa1;Bb1;Cc1;Dd1;return0;}11.7题#includeiostreamusingnamespacestd;classA{public:A(){a=0;b=0;}A(inti){a=i;b=0;}A(inti,intj){a=i;b=j;}voiddisplay(){couta=ab=b;}private:inta;intb;};classB:publicA{public:B(){c=0;}B(inti):A(i){c=0;}B(inti,intj):A(i,j){c=0;}B(inti,intj,intk):A(i,j){c=k;}voiddisplay1(){display();coutc=cendl;}private:intc;};intmain(){Bb1;Bb2(1);Bb3(1,3);Bb4(1,3,5);b1.display1();b2.display1();b3.display1();b4.display1();return0;}11.8题#includeiostreamusingnamespacestd;classA{public:A(){coutconstructingAendl;}~A(){coutdestructingAendl;}};classB:publicA{public:B(){coutconstructingBendl;}~B(){coutdestructingBendl;}};classC:publicB{public:C(){coutconstructingCendl;}~C(){coutdestructingCendl;}};intmain(){Cc1;return0;}11.9题#includestring#includeiostreamusingnamespacestd;classTeacher{public:Teacher(stringnam,inta,chars,stringtit,stringad,stringt);voiddisplay();protected:stringname;intage;charsex;stringtitle;stringaddr;stringtel;};Teacher::Teacher(stringnam,inta,chars,stringtit,stringad,stringt):name(nam),age(a),sex(s),title(tit),addr(ad),tel(t){}voidTeacher::dis
本文标题:C++程序设计第三版(谭浩强)第十一章习题答案
链接地址:https://www.777doc.com/doc-6056780 .html