源程序:
//2.使用内联函数设计一个类,用来表示直角坐标系中的任意一条直线并输出它的属性。
#include < iostream.h >
#include < math.h >
class Line
{
private:
int x1, y1, x2, y2;
public :
//Line();
Line(int =0, int =0, int =0, int=0 );
void printPoint();
double getLength();
};
inline Line::Line(int a, int b, int c, int d)
{
x1 = a;
y1 = b;
x2 = c;
y2 = d;
}
inline void Line::printPoint()
{
cout<<"A:"<< x1 <<", "<< y1 << endl;
cout<<"B:"<< x2 <<", "<< y2 << endl;
}
inline double Line::getLength()
{
double length;
length = sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) );
return length;
}
void main()
{
Line line(10,80,-10,12);
line.printPoint();
cout<< line.getLength() << endl;
}
运行结果: