请C++高手们帮我找一下以下程序的错误吧!(在VC++6.0环境下编译的)
#include<iostream.h>#include<math.h>classDistance;classPoint{private:doublex;doubley;...
#include<iostream.h>
#include<math.h>
class Distance;
class Point
{
private:
double x;
double y;
public:
Point(double a=0,double b=0);
void set_point(double a,double b);
double get_x();
double get_y();
friend Distance;
};
class Distance
{
public:
double output_distance(Point &p1,Point &p2);
};
Point::Point(double a=0,double b=0)
{
x=a;
y=b;
}
void Point::set_point(double a,double b)
{
x=a;
y=b;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
double Distance::output_distance(Point &p1,Point &p2)
{
double y1,y2;
y1=(p1.x-p2.x)*(p1.x-p2.x);
y2=(p1.y-p2.y)*(p1.y-p2.y);
return sqrt(y1+y2);
}
void main()
{
double x1,y1,x2,y2;
cout<<"默认的两点是(0,0)和(1,1)"<<endl;
Point p1,p2(1,1);
Distance d;
cout<<"故两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
cout<<"现在重新定义两点:"<<endl;
cout<<"请输入第一个点的:"<<endl;
cout<<"横坐标:";
cin>>x1;
cout<<"纵坐标:";
cin>>y1;
cout<<"请输入第二个点的:"<<endl;
cout<<"横坐标:";
cin>>x2;
cout<<"纵坐标:";
cin>>y2;
p1.set_point(x1,y1);
p2.set_point(x2,y2);
cout<<"两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
} 展开
#include<math.h>
class Distance;
class Point
{
private:
double x;
double y;
public:
Point(double a=0,double b=0);
void set_point(double a,double b);
double get_x();
double get_y();
friend Distance;
};
class Distance
{
public:
double output_distance(Point &p1,Point &p2);
};
Point::Point(double a=0,double b=0)
{
x=a;
y=b;
}
void Point::set_point(double a,double b)
{
x=a;
y=b;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
double Distance::output_distance(Point &p1,Point &p2)
{
double y1,y2;
y1=(p1.x-p2.x)*(p1.x-p2.x);
y2=(p1.y-p2.y)*(p1.y-p2.y);
return sqrt(y1+y2);
}
void main()
{
double x1,y1,x2,y2;
cout<<"默认的两点是(0,0)和(1,1)"<<endl;
Point p1,p2(1,1);
Distance d;
cout<<"故两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
cout<<"现在重新定义两点:"<<endl;
cout<<"请输入第一个点的:"<<endl;
cout<<"横坐标:";
cin>>x1;
cout<<"纵坐标:";
cin>>y1;
cout<<"请输入第二个点的:"<<endl;
cout<<"横坐标:";
cin>>x2;
cout<<"纵坐标:";
cin>>y2;
p1.set_point(x1,y1);
p2.set_point(x2,y2);
cout<<"两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
} 展开
4个回答
展开全部
我来总结一下
(1)构造函数声明那不给默认形参值,后面定义给出就可以了。
Point(double a,double b);//这里不用加默认形参值
Point::Point(double a=0,double b=0)//使用后就可以加
这是一种符合规范的书写格式,因为主要的是声明函数,不需要指定出的默认值,等使用后就可以加上自己的默认值了.
(2)在构造函数声明加默认形参值,后面定义时不要加.
因为在声明的时候规定了默认值,所以在重新定义的时候就不需要再加默认的参数.
(3)不用声明,直接定义
这种也是可以的,在类里面就直接写好了,也就不需要声明了.
(1)构造函数声明那不给默认形参值,后面定义给出就可以了。
Point(double a,double b);//这里不用加默认形参值
Point::Point(double a=0,double b=0)//使用后就可以加
这是一种符合规范的书写格式,因为主要的是声明函数,不需要指定出的默认值,等使用后就可以加上自己的默认值了.
(2)在构造函数声明加默认形参值,后面定义时不要加.
因为在声明的时候规定了默认值,所以在重新定义的时候就不需要再加默认的参数.
(3)不用声明,直接定义
这种也是可以的,在类里面就直接写好了,也就不需要声明了.
展开全部
Point::Point(double a,double b)///声明时候加默认值,定义的时候不用加
{
x=a;
y=b;
}
{
x=a;
y=b;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
构造函数声明那儿不要给默认形参值,后面定义给出就可以了。
#include<iostream.h>
#include<math.h>
class Distance;
class Point
{
private:
double x;
double y;
public:
Point(double a,double b);//这里不用加默认形参值
void set_point(double a,double b);
double get_x();
double get_y();
friend Distance;
};
class Distance
{
public:
double output_distance(Point &p1,Point &p2);
};
Point::Point(double a=0,double b=0)
{
x=a;
y=b;
}
void Point::set_point(double a,double b)
{
x=a;
y=b;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
double Distance::output_distance(Point &p1,Point &p2)
{
double y1,y2;
y1=(p1.x-p2.x)*(p1.x-p2.x);
y2=(p1.y-p2.y)*(p1.y-p2.y);
return sqrt(y1+y2);
}
void main()
{
double x1,y1,x2,y2;
cout<<"默认的两点是(0,0)和(1,1)"<<endl;
Point p1,p2(1,1);
Distance d;
cout<<"故两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
cout<<"现在重新定义两点:"<<endl;
cout<<"请输入第一个点的:"<<endl;
cout<<"横坐标:";
cin>>x1;
cout<<"纵坐标:";
cin>>y1;
cout<<"请输入第二个点的:"<<endl;
cout<<"横坐标:";
cin>>x2;
cout<<"纵坐标:";
cin>>y2;
p1.set_point(x1,y1);
p2.set_point(x2,y2);
cout<<"两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
}
#include<iostream.h>
#include<math.h>
class Distance;
class Point
{
private:
double x;
double y;
public:
Point(double a,double b);//这里不用加默认形参值
void set_point(double a,double b);
double get_x();
double get_y();
friend Distance;
};
class Distance
{
public:
double output_distance(Point &p1,Point &p2);
};
Point::Point(double a=0,double b=0)
{
x=a;
y=b;
}
void Point::set_point(double a,double b)
{
x=a;
y=b;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
double Distance::output_distance(Point &p1,Point &p2)
{
double y1,y2;
y1=(p1.x-p2.x)*(p1.x-p2.x);
y2=(p1.y-p2.y)*(p1.y-p2.y);
return sqrt(y1+y2);
}
void main()
{
double x1,y1,x2,y2;
cout<<"默认的两点是(0,0)和(1,1)"<<endl;
Point p1,p2(1,1);
Distance d;
cout<<"故两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
cout<<"现在重新定义两点:"<<endl;
cout<<"请输入第一个点的:"<<endl;
cout<<"横坐标:";
cin>>x1;
cout<<"纵坐标:";
cin>>y1;
cout<<"请输入第二个点的:"<<endl;
cout<<"横坐标:";
cin>>x2;
cout<<"纵坐标:";
cin>>y2;
p1.set_point(x1,y1);
p2.set_point(x2,y2);
cout<<"两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
直接把构造函数定义放在Point类里面就可以了,试试下面的,我这可以了
#include<iostream.h>
#include<math.h>
class Distance;
class Point
{
private:
double x;
double y;
public:
Point(double a=0,double b=0)
{
x=a;
y=b;
}
void set_point(double a,double b);
double get_x();
double get_y();
friend Distance;
};
class Distance
{
public:
double output_distance(Point &p1,Point &p2);
};
void Point::set_point(double a,double b)
{
x=a;
y=b;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
double Distance::output_distance(Point &p1,Point &p2)
{
double y1,y2;
y1=(p1.x-p2.x)*(p1.x-p2.x);
y2=(p1.y-p2.y)*(p1.y-p2.y);
return sqrt(y1+y2);
}
void main()
{
double x1,y1,x2,y2;
cout<<"默认的两点是(0,0)和(1,1)"<<endl;
Point p1,p2(1,1);
Distance d;
cout<<"故两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
cout<<"现在重新定义两点:"<<endl;
cout<<"请输入第一个点的:"<<endl;
cout<<"横坐标:";
cin>>x1;
cout<<"纵坐标:";
cin>>y1;
cout<<"请输入第二个点的:"<<endl;
cout<<"横坐标:";
cin>>x2;
cout<<"纵坐标:";
cin>>y2;
p1.set_point(x1,y1);
p2.set_point(x2,y2);
cout<<"两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
}
#include<iostream.h>
#include<math.h>
class Distance;
class Point
{
private:
double x;
double y;
public:
Point(double a=0,double b=0)
{
x=a;
y=b;
}
void set_point(double a,double b);
double get_x();
double get_y();
friend Distance;
};
class Distance
{
public:
double output_distance(Point &p1,Point &p2);
};
void Point::set_point(double a,double b)
{
x=a;
y=b;
}
double Point::get_x()
{
return x;
}
double Point::get_y()
{
return y;
}
double Distance::output_distance(Point &p1,Point &p2)
{
double y1,y2;
y1=(p1.x-p2.x)*(p1.x-p2.x);
y2=(p1.y-p2.y)*(p1.y-p2.y);
return sqrt(y1+y2);
}
void main()
{
double x1,y1,x2,y2;
cout<<"默认的两点是(0,0)和(1,1)"<<endl;
Point p1,p2(1,1);
Distance d;
cout<<"故两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
cout<<"现在重新定义两点:"<<endl;
cout<<"请输入第一个点的:"<<endl;
cout<<"横坐标:";
cin>>x1;
cout<<"纵坐标:";
cin>>y1;
cout<<"请输入第二个点的:"<<endl;
cout<<"横坐标:";
cin>>x2;
cout<<"纵坐标:";
cin>>y2;
p1.set_point(x1,y1);
p2.set_point(x2,y2);
cout<<"两点之间的距离是:"<<d.output_distance(p1,p2)<<endl;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询