类成员,类成员的构造(冒号语法)(C++)

下面的一段程序中的 Cline 类中
缺少了部分成员函数,该程序的运行结果如下:
Point 1 is:(0,0)
Point 2 is:(0,0)
Length=0
Point 1 is:(1,1)
Point 2 is:(5,5)
Length=5.65685
请为 Cline 函数补充必要的成员函数与实现代码,使得程序正确运行。
#include <iostream>
#include <cmath>
using namespace std;
class CPoint
{
int m_x; //点的X坐标
int m_y; //点的Y坐标º
public:
CPoint()
{
m_x=0;
m_y=0;
}
CPoint (int x,int y)
{
m_x=x;
m_y=y;
}
int getx()
{
return m_x;
}
int gety()
{
return m_y;
}
void showPoint()
{
cout<<"("<<this->m_x<<","<<this->m_y<<")"<<endl;
}
};
class CLine
{
CPoint m_point1;
CPoint m_point2;
};
void main()
{
CLine line1;
line1.ShowLine();
cout<<"Length="<<line1.distance()<<endl;
CLine line2(1,1,5,5);
line2.ShowLine();
cout<<"Length="<<line2.distance()<<endl;
}
 

/*=======================================================
*学号:
*作业:E16
*功能:补充函数
*作者:
*日期:2016.5.15
*========================================================*/

#include<iostream>
#include<cmath>

using namespace std;

class CPoint
{
	int m_x;         //点x的坐标
	int m_y;         //点y的坐标
public:
	CPoint()         //构造函数
	{
		m_x=0;
		m_y=0;
	}
	CPoint(int x,int y)    //构造函数重载
	{
		m_x=x;
		m_y=y;
	}
	int getx()           //获取x的值
	{
		return m_x;
	}
	int gety()          //获取y的值
	{
		return m_y;
	}
	void showPoint()       //在屏幕上输出坐标的值
	{
		cout<<"("<<this->m_x<<","<<this->m_y<<")"<<endl;
	}
};

class CLine
{
	CPoint m_point1;
	CPoint m_point2;
public:
	CLine(){};                //无参构造函数
	CLine(int x1,int y1,int x2,int y2):m_point1(x1,y1),m_point2(x2,y2){};    //构造函数重载
	void ShowLine()
	{
		m_point1.showPoint();
		m_point2.showPoint();
	}
	double distance()
	{
		double d=sqrt((double)(m_point1.getx()-m_point2.getx())*(m_point1.getx()-m_point2.getx())+(m_point1.gety()-m_point2.gety())*(m_point1.gety()-m_point2.gety()));
		return d;
	}
};

void main()
{
	CLine line1;
	line1.ShowLine();
	cout<<"Length="<<line1.distance()<<endl;

	CLine line2(1,1,5,5);
	line2.ShowLine();
	cout<<"Length="<<line2.distance()<<endl;
}

猜你喜欢

转载自blog.csdn.net/ukco_well/article/details/86576690
今日推荐