c++主函数中的调用

来源:百度知道 编辑:UC知道 时间:2024/09/22 10:39:41
#include <iostream>
#include <math.h>
using namespace std;
class point
{
public :
point(double a,double b);
point(const point &p);
~point();
double Len(point p);
double GetX();
double GetY();
private :
double x,y;
};
class Traingle
{
public :
Traingle(point n,point m,point s);
Traingle(Traingle & T);
~Traingle();
double Getlen();
private :
point p1,p2,p3;
};

double point ::Len(point p)
{
return sqrt((p.x-x)*(p.x-x)+(p.y-y)*(p.y-y));
}
point ::point(double a,double b)
{
x=a;
y=b;
}
point::point(const point &p) :x(p.x),y(p.y)
{
}
point :: ~point(){}
double point ::GetX()
{
return x;
}
double point ::GetY()
{
return y;
}
Traingle ::Traingle(point n,point m ,point s): p1(n),p2(m),p3(s)
{
}

void main()
{
point a(1,2);
point b(3,5);
point c(2,4);
Traingle d( a, b, c );
cout << d.Getlen() << endl;
}
很明显,你还没有区别清楚什么是类和类的对象,好好看书

Traingle是个类,要用的话得声明个对象,也就是Traingle a,再用a去调用成员函数。或者将要调用的成员函数声明为静态的就可以直接调用:Traingle::函数。