首页 > 其他 > 详细

友元函数

时间:2015-04-26 22:28:46      阅读:292      评论:0      收藏:0      [点我收藏+]

Description
定义Boat与Car两个类,二者都有weight属性,定义二者的一个友元函数totalWeight(),计算二者的重量和。
Input
多组数据。每组包含2个整数mn,分别表示BoatCat的重量。
Output
BoatCat的重量之和。
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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!