使用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!
1
A 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;
}
第1题:
使用VC6打开考生文件夹下的工程test2_1,此工程包含一个源程序文件test2_1.cpp,但该程序运行有问题,请改正程序中的错误,使该程序的输出结果如下:
调用基类BASE的构造函数:1
调用基类BASE的构造函数:2
调用派生类A的构造函数:3
调用派生类A的析构函数
调用基类BASE的析构函数
调用基类BASE的析构函数
源程序文什test2_1.cpp清单如下:
include<iostream.h>
class BASE
{
public:
BASE(int i){cout<<"调用基类BASE的构造函数:"<<i<<endl;}
~BASE(){cout<<"调用基类BASE的析构函数"<<endl;)
/***************** found *****************/
}
class A:public BASE
{
public:
/***************** found *****************/
A(int a,int b):i(a),b(b)
{cout<<"调用派生类A的构造函数:"<<a+b<<endl;}
~A(){cout<<"调用派生类A的析构函数"<<endl;)
private:
BASE b;
};
void main()
{
/****************found***************/
BASE obj(1,2);
}
第2题:
使用VC6打开考生文件夹下的工程MyProj2。此工程包含一个源程序文件 MyMain2.cpp,此程序的运行结果为:
Derive1's Print() Called.
Derive2's Print() called.
其中定义的类并不完整,按要求完成下列操作,将类的定义补充完整。
①定义函数Print()为无值型纯虚函数。请在注释“//**1**”之后添加适当的语句。
②建立类Derivel的构造函数,请在注释“//**2**”之后添加适当的语句。
③完成类Derive2成员函数Print()的定义。请在注释“//**3**”之后添加适当的语句。
④定义类Derivel的对象指针d1,类Derive2的对象指针d2。其初始化值分别为1和2。
源程序文件MyMain2.cpp中的程序清单如下:
//MyMain2. cpp
include <iostream>
using namespace std;
class Base
{
public:
Base(int i)
{
b=i;
}
//* * 1 * *
protected:
int b;
};
class Derivel: public Base
{
public:
//* * 2 * *
void print ()
{
cout<<" Derivel's Print() called."<<end1;
}
};
class Derive2 : public Base
{
public:
Derive2(int i) :Base(i) { }
//* * 3 * *
};
void fun (Base *obj)
{
obj->Print ();
}
int main ( )
{
//* * 4 * *
fun (d1);
fun (d2);
return 0;
}
第3题:
使用VC6打开考生文件夹下的工程MyProj10。此工程包含一个源程序文件MyMain10.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类Derived是基类Base公有派生来的。请在注释“//* *1* *”之后添加适当的语句。
②完成构造函数Derived(int i)定义,采用初始化列表的方式使基类Base私有成员a初始化为i+1,类Derived的私有成员b初始化为i。请在注释“//* *2* *”之后添加适当的语句。
③完成类Derived的成员函数show()的类体外的定义。函数show()中要显式调用基类的show()函数,然后要输出私有成员b的值。请在注释“//* *3**”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain10.cpp清单如下:
//MyMain10.cpp
include<iostream>
using namespace std;
class Base
{
public:
Base(int x)
{
a=x
}
void show()
{
cout<<a;
}
private:
int a;
};
//* * *1* * *
{
public:
//* * * 2 * * *
void show();
private:
int b;
};
void Derived :: show()
{
//* * * 3 * * *
}
int main()
{
Derived d(1), *pb;
pb=&d;
pb->show();
return 0;
}
第4题:
使用VC6打开考生文件夹下的工程MyProj15。此工程包含一个源程序文件MyMain15.cpp。程序中定义了3个类A、B和C,但类的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类Inherit是类Base的公有派生类。请在注释“//* *1* *”之后添加适当的语句。
②完成类Inherit成员函数setvalue(int a,int b,int c,int d)的定义,此函数实现的功能是将基类成员x、y、z和派生类的数据成员m的值分别设置成a、b、c和d。请在注释“//* *2* *”之后添加适当的语句。
③完成类Inherit成员函数display()的类体外定义,此函数实现的功能是以“,,,,”的格式将x、y、z和m的值输出到屏幕上。请在注释“//* *3* *”之后添加适当的语句。
注意:除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain15.cpp清单如下:
//Mymain15.cpp
include<iostream>
using namespace std;
class Base
{
private:
int x;
protected:
int y;
public:
int z;
void setx(int i)
{
x=i;
}
int getx()const
{
return x;
}
};
//* * *1* * *
{
private:
int m;
public:
void setvalue(int a,int b,int c,int d)
{
//* * *2* * *
}
void display()const;
};
//* * *3* * *
int main()
{
Inherit A;
A.setvalue(1,2,3,4);
A.display();
return 0;
}
第5题:
使用VC6打开考生文件夹下的工程MyProj12。此工程包含一个源程序文件MyMain12.cpp。程序中定义了两个类Base和Derived,但类的定义并不完整。
请按要求完成下列操作,将类的定义补充完成:
①类Derived是基类Base公有派生来的。请在注释“//* *1* *”之后添加适当的语句。
②完成构造函数Derived(int x)定义,采用初始化列表的方式使基类Base私有成员a初始化为x,类Derived的私有成员b初始化为x+1。请在注释“//* *2* *”之后添加适当的语句。
③完成类Derived的成员函数show()的类体外的定义。函数show()中要输出基类数据成员a的值,然后要输出私有成员b的值。请在注释“//* *3* *之后添加适当的语句。
注意;除在指定位置添加语句之外,请不要改动程序中的其他内容。
源程序文件MyMain12.cpp清单如下:
//MyMain12.cpp
include<iostream>
using namespace std;
class Base
{
public:
int a;
Base(int i)
{
a=i;
}
};
//* * * 1 * * *
{
private:
int b;
public:
//* * * 2 * * *
void show();
};
void Derived::show()
{
//* * * 3 * * *
}
int main()
{
Derived d(1);
d.show();
return 0;
}