#include <iostream>
using namespace std;
class box2;
class box1
{int weight;
public:
box1();
box1(int b1){weight=b1;};
friend int gettotalweight(box1 &b1,box2 &b2);
};
class box2
{int weight;
public:
box2();
box2(int b2){weight=b2;};
friend int gettotalweight(box1 &b1,box2 &b2);
};
int gettotalweight(box1 &b1,box2 &b2)
{
return(b1.weight+b2.weight);
}
main()
{box1 box1(88);
box2 box2(99);
cout<<gettotalweight(box1, box2);}
两个类的友元函数
本文介绍了如何在C++中使用类(class)和对象(object)的概念,通过定义类box1和box2,以及friend函数gettotalweight来计算两个盒子的总重量。实例展示了如何创建对象并调用成员函数实现功能。
摘要由CSDN通过智能技术生成