C++试题及答案(2)

2018-12-10 15:12

19. C++的输入/输出流库中,ios类是一个虚基类,istream类、ostream类以及streambuf类都是ios类的派生类。

20. 设inf是一个ifstream类的流对象,则inf.seekg(10,ios::beg);表示将文件指针从文件当前位置向后移动10个字节。

得 分 三、读程序写结果(每空2分,共32分)

(注意将结果写在下面相应横线上!)

⑴ ⑵ ⑶ ⑷

⑸ ⑹ ⑺ ⑻ ⑼ ⑽ ⑾_ ⑿ ⒀ ⒁ ⒂ ⒃ 1. 写出下面程序的运行结果。 #include class A { public: A( )

{ cout<<\} virtual ~A( )

{ cout<<\} };

class B:public A { public: B(int i)

{ cout<<\ buffer=new char[i]; } virtual ~B( ) { delete []buffer;

cout<<\} private: char* buffer;

};

void fun(A* a) { delete a; } void main() { A *b=new B(10); fun(b); }

运行结果: ⑴ ⑵ ⑶ ⑷

2. 写出下面程序的运行结果。 #include class Shape { public: void Draw() {

cout<<\ }

virtual void Print()=0; };

class Circle:public Shape { private:

double r; public: void Draw() {

cout<<\ }

void SetData(double radius) {

r=radius; }

void Print() {

cout<<\ } };

class Rectangle:public Shape { private: double a,b; public: void Draw() {

cout<<\ }

void SetData(double x,double y) {

a=x,b=y;

}

void Print() {

cout<<\ } };

void main() { Circle c; Rectangle r; Shape *sp1=&c; Shape &sp2=r; c.SetData(10); r.SetData(3,5); sp1->Draw(); c.Print(); sp2.Draw(); r.Print(); }

运行结果: ⑸ ⑹ ⑺ ⑻

3. 写出下面程序的运行结果(注:运行结果中首行的空白行不考虑)。 #include class CArray

{ public: CArray(int i) { Length=i;

Buffer=new char[Length+1]; } ~CArray()

{ delete []Buffer; } int GetLength() { return Length; } char& operator[](int i); private: int Length; char* Buffer; };

char& CArray::operator[](int i) { static char ch; if(i=0) return Buffer[i]; else

{ cout<<\ return ch; } }

void main() { int cnt;

CArray string1(6); char *string2=\ for(cnt=0;cnt<8;cnt++) string1[cnt]=string2[cnt];


C++试题及答案(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:公司理财习题参考答案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: