posts - 131, comments - 12, trackbacks - 0, articles - 0
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

构造函数

Posted on 2012-09-24 10:47 盛胜 阅读(269) 评论(0)  编辑 收藏 引用 所属分类: vc++深入详解
#include<iostream.h>
class point
{
   public:
      int x;
      int y;
   point()
   {
      x=0;
      y=0;
   }
   point(int a,int b)
   {
      x=a;
      y=b;
   }
   void output()
   {
      cout<<x<<endl<<y<<endl;
   }
   void input(int x,int y)
   {
      this->x=x;
      this->y=y;
   }

}

void main()
{
   point pt(5,5);
   pt.input(10,10);
   pt.output();

}
输出结果为10 
10

如果把this指针去掉则为5
5

在使用构造函数时注意是否需要释放内存。~构造函数名()利用析构函数解决内存泄漏问题。

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理