C++练习--创建Boat类和Car类(含友元)
2024-08-27 14:32:58
/*
定义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;
}
- 测试截图
最新文章
- ecshop登录
- createStatement()的用法
- MMTx使用说明
- 安装Django,运行django-admin.py startproject 工程名,后不出现指定的工程解决办法!!
- HTML5标准简介
- sixsix团队“餐站”应用M2阶段发布报告
- 操作properties文件,注意抹掉最前面的";file:";
- DB2行转列、列转行等操作
- Luence简单实现2
- poj 2230 Watchcow(欧拉回路)
- Is it possible to change the iPhone device name programmatically?
- memcached部署memcached环境及PHP扩展
- HTML 基础 2
- Javascript禁止父元素滚动条滚动, pc、移动端均有效
- Web用户控件
- WordPress二级菜单设置
- Android应用跳转到微信和支付宝扫一扫
- C#代码总结04---通过创建临时表DataTable进行临时编辑删除
- github仓库的初步使用
- python redis存入字典序列化存储