/*
定义Boat与Car两个类,二者都有weight属性,
定义二者的一个友元函数totalWeight()为外部函数,
计算二者的重量和。
*/
#include<iostream>
#include<string>
using namespace std;
class Boat;
class Car {
private:
double weight;
public:
Car(double wg) {
this->weight = wg;
}
Car(Car &tp) {
this->weight = tp.weight;
}
double getWeight() {
return weight;
}
~Car() { }
void show() {
cout << "This is a Car:" << endl;
cout << "Weight:" << weight << endl;
}
};
class Boat {
private:
double weight;
public:
Boat(double wg) {
this->weight = wg;
}
Boat(Boat &tp) {
this->weight = tp.weight;
}
double getWeight() {
return weight;
}
~Boat()
{ }
friend double totalWeight(Boat &a, Car &b);
void show() {
cout << "This is a Boat:" << endl;
cout << "Weight:" << weight << endl;
}
};
double totalWeight(Boat &a, Car &b) {
return a.getWeight() + b.getWeight();
}
int main() {
Boat a(1024.0);
Car b(1024.0);
a.show();
b.show();
cout << "The total weight of the Car and the Boat:" << endl;
cout << totalWeight(a, b);
return 0;
}
  • 测试截图

最新文章

  1. ecshop登录
  2. createStatement()的用法
  3. MMTx使用说明
  4. 安装Django,运行django-admin.py startproject 工程名,后不出现指定的工程解决办法!!
  5. HTML5标准简介
  6. sixsix团队“餐站”应用M2阶段发布报告
  7. 操作properties文件,注意抹掉最前面的&quot;file:&quot;
  8. DB2行转列、列转行等操作
  9. Luence简单实现2
  10. poj 2230 Watchcow(欧拉回路)
  11. Is it possible to change the iPhone device name programmatically?
  12. memcached部署memcached环境及PHP扩展
  13. HTML 基础 2
  14. Javascript禁止父元素滚动条滚动, pc、移动端均有效
  15. Web用户控件
  16. WordPress二级菜单设置
  17. Android应用跳转到微信和支付宝扫一扫
  18. C#代码总结04---通过创建临时表DataTable进行临时编辑删除
  19. github仓库的初步使用
  20. python redis存入字典序列化存储

热门文章

  1. 【学习笔记】Java实用类:枚举(Enum)
  2. Java ScheduledExecutorService源码分析
  3. python 爬虫初试
  4. asp: suggest ajax
  5. Vue element-ui:滚动条 分页 禁用选项
  6. 利用函数回调获取setInterval中返回的值
  7. 数据结构复习之C语言malloc()动态分配内存概述
  8. 打杂程序员之nginx服务配置
  9. 使用CKRule实现PVC配方计算
  10. c++类模板成员函数报错