好文档 - 专业文书写作范文服务资料分享网站

C++练习题(附答案)

天下 分享 时间: 加入收藏 我要投稿 点赞

static int b; const int c; };

int Test::b = 0;

在标注号码的行中,能被正确编译的是 A)① B)② C)③ D)④ (30)有如下程序: #include using namespace std; class A { public:

A() { cout << \ ~A() { cout << \ };

class B : public A { A* p; public:

B() { cout << \ p = new A(); }

~B() { cout << \ delete p; } }; int main() {

B obj; return 0; }

执行这个程序的输出结果是 A)BAA~A~B~A B)ABA~B~A~A C)BAA~B~A~A D)ABA ~A~B~A (32)有如下程序: #include using namespace std; class Base {

public:

void fun1() { cout << \ virtual void fun2() { cout \ };

class Derived : public Base {

<< public:

void fun1() { cout << \ void fun2() { cout << \ };

void f(Base& b) { b.fun1(); b.fun2(); } int main() {

Derived obj; f(obj); return 0; }

执行这个程序的输出结果是 A) B) Base Base Base Derived C) D) Derived Derived Base Derived (33)有如下程序: #include using namespace std;

class Complex {

double re, im; public:

Complex(double r, double i) : re(r), im(i) { }

double real() const { return re; } double image() const { return im; } Complex& operator += (Complex a) {

re += a.re; im += a.im; return *this; } };

ostream& operator << (ostream& s, const Complex& z) { return

s<<'('<

int main()

{

Complex x(1,-2), y(2,3); cout << (x+=y) << endl; return 0; }

执行这个程序的输出结果是 A)(1,-2) B)(2,3) C)(3,5) D)(3,1) (35)有如下类声明: class SAMPLE { int n; public:

SAMPLE(int i=0):n(i){} void setValue(int n0); };

下列关于 getValue 成员函数的实现中,正确的是

A) SAMPLE::setValue(int n0){ n=n0;} B)void SAMPLE::setValue(int n0){ n=n0;}

C++练习题(附答案)

staticintb;constintc;};intTest::b=0;在标注号码的行中,能被正确编译的是A)①B)②C)③D)④(30)有如下程序:#includeusingnamespacestd;classA{public:
推荐度:
点击下载文档文档为doc格式
0vlf58kind1h1yk7phhy1xkfw968dk01aux
领取福利

微信扫码领取福利

微信扫码分享