菜鸟学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;
}
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;
}
相关文章推荐
- C++线性运算库梳理及Win/Linux下编译小记
- 菜鸟也能搞定C++内存泄漏
- 小记:c++ vector 可能占用大量内存
- C++菜鸟进阶—>什么时候使用继承和组合
- 菜鸟小鱼啄定C++
- c++ bitset学习小记
- C++菜鸟进阶——>关于重载、隐藏和重写
- 【菜鸟C++学习笔记】9.三目运算符
- C++菜鸟常见错误整理(第一天)
- c++超级菜鸟
- 【菜鸟C++学习笔记】11.内联函数与const成员函数
- 菜鸟潇寒学习C++笔记------第二部分
- 菜鸟运维笔记:小记编译安装Nginx所遇到的坑
- C++菜鸟常见错误整理(第十一天)
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习3
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第4章编程练习9
- 我是菜鸟 学好C++C#有什么秘诀吗
- c++代码的陪伴下----菜鸟的转变
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习1
- 编程菜鸟的日记-初学尝试编程-C++ Primer Plus 第5章编程练习8