问答题

使用VC6打开考生文件夹下的工程test12_1,此工程包含一个源程序文件test_12.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下: fun (Sample &p) 1 2 fun (Sample *p) 3 4 20 10 源程序文件test12_1清单如下: #include<iostream .h> class Sample { private: int x,y; static int z; public: Sample(int a,int b){ x=a;y=b; } void fun(Sample &p); void fun(Sample *p); static void print(Sample s); }; /*************** found ***************/ int z=10; void Sample::fun(Sample &p) { x=p.x;y=p.y; cout<<"fun(Sample &p)"<<" "<<x<<" "<<y<<endl; } void Sample::fun(Sample *p) { /************** found **************/ x=p.x; y=p.y; cout<<"fun(Sample *p) "<<" ’<<x<<" "<<y<<endl; } void Sample::print (Sample s) { /*************** found *****************/ x=20; cout<<s. x<<" "<<z<<endl; } void main() { Sample p(1,2),q(3,4); p. fun(p); p. fun(&q); p. print(p); }

【参考答案】

(1)错误:int z=10; 正确:int Sample::z=10;(2)错误:x=p.x;y=p.y; 正确;x=......

(↓↓↓ 点击下方‘点击查看答案’看完整答案 ↓↓↓)
热门 试题

问答题
使用VC6打开考生文件夹下的工程test12_3,此工程包含一个test12_3.cpp,其中定义了类Base和类A,类A公有继承 Base,但这两个类的定义都并不完整。请按要求完成下列操作,将程序补充完整。(1)定义枚举类型变量en,它包含两个枚举符front和back,请在注释“ **1**”之后添加适当的语句。(2)在类Base中添加常成员虚函数void E()的定义,该函数输出“In Base E!”,请在注释“ **2**”之后添加适当的语句。(3)在类A中添加常成员虚函数void E()的定义,该函数先调用基类中的虚函数 E()再输出“In AE!”,请在注释“ ** 3**”之后添加适当的语句。(4)完成类A构造函数的定义,请使用参数列表的形式初始化类A的成员,并输出“A constructor.”,请在注释“ ** 4**”之后添加适当的语句。输出结果如下:Base constructor.A constructor.In BaseE!In AE!In BaseP!In A!1A destructor.Base destructor.注意:除在指定的位置添加语句外,请不要改动程序中的其他语句。源程序文件test12_3.cpp清单如下:#include<iostream .h> ** 1 **class Base{protected:int b1;int b2;public:Base();~Base();int Getb1()const { return b1; }void Setb1(int x){ b1 = x; }int Getb2()const { return b2; }void Setb2(int y) { b2 = y; }void Print()const {cout<< In Base P! <<end1;} ** 2 **};Base::Base():b1(1),b2(5){cout<< Base constructor. <<endl;}Base::~Base(){cout<< Base destructor. <<endl;}class A:public Base{protected:en enA;public:A();~A();en GetColor()const { return enA; }void SetColor(en color){ enA = color; }void InA(){cout<< In A! <<endl;} ** 3 **{Base::E();cout<< In AE! <<endl;}}; ** 4 **{cout<< A constructor. <<endl;}A::~A(){cout<< A destructor. <<endl;}void main(){A a1;a1.E();cout<<endl;a1.Print();a1.InA();cout<<a1.Getbl()<<endl;}