2016整理版北邮面向对象C++试题(房鸣)(3)

2019-08-17 13:24

更正错误如下:

No.1 $:

No.2 $:在标志处下一行添加如下语句

No.3 $:

No.4 $:

No.5 $:

课程复习--补充题:

1. +程序存在若干处错误,以不改变程序结构为前提,请在出错的行首做一标记(如*),并将改动最少的正确语句写在所在行或出错部位的右侧。 例1:

#include using namespace std; class A { protected:

int i;

public:

};

A(int x):i(x){}

virtual int operator!()=0; virtual int operator~()=0;

//补充定义虚基函数,使基类能访问派生类同名成员函数

class B:public A {

int i;

public: }; void main() {

A* p=new B(2,1);

cout<<\cout<<\

//由new B(2,1)知,只有B的基类A中i为1,因此需要调用成员函数operator~()访问基类A中i。原代码中调用operator!(),返回值是2,不合要求。此处~(*p)等价于(*p).operator~()

} 例2:

#include using namespace std; class A { protected:

int i;

B(int x,int y):i(x),A(y){} int operator!(){return i;} int operator~(){return A::i;}

public:

A(int x):i(x){}

virtual int operator~()=0; virtual int operator!()=0;

//补充定义虚基函数,使基类能访问派生类同名成员函数

};

class B:public A {

protected:

int i; A* p; public: }; void main() {

A* p=new B(1,2);

cout<

B(int x,int y):i(x),A(y){ p=(A*)this;} int operator!(){return i;}

int operator~(){return p->i;}//标准规则限制必须用基类强制

int operator~() {return A::i;} //强制使用基类中i

}

2. 下述C++程序尚未完成,试根据已知条件补上类A 中欠缺的成员函数。 例1:

#include using namespace std; class A {

int i; A* p;

public: }; void main() {

A a(5);

for(int i=0;i<5;i++) {

a[i]=i;

//此处等价于a.operator[](i).operator=(i); A():p(NULL){}

A(int x):i(x){p=new A[x];}

A& operator[](int x){return p[x];}//补充定义operator[](int) void operator=(int x){i=x;}//补充定义operator=(int) int operator!(){return i;}//补充定义operator!() ~A(){if(p) delete [] p;}//因有动态内存分配,补充定义析构函数,释放内存

cout<

//此处等价于a.operator[](i).operator!(); }

} cin.get();

例2:

#include using namespace std; class A { protected:

int i;

public:

A(){cout<<\

bool operator == (const A& rhs) const {return i==rhs.i;} //补充定义相等运算符,比较两个A对象 }; void main() { }

另一种方法不知道对不对

A a,b;

if(a==b)cout<<\else cout<<\

补充:2011.01.13考试题

下面的程序中有一些错误,请对程序进行修改,使其能最好地完成预期功能。 #include using namespace std; class A {

int i;

public: };

class B:public A { public: };

class C:public B {

B(int x=1):A(x){}

void operator!() {cout<<\A(int x=0):i(x){} int get(){return i;}


2016整理版北邮面向对象C++试题(房鸣)(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:习题10 - 事务管理与恢复

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

马上注册会员

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