实验8 类的成员
【实验目的】
通过本实验,强化理解类定义中不同成员的概念。
【实验要求】
熟练掌握建立不同对象的方法。
【实验内容】
实现下面类的定义及对象建立的过程,注意构造函数、赋值函数及析构函数的用法。
#include<iostream>
using namespace std;
class A{
private:
int x,y;
public:
A(){ cout<<"Constructing!"<<endl; x=1; y=1; }
A(int a,int b){ x=a; y=b; }
A(const A &a){ x=a.x+1; y=a.y+1; }
A& operator=(const A &a){
x=a.x+a.y;
y=a.x-a.y;
return *this;
}
~A(){ cout<<"x="<<x<<" y="<<y;
cout<<" Destructing!"<<endl;
}
int z;
void fun(){ cout<<"x="<<x<<", y="<<y;
z=x+y; cout<<", z="<<z<<endl;
}
};
int main(){
A h; h.fun();
A k(h); k.fun();
A g(2,3); g.fun();
A m; m=g; m.fun();
cout<<endl;
return 0;
}