西安石油大学大二(上)C++面向对象程序设计课件各章习题

class A {

private: int a,b; public :

A( ) { a=b=0;

cout<<\

}

A(int m) { a=b=m;

cout<<\ }

A(int m,int n) { a=m;b=n;

cout<<\ }

void print( ) { cout<<\ }

};

void main( ) { A a1,a2(10),a3(10,20); a1.print( ); a2.print( ); a3.print( ); }

2)

#include \class point {

private:

int x;

16

static int y; public:

point(int px=10) { x=px; y++;

} static int getpx(point a)

{ return(a.x); }

static int getpy(point b) { return(b.y); } void setx(int c)

{ x=c;

}

};

int point::y=0; void main() { point p[5];

for(int i=0;i<5;i++) p[i].setx(i); for (i=0;i<5;i++) { cout<

}

3)

#include \class sam {

int x; public:

17

void setx(int i) { x=i;

}

int putx() { return x; }

};

void main() {

sam *p; sam s[3];

for(int i=0;i<3;i++) s[i].setx(i+4); p=s;

for(i=0;i<3;i++)

cout<putx()<

4)

#include #include class A { char *a; public:

A(char *s) {

a=new char[strlen(s)+1]; strcpy(a,s); cout<

delete []a; cout<<\ }

};

void main() {

A x(\

18

A *y=new A(\ delete y; }

(5)

#include class A { int *a; public:

A(int x=0):a(new int(x)){} ~A() {delete a;}

int getA() {return *a;} void setA(int x) {*a=x;} };

void main() { A x1,x2(3); A *p=&x2; p->setA(x2.getA()+5);

x1.setA(15+x1.getA());

cout<

(6)

#include class A { int a; public:

A(int aa=0): a(aa) {cout<

void main() { A *p; A x[3]={1,2,3},y=4; cout<

delete []p; cout<

}

(7)

19

#include class A { int a,b; public:

A() {a=b=0;}

A(int aa, int bb) {a=aa; b=bb;} int Sum() {return a+b;} int* Mult() { int *p=new int(a*b); return p;

}

};

void main() { int *k;

A x(2,3), *p; p=new A(4,5);

cout<Sum()<<' '<<*(k=p->Mult())<

8)

#include class A {

int a[10]; int n; public:

A(int aa[], int nn): n(nn) {

for(int i=0; i

int Get(int i) {return a[i];} int SumA(int n) { int s=0;

for(int j=0; j

}

};

void main() {

int a[]={2,5,8,10,15,20}; A x(a,4);

20

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4