int getx();
int gety();
};
void location::init (int initx, int inity) {
x = initx; y = inity;
}
int location::getx() {
return x;
}
int location::gety() {
return y;
}
#include
void main() {
location a1;
_____________________ // 定义一个指向a1的指针pa1;
_____________________ // 用pa1将对象a1的书籍成员x和y分别初始化为6和8
}
五、 程序分析题(5×6):
31、
# include
class a {
int * a;
public:
a (int x) { a = new int(x); cout<<"*a = "<<*a<};
void main() {
a x(3), *p;
p = new a (5);
delete p;
}
32、
# include
template
void f (t &x, q &y) {
if (sizeof (t) > sizeof (q)) x = (t)y;
else y = (q)x;
}
void main() {
double d;
int i;
d = 9999; i = 88;
f (d,i);
cout << "d=" << d << " i= " << i << endl;
d = 88; i = 9999;
f (i,d);
cout << "d=" << d << " i= " << i << endl;
}
33、
# include
class base {
public:
virtual int func () { return 0; }
};
class derived: public base {
public:
int func() { return 100; }
};
void main() {
derived d;
base& b = d;
cout << b.func() << endl;
cout << b.base::func() << endl;
}
34、
# include
class test {
private:
static int val;
int a;
public:
static int func();
static void sfunc(test &r);
};
int test::val = 20;
int test::func() {
val += val;
return val;
}
void test::sfunc(test &r) {
r.a = 25;
cout << " result3 = " << r.a;
}
void main() {
cout << "result1 = " << test::func() << endl;
test a;
cout << "result2 = " << a.func();
test::sfunc(a);
}