习题1 下载本文

int gety() {return y;} private: int x, y; };

int A::f1() {return x+y;} int A::f2() {return x-y;} void main() {

A a;

a.setx(10); a.sety(5);

cout<

4、

# include class T { public:

T(int x, int y) {

a=x; b=y;

cout<<”diao yong gou zao han shu 1.”<

} T(T &d)

{

9

cout<<”diao yong gou zao han shu 2.”<

cout<< d.a<<’\\t’<

} ~T() {cout<<”diao yong xi gou han shu.”<

int add(int x,int y=10) {return x+y;}

private: int a,b; };

void main() {

T d1(4,8);

T d2(d1);

cout<

}

diao yong gou zao han shu 1. 4 8

diao yong gou zao han shu 2. 4 8 20 20

diao yong xi gou han shu. diao yong xi gou han shu.

5、

#include class A { int a;

double b;

public:

10

A(int x=100,double y=1.2){a=x;b=y;} void show(char *pt) { cout<

}

};

void main() { A obj1,obj2(100,3.5); obj1.show(\ obj2.show(\ A *p; p=&obj1;

p->show(\ (*p).show(\ p=&obj2;

p->show(\

(*p).show(\ p=new A;

p->show(\ delete p;

}

6、

#include class K { public:

int x;

11

K (int i){x=i;}

void fun1(int j){x+=j;cout<<\ void fun2(int j){x+=j;cout<<\

};

void main() { K c1(3),c2(6);

void(K::*pfun)(int)=K::fun1; (c1.*pfun)(6); pfun=K::fun2; (c2.*pfun)(12);

}

7、

#include using namespace std; class TIME; class DATE { public: DATE(int a=2004,int m=9,int b=23) { year=a; month=m; day=b;

}

void DateTime(TIME &t);

private: int year,month,day;

};

class TIME { public:

12