关于类的问题
在下面的代码中。。。打了注释符的。。。请帮忙解释下,谢谢。#include<iostream.h>
class distance
{
int mile,yard;
public:
distance();
distance(int,int);
void get_value();
distance add_value(distance);//这里定义的是函数么?可以这样定义的么?就是说,中间可以有空格么?
void display();
};
distance::distance()
{
mile=0;
yard=0;
}
distance::distance(int v1,int v2)
{
mile=v1;
yard=v2;
}
void distance::get_value()
{
cout<<"Enter mile:";
cin>>mile;
cout<<"Enter yard:";
cin>>yard;
}
distance distance::add_value(distance A)//这里也不大明白,是类中函数的定义么?为什么不是distance::distance add_value(distance A)。
{
distance B;
B.yard=yard+A.yard;
B.mile=0;
if(B.yard>=1760)
{
B.mile=1;
B.yard-=1760;
}
B.mile+=mile+A.mile;
return B;
}
void distance::display()
{
cout<<mile<<"miles"<<yard<<"yaeds"<<endl;
}
void main()
{
distance D(10,100);
distance F,G;
F.get_value();
G=D.add_value(F);
cout<<"The first distance:";
D.display();
cout<<"The second distance:";
F.display();
cout<<"The third distance:";
G.display();
}