
.#include
#include
int count=0;
class Point{
int x,y;
public:
Point(){x=1;y=1;count++;}
~Point(){count--;}
friend void display();
};
void display()
{ cout <<"There are "<
{ Point a;
display();
{ Point b[5];
display();
}
display();
}
两个问题注意了就行了.class是一个类,当创建一个对象时,先调用类的构造函数,撤销类对象时调用析构函数.
#include
#include
using namespace std;
int count=0;//全局变量
class Point{//定义一个point类
int x,y;//类成员
public:
Point(){x=1;y=1;count++;}//构造函数实现count++;
~Point(){count--;}//析构函数凯御实现count--
friend void display();//友元
};
void display()
{ cout <<"There are "<
{ Point a;//定义Point的对象a,这里需要注意,当你定义一个对象会调用构造函数.此时count=1
display();//执行display函数
{ Point b[5];//对象数组,需要初始化盯喊岩5次,count变为6
display();
}//对象数组由一对括号包住,证明对象数组的作用范围到此结束,次数需要调用析构函数,老规矩,调用5次,此渗锋时count又变回1
display();
}