例:
#include <iostream>
using namespace std;
class Point {
private:
int x,y;
static int count;//静态数据成员声明,用于记录点的个数
public:
Point(int x=0,int y=0):x(x),y(y){
count++;
}
Point(Point &p){//复制构造函数
x = p.x;
y = p.y;
count++;
}
~Point(){ count--;}
int getX(){ return x;}
int getY(){ return y;}
void addCount(){ count++;}
// void showCount(){//输出静态数据成员
// cout<<" Object count = "<<count<<endl;
// }
static void showCount(){//静态数据成员
cout<<" Object count = "<<count<<endl;
}
};
int Point::count = 0;//静态数据成员定义和初始化,使用类名限定
int main(){
Point a(4,5);//定义对象a,其构造函数会使count增1
cout<<"Point A: "<<a.getX()<<","<<a.getY();
//a.showCount();//输出对象个数
Point::showCount();
Point b;
cout<<"Point B: "<<b.getX()<<","<<b.getY();
//b.showCount();//输出对象个数
Point::showCount();
return 0;
}
1.定义一个对象时,以本类另一个对象作为初始值
Point a(10,20);
Point b=a;
分析:将对象a赋给参数p,即右边是p.x=a.p,左边x的x,y是b的xy分量(this->x=p.x)
2.传参
void fun1(Point p){
cout<<p.get(X)<<endl;
}
调用语句:fun1(b);
3.函数值返回
Point fun2(){
return Point(1,2);
}
调用语句:b=fun2();