C++语言程序设计实验报告
}
结果:
提高题一
实验任务:
编写一个程序,设计一个汽车类,包含的数据成员有:车轮个数和车重。小车类car是它的私有派生类,其中包含载人数。卡车类是汽车类的私有派生类,其中包含载人数和载重量。每个类都有相关数据的输出方法。
分析:
先定义qiche类,给出数据成员车轮个数和车重,再派生出小车类和卡车类,在小车类中添加新的成员载人数,在卡车类中添加新的成员载人数和载重量,最后都用各自的Get函数输出结果即可
代码:
#include<iostream>
using namespace std;
class Qiche
{
public:
Qiche(int chelun=0,int chezhong=0)
{
x=chelun;
y=chezhong;
}
int GetX(){return x;}
int GetY(){return y;}
~Qiche(){}
private:
int x,y;
};
class Car:private Qiche
{
public: