网站开发 ppt,自己做网站的二维码,照片做视频的网站,担路做网站定义猫和老鼠#xff1a;Cat与Mouse两个类#xff0c;二者都有weight属性#xff0c;定义二者的一个友元函数totalweight()#xff0c;计算二者的重量和。
裁判测试程序样例#xff1a;
#include iostream
using namespace std;/* 请在这里填写答案 */int main(…定义猫和老鼠Cat与Mouse两个类二者都有weight属性定义二者的一个友元函数totalweight()计算二者的重量和。
裁判测试程序样例
#include iostream
using namespace std;/* 请在这里填写答案 */int main()
{int w1,w2;cinw1w2;Cat tom(w1);Mouse jerry(w2);couttotalWeight(tom,jerry)endl;
}
输入样例
100 200输出样例
300思路
// 前向声明猫类以便在鼠类中声明友元函数时使用
class Cat;// 鼠类
class Mouse{
public:// 鼠类的构造函数用于初始化重量属性Mouse(int w):weight(w){} // 声明totalWeight函数为鼠类的友元函数以便在函数中访问鼠类的私有成员friend int totalWeight(const Cat c, const Mouse m);
private:int weight; // 鼠的重量
};// 猫类
class Cat{int weight; // 猫的重量
public:// 猫类的构造函数用于初始化重量属性Cat(int w):weight(w){} // 声明totalWeight函数为猫类的友元函数以便在函数中访问猫类的私有成员friend int totalWeight(const Cat c, const Mouse m);
};// 计算猫和鼠的总重量的友元函数
int totalWeight(const Cat c, const Mouse m) { return c.weight m.weight; // 返回猫和鼠的总重量
}