⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 demo_constructor_assemble_3.cpp

📁 对于一个初涉VC++的人来书
💻 CPP
字号:

//***************************************************

#include <iostream.h>
#include <math.h>

class Point	 //Point类定义
{
public:
	Point(int xx=0, int yy=0) {X=xx;Y=yy;}
	Point(Point &p);
	int GetX() {return X;}
	int GetY() {return Y;}

private:
	int X,Y;
};

Point::Point(Point &p)	//拷贝构造函数的实现
{
	X=p.X;
	Y=p.Y;
	cout<<"Point Class Copy Constructor Is Called."<<endl;
}

//类的组合(聚集)
class Distance	//Distance类的定义
{
public:	  //外部接口
	Distance(Point xp1, Point xp2);
	double GetDis(){return dist;}
private:  //私有数据成员
	Point p1,p2;  //Point类的对象p1,p2
	double dist;	
};

//类的组合的构造函数
Distance::Distance(Point xp1, Point xp2):p1(xp1),p2(xp2) //冒号语法
{
	cout<<"Distance Class Copy Constructor Is Called."<<endl;
	double x=double(p1.GetX()-p2.GetX());
	double y=double(p1.GetY()-p2.GetY());
	dist=sqrt(x*x+y*y);
}

void main()  //主函数
{
	Point myp1(1,1),myp2(4,5);	//定义Point类的对象
	Distance myd(myp1,myp2);	//定义Distance类的对象

	cout<<"The distance is: ";
	cout<<myd.GetDis()<<endl;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -