【问题描述】
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数getTotalWeight(),计算二者的重量和。
【输入形式】
参考的输入(数字前为提示文字):
Input boat weight:3
Input car weight:5
【输出形式】
参考的输出:
Total weight:8
【样例输入】
Input boat weight:3
Input car weight:5
【样例输出】
Total weight:8
【样例说明】
【评分标准】
#include <iostream>
using namespace std;
class Car;
class Boat
{
friend void getTotalWeight(Boat b,Car c);
public:
void getboat();
private:
int weight;
};
class Car
{
friend void getTotalWeight(Boat b,Car c);
public:
void getcar();
private:
int weight;
};
void Boat::getboat()
{
cout<<"Input boat weight:";
cin>>weight;
}
void Car::getcar()
{
cout<<"Input car weight:";
cin>>weight;
}
void getTotalWeight(Boat b,Car c)
{
cout<<"Total weight:"<<b.weight+c.weight;
}
int main()
{
Boat x;
Car y;
x.getboat();
y.getcar();
getTotalWeight(x,y);
return 0;
}