您的位置:首页 > 编程语言 > C语言/C++

菜鸟学C++小记

2014-03-12 21:23 651 查看
#include <iostream>

using namespace std;

class Human

{

public:

int get()const{return *i;}

void set(int x){*i=x;}

~Human();

Human();

private:

int *i;

};

int main()

{

Human *p=new Human;

p->set(1); //如果屏蔽这一句,结果为“构造函数执行中··· 999 析构函数执行中···”

//如果不屏蔽这一句,结果为“构造函数执行中··· 1 析构函数执行中···”

cout<<p->get()<<endl;

delete p;

return 0;

}

Human::Human()

{

cout<<"构造函数执行中...\n";

i=new int(999);

}

Human::~Human()

{

cout<<"析构造函数执行中...\n";

delete i;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: