1、编写一程序,构造一个类Point_int,使该类对象表示二维平面上的一点(横、纵坐标用int型数表示);构造一个类Point_double,使该类对象表示二维平面上的一点(横、纵坐标用double型数表示)。重载友元函数distance以表示平面上两点间的距离。在main函数中:①先输入平面上(int型数表示的)两个点p1、p2的坐标;②然后输入(double型数表示的)两个点p3、p4的坐标;③最后调用友元函数distance,在main函数中分别输出p1与p2间、p3与p4间、p1与p4间的距离。
#include <iostream>
#include <math.h>
class Point_double;
class Point_int
{
friend double distance(const Point_int& point1, const Point_int& point2);
friend double distance(const Point_int& point1, const Point_double& point2);
friend double distance(const Point_double& point1, const Point_int& point2);
protected:
int m_x, m_y;
public:
Point_int(int x = 0, int y = 0)
: m_x(x)
, m_y(y)
{
}
};
class Point_double
{
friend double distance(const Point_double& point1, const Point_double& point2);
friend double distance(const Point_double& point1, const Point_int& point2);
friend double distance(const Point_int& point1, const Point_double& point2);
protected:
double m_x, m_y;
public:
Point_double(double x = 0.0, double y = 0.0)
: m_x(x)
, m_y(y)
{
}
};
double distance(const Point_double& point1, const Point_double& point2)
{
return sqrt((point1.m_x - point2.m_x) * (point1.m_x - point2.m_x)
+ (point1.m_y - point2.m_y) * (point1.m_y - point2.m_y));
}
double distance(const Point_int& point1, const Point_int& point2)
{
return sqrt(((double)point1.m_x - point2.m_x) * ((double)point1.m_x - point2.m_x)
+ ((double)point1.m_y - point2.m_y) * ((double)point1.m_y - point2.m_y));
}
double distance(const Point_int& point1, const Point_double& point2)
{
return sqrt((point1.m_x - (double)point2.m_x) * (point1.m_x - (double)point2.m_x)
+ (point1.m_y - (double)point2.m_y) * (point1.m_y - (double)point2.m_y));
}
double distance(const Point_double& point1, const Point_int& point2)
{
return sqrt(((double)point1.m_x - point2.m_x) * ((double)point1.m_x - point2.m_x)
+ ((double)point1.m_y - point2.m_y) * ((double)point1.m_y - point2.m_y));
}
int main()
{
int nx = 0;
int ny = 0;
std::cout << "第一个点坐标(int):";
std::cin >> nx >> ny;
Point_int p1(nx, ny);
std::cout << "第二个点坐标(int):";
std::cin >> nx >> ny;
Point_int p2(nx, ny);
double dx = 0.0;
double dy = 0.0;
std::cout << "第三个点坐标(double):";
std::cin >> dx >> dy;
Point_double p3(dx, dy);
std::cout << "第四个点坐标(double):";
std::cin >> dx >> dy;
Point_double p4(dx, dy);
std::cout << "p1p2:" << distance(p1, p2) << std::endl;
std::cout << "p3p4:" << distance(p3, p4) << std::endl;
std::cout << "p1p4:" << distance(p1, p4) << std::endl;
system("pause");
return 0;
}