};
23、指出下面程序中的错误,并改正
#include
class test {
private:
static int x;
public:
virtual static int func();
};
int test::x = 20;
int test::func() {
return x;
}
void main() {
cout<test a;
cout<}
24、指出下面程序重点错误,在错误处说明出错原因
#include
class a {
private:
int a;
public:
void seta( int x) { a = x; }
void showa() { cout<<"a="<};
class b: private a {
private:
int b;
public:
void setb(int x, int y) { b=x; seta(y); }
void showb() { showa(); cout<<"b="<};
void main() {
b obj;
obj.seta(53); obj.showa();
obj.setb(53,58); obj.showb();
}
25、下面程序希望产生的输出为4+3i,但是运行输出是3+3i,请指出错误并改正
#include
class complex {
int real; // 实部
int imag; // 虚部
public:
complex(int r=0; int i=0): real(r),imag(i) {}
void show() { cout<complex operator++() { real++; return * this; }
};
void main() {
complex o(2,3);
++++c;
c.show();
}
四、 完成程序题(4×5):
26、在下面程序的横线处填上适当字句,使该程序执行结果为6。
#include
class base {
int x;
public:
________________________________________// 为x置值
________________________________________// 取x值
};
void main()
base test;
test.init(6);
cout<}
27、在下面横线处填上适当字句,完成类中成员函数的定义:
# include
class line;
int x;
class box {
private:
int color;
int upx, upy;
int lowx, lowy;
public:
_____________________ int same_color( line a, box b);
void set_color( int c) { color = c; }
void define_line (int x, int y) { startx = x; }
};
int same_color (line a, box b) {
if (a.color = = b.color) return 1;
return 0;
}
28、在横线处填上适当字句,完成程序。
# include
class a {
public:
void f (int i) { cout<void g () { cout<<"g\n"; }
};
class b: a {
public:
void h() { cout<<"h\n"; }
_____________________ f ;
};
void main () {
b d1;
d1.f(6);
_____________________.h();
}
29、下面是一个栈的类模板,其中push是把元素i压入栈顶,栈初始为空,top值为0,栈顶元素在stack[top-1]中,在下面横线处填上适当语句,完成栈类模板的定义。
# include
template
class tstack {
enum { size = 1000 };
t stack[size];
int top;
public:
tstack (t c) _____________________ {
stack[top] = c;
}
void push(const t &i ) {
if (top}
t pop () {
return stack[top>0?- - top:top];
}
};
30、
class location {
private:
int x, y;
public:
void init( int initx, int inity);