问题描述】声明boat与car两个类,两者都有weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和。类的信息按需要自行定义。
【输入形式】在主程序中输入一个boat对象以及一个car对象的重量
【输出形式】输出两个对象的重量和
【样例输入1】4 5
【样例输出1】9
【样例输入2】5.5 5.5
【样例输出2】11
我自己写的代码如下:
#include<stdlib.h>
#include<iostream>
using namespace std;
class car;
class boat
{
private:
float weight;
public:
boat(float a)
{
a=weight;
}
void totalweight(car &t);
};
class car
{
private:
float weight;
public:
car(float b)
{
b=weight;
}
friend void boat::totalweight(car &t);
};
void boat::totalweight(car &t)
{
float n=t.weight+weight;
cout<<n<<endl;
}
int main()
{
float c,m;
cin>>c>>m;
car t1(m);
boat w(c);
w.totalweight(t1);
system("pause");
return 0;
}
但是我的输出的是n的地址。
请问是什么原因?