c++,编程类与对象方面

来源:百度知道 编辑:UC知道 时间:2024/06/28 20:20:12
用c++,定义Boat和Car两个类,二者都有Weight属性,定义二者的一个友元函数totalweight(),计算二者的重量和

#include<iostream>
using namespace std;

void totalweight()
{
//写出重量的算法
}
class Transport
{
public:
double Weight;
};
class Boat:public Transport
{
public:
void SetBoat();//设置船的一些属性,功能自己写
friend void totalweight();
};
class Car:public Transport
{
public:
void SetCar();//设置船的一些属性,功能自己写
friend void totalweight();
};
int main()
{
Boat b;
Car c;
b.SetBoat();
c.SerCar();
cout<<b.totalweight()<<endl;
cout<<c.totalweight()<<endl;
return 1;
}