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);
}

相关文章:

  • 2021-05-08
  • 2021-11-20
  • 2022-01-09
  • 2021-05-27
  • 2021-07-19
猜你喜欢
  • 2021-06-12
  • 2021-08-13
  • 2021-06-18
  • 2021-11-17
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
相关资源
相似解决方案