雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

定义两个类共同的友元函数

Posted on 2011-04-04 15:56  huhuuu  阅读(830)  评论(0编辑  收藏  举报
View Code
#include<iostream>
using namespace std;

class Car;//为了使class Boat中可以定义Car类,可以暂时先声明可以引用Car类
class Boat
{
public:
Boat(
int tBweight=0)
{
Bweight
=tBweight;
}
friend
class Car;
friend
void getTotalWeight(Car &C,Boat &B);//共同友元函数在每个引用的类都要定义
private:
int Bweight;
};

class Car
{
public:
Car(
int tCweight=0)
{
Cweight
=tCweight;
}
friend
class Boat;
friend
void getTotalWeight(Car &C,Boat &B);//共同友元函数在每个引用的类都要定义
private:
int Cweight;
};

void getTotalWeight(Car &C,Boat &B)
{
int all;
all
=B.Bweight+C.Cweight;
cout
<<all<<endl;
}

void main()
{
Boat boat(
1);
Car car(
2);
getTotalWeight(car,boat);
}