现在的位置: 首页 > 综合 > 正文

c++笔记0

2013年10月21日 ⁄ 综合 ⁄ 共 484字 ⁄ 字号 评论关闭

class Point{
int x ;
int y ;
};

 对于上面定义的类, c++编译器一般默认定义四个函数:不带参数的构造函数(默认构造函数),带一个参数为该类对象的构造函数(拷贝构造函数),赋值函数(=),析构函数。示例代码:

/*demo1.cpp*/
#include <iostream>
using namespace std;
class Point{
public:
	int x ; 
	int y ;
public:
/*	Point( const Point &a)
	{
           x = a.x ; 
		   y = a.y ;
		   cout<<"copy construct"<<endl ; 
	}
	Point():x(0),y(0)
	{}*/  //第二编译请去掉该注释语句            
	void outPut()
	{
		cout<<x<<endl<<y<<endl ;
	}
};
Point Copy( const Point &a )
{
      Point  tmp ;
	  tmp.x = a.x ; 
	  tmp.y = a.y ; 
	  return tmp ;
}
int main()
{
        Point  p1,p2,p3 ;
	p1.x  = 1 ; 
	p1.y = 2 ;
	p3 = p1 ;
	p2 = Copy(p1);
	p2.outPut();
	p3.outPut();
	return 0 ;
}

抱歉!评论已关闭.