应用题2.请打开考生文件夹下的解决方案文件proj2,其中在编辑窗口内显示的主程序文件中定义有类Base和Derived,以及主函数main。程序文本中位于每行“//****found****”下面的一行内有一处或多处下画线标记,请在每个下画线标记处填写合适的内容,并删除下画线标记。经修改后运行程序,得到的输出应为: sum=55。 注意:只在横线处填写适当的代码,不要改动程序中的其他内容。 #include<iostream> using namespace std; class Base { public: Base(int m1,int m2){ mem1=m1; mem2=m2; } int sum(){return mem1+mere2;} private: int mem1,mem2;//基类的数据成员 };
//派生类Derived从基类Base公有继承 //**********found********** class Derived:_____________ { public: //构造函数声明 Derived(int m1,int m2,intm3); //sum函数定义,要求返回mem1、mem2和mem3之和 //**********found********** int sum(){return__________+mem3;} private: int mem3; //派生类本身的数据成员 };
//构造函数的类外定义,要求由m1和m2分别初始化mem1和mem2,由m3初始化mem3 //********** found ********** __________Derived(int m1,intm2,int m3): //********** found ********** __________,mem3(m3){} int main(){ Base a(4,6); Derived b(10,15,20); int sum=a.sum()+b.sum(); cout<<"sum="<<sum<<endl; return 0; }
【正确答案】(1)public Base (2)Base::sum() (3)Derired:: (4)Base(m1,m2)