#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
class Dog
{
public:
//构造函数
Dog(){
++count;
}
//拷贝构造函数
Dog( const Dog & g ){
++count;
}
//析构函数
~Dog(){}
//静态函数
static int getCount(){
return count;
}
protected:
//静态成员
static int count;
};
//静态成员初始化
int Dog::count =0;
int main(int argc, char** argv) {
//定义类对象
Dog a;
//通过类名调用静态函数
cout<<Dog::getCount()<<endl;
//通过拷贝构造函数定义类对象
Dog b(a);
//通过类对象调用静态函数
cout<<b.getCount()<<endl;
return 0;
}
C++ work4
最新推荐文章于 2024-08-23 21:39:27 发布