标签:
Description |
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和。 |
Input |
多组数据。每组包含2个整数m,n,分别表示Boat和Cat的重量。 |
Output |
Boat和Cat的重量之和。 |
Sample Input |
4 5
6 8 |
Sample Output |
9 14
|
Hint |
知识点:友元函数 |
#include <iostream>
#include<cmath>
using namespace std;
class Car;
class Boat
{
public:
int weight;
friend int totalWeight(Boat &boat, Car &car);
Boat(int a):weight(a){}
};
class Car
{
public:
int weight;
friend int totalWeight(Boat &boat, Car &car);
Car(int a):weight(a){}
};
int totalWeight(Boat &boat, Car &car)
{
return boat.weight+car.weight;
}
int main()
{
int boatw,carw;
while(cin>>boatw>>carw)
{
Boat boat(boatw);Car car(carw);
cout<<totalWeight(boat,car)<<endl;
}
return 0; `
}
标签:
原文地址:http://www.cnblogs.com/zeross/p/4458472.html