主程序中创建类location的两个对象A和B,A的坐标点设置方式如下:
类Location的参考框架如下:
class Location
{public:
\x05 Location(double,double)//构造函数
\x05 double getx()//成员函数,取x坐标值
\x05 double gety()//成员函数,取y坐标值
double distance(Location &)//成员函数,求给定两点之间的距离
friend double distance(Location &,Location &)//友元函数,求给定两点之间的距离
private:
\x05 double x,y
}
坐标设置如下:
#include
#include
class Location
{
public:
\x05Location(double,double)//构造函数
\x05double getx()//成员函数,取x坐标值
\x05double gety()//成员函数,取y坐标值
\x05double distance(Location &)//成员函数,求给定两点之间的距离
\x05friend double distance(Location &,Location &)//友元函数,求给定两点之间的距离
private:
\x05double x,y
}
Location::Location(double x,double y)
{
\x05this->x = x
\x05this->y = y
}
double Location::getx()
{
\x05return this->x
}
double Location::gety()
{
\x05return this->y
}
double Location::distance(Location &locat)
{
\x05double dis = 0.0
\x05dis = sqrt((this->x - locat.x)*(this->x - locat.x) + (this->y - locat.y)*(this->y - locat.y))
\x05return dis
}
double distance(Location &locat1,Location &locat2)
{
\x05double dis = 0.0
\x05dis = sqrt((locat1.x - locat2.x)*(locat1.x - locat2.x) + (locat1.y - locat2.y)*(locat1.y - locat2.y))
\x05return dis
}
int _tmain(int argc,_TCHAR* argv[])
{
\x05Location A(1.0,2.0)
\x05Location B(4.0,6.0)
\x05std::cout
两个错误class Location{
public:
Location(int m,int n)
{X=mY=n
}
void Init(int initX,int initY)
{X=initXY=initY
}
int getX()
{return X
}
int getY()
{return Y
}
private:
int X,Y
}//类定义少了个分号
int main()
{
Location A3(3,2)//你的类的构造函数是带参的 所以你必须也要带参 这个是我随便改的
A3.Init(785,999)
cout<<A3.getX()<<" "<<A3.getY()<<endl
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)