第十四周实验内容1

  • 格式:doc
  • 大小:202.50 KB
  • 文档页数:26

第十四周实验内容:继承与派生1.编程【问题描述】:开发一个名为Vehicle的类的层次体系。

创建两个类Taxi和Truck,均以公有模式从类Vehicle中继承而来。

Taxi类中应包含一个数据成员说明其是否载客。

Truck类应包含一个数据成员说明其是否载货。

添加必要的函数来控制和访问类的数据。

编写一段测试程序,将Truck对象和Taxi对象打印到屏幕。

【实例输出】【程序模板】共7个文件(Vehicle.h、Vehicle.cpp、Taxi.h、Taxi.cpp、Truck.h、Truck.cpp、driver.cpp)*************************************************************// vehicle.h#ifndef VEHICLE_H#define VEHICLE_H#include <iostream>using namespace std;class Vehicle{public:Vehicle( const int doors, const int cylinders,char *color, double initialFuel,const int transmission );~Vehicle();void setColor ( char *color);void setFuelLevel( double amount);const char *getColor() const;double getFuelLevel() const;const int getTransmissionType() const;const int getNumberOfDoors() const;const int getNumberOfCylinders() const;void setClassName( const char*);const char *getClassName() const;void printVehicle();private:const int numberOfDoors; //门的个数const int numberOfCylinders; // 汽缸的个数char *vehicleColor; // 交通工具的颜色double fuelLevel; //const int transmissionType;char *className;};#endif***********************************************************//vehicle.cpp#include <iostream>#include <cstring>#include "vehicle.h"using namespace std;Vehicle::Vehicle(const int doors, const int cylinders,char * color, double initialFuel,const int transmission):numberOfDoors(doors),numberOfCylinders(cylinders), transmissionType(transmission) /* 实现Vehicle类的构造函数定义*/Vehicle::~Vehicle(){delete [] vehicleColor;delete [] className;}void Vehicle::printVehicle(){cout<<className <<"\n"<<"\tNumber of doors: "<<numberOfDoors<<"\n\tNumber of cylinders: "<<numberOfCylinders<<"\n\tTransmission Type:"<<transmissionType<<"\n\tColor:"<<vehicleColor<<"\n\tFuel Level:" <<fuelLevel<<endl;}/*实现Vehicle类成员函数setColor的定义*/void Vehicle::setFuelLevel( double amount){//假设满箱为20加仑if (amount > 0.0 && amount <=20.0)fuelLevel=amount;elsefuelLevel=5.0;}// caller is responsible for deleting memoryconst char *Vehicle::getColor() const{return vehicleColor;}double Vehicle::getFuelLevel() const{return fuelLevel;}const int Vehicle::getTransmissionType() const{return transmissionType;}const int Vehicle::getNumberOfDoors() const{return numberOfDoors;}const int Vehicle::getNumberOfCylinders() const{return numberOfCylinders;}void Vehicle::setClassName(const char *newName){if (className!=0)delete [] className;className = new char [ strlen( newName) + 1];strcpy (className, newName);}// caller is responsible for deleting memoryconst char *Vehicle::getClassName() const { return className;}************************************************************** //taxi.h#ifndef TAXI_H#define TAXI_H#include <iostream>using namespace std;#include "vehicle.h"class Taxi: public Vehicle{public:Taxi(double);/* 为成员函数hasCustomers写声明*//* 为成员函数setCustomers写声明*/void printTaxi() const;private:bool customers;};#endif*************************************************************************** //taxi.cpp#include "taxi.h"Taxi::Taxi(double f):Vehicle ( 4, 6, "yellow", f, 5){customers = false;setClassName("Taxi");}/* 实现Taxi类的成员函数setCustomers*//* 实现Taxi类的成员函数hasCustomers*/void Taxi::printTaxi() const{cout<<getClassName()<<"\n"<<"\tNumber of doors:"<<getNumberOfDoors()<<"\n\tNumber of cylinders: "<<getNumberOfCylinders()<<"\n\tTransmission Type: "<<getTransmissionType()<<"\n\tColor: “<<getColor()<<"\n\tFuel level: "<<getFuelLevel()<<"\n";if (/* Taxi有乘客*/)cout<<"\t The Taxi has passengers.\n";elsecout<<"\t The Taxi has no passengers.\n";}*********************************************************************** //truck.h#ifndef TRUCK_H#define TRUCK_H#include <iostream>using namespace std;#include "vehicle.h"class Truck: public Vehicle{public:Truck (double);bool hasCargo() const;void setCargo( bool);void printTruck() const;private:bool cargo;};#endif************************************************************************* //truck.cpp#include "truck.h"/* 实现Truck类构造函数*/bool Truck::hasCargo() const { return cargo; }void Truck::setCargo(bool c) { cargo = c;}void Truck::printTruck() const{cout<<getClassName()<<"\n"<<"\tNumber of doors: "<<getNumberOfDoors()<<"\n\tNumber of cylinders: "<<getNumberOfCylinders()<<"\n\tTransmission type: "<<getTransmissionType()<<"\n\tColor: "<<getColor()<<"\n\tFuel level: "<<getFuelLevel()<<"\n";if (cargo)cout<<"\tThe truck is carrying cargo.\n";elsecout<<"\tThe truck is not carrying cargo.\n";}************************************************************************* //driver for vehicle#include <iostream>using namespace std;#include "vehicle.h"#include "taxi.h"#include "truck.h"int main(){Vehicle car(2, 6, "blue", 14.6, 3);Taxi cab(3.3);Truck mack(7.54);/* 编写代码使mack载货*//* 编写代码打印所有对象,包括Vehicle对象、Taxi对象、Truck对象*/ return 0;}修改后代码:// vehicle.h#ifndef VEHICLE_H#define VEHICLE_H#include <iostream>using namespace std;class Vehicle{public:Vehicle( const int doors, const int cylinders, char *color, const int transmission,double initialFuel);~Vehicle();void setColor ( char *color);void setFuelLevel( double amount);char *getColor() const;double getFuelLevel() const;int getTransmissionType() const;int getNumberOfDoors() const;int getNumberOfCylinders() const;void setClassName( const char*);char *getClassName() const;void printVehicle();private:const int numberOfDoors; //门的个数const int numberOfCylinders; // 汽缸的个数char *vehicleColor; // 交通工具的颜色double fuelLevel; //const int transmissionType;char *className;};#endif//vehicle.cpp#include <iostream>#include <cstring>#include "vehicle.h"using namespace std;Vehicle::Vehicle(const int doors, const int cylinders,char * color, const int transmission,double initialFuel):numberOfDoors(doors), numberOfCylinders(cylinders), transmissionType(transmission) /* 实现Vehicle类的构造函数定义*/{setClassName("Vehicle");setColor(color);fuelLevel=initialFuel;}Vehicle::~Vehicle(){delete [] className;}void Vehicle::printVehicle(){cout<<className <<"\n"<<"\tNumber of doors: "<<numberOfDoors<<"\n\tNumber of cylinders: "<<numberOfCylinders<<"\n\tTransmission Type:"<<transmissionType<<"\n\tColor:"<<vehicleColor<<"\n\tFuel Level:" <<fuelLevel<<endl;}void Vehicle::setColor(char *cl)/*实现Vehicle类成员函数setColor的定义*/{vehicleColor=new char[strlen(cl)+1];if(vehicleColor!=0)strcpy(vehicleColor,cl);}void Vehicle::setFuelLevel( double amount){//假设满箱为20加仑if (amount > 0.0 && amount <=20.0)fuelLevel=amount;elsefuelLevel=5.0;}// caller is responsible for deleting memorychar *Vehicle::getColor() const{return vehicleColor;}double Vehicle::getFuelLevel() const{return fuelLevel;}int Vehicle::getTransmissionType() const {return transmissionType;}int Vehicle::getNumberOfDoors() const{return numberOfDoors;}int Vehicle::getNumberOfCylinders() const{return numberOfCylinders;}void Vehicle::setClassName(const char *newName) {className = new char [ strlen( newName) + 1];if (className!=0)strcpy (className, newName);}// caller is responsible for deleting memorychar *Vehicle::getClassName() const { return className;}#include <iostream>using namespace std;#include "vehicle.h"class Taxi: public Vehicle{public:Taxi(int a,int b,char *c,int d,double f);bool hasCustomers() const;/* 为成员函数hasCustomers写声明*/void setCustomers(bool) ;/* 为成员函数setCustomers写声明*/virtual void printTaxi() const;private:bool customers;};#ifndef TAXI_H#define TAXI_H#include "taxi.h"Taxi::Taxi(int a,int b,char *c,int d,double f):Vehicle ( a,b,c,d,f){customers = false;setClassName("Taxi");}bool Taxi::hasCustomers() const{return customers;}/* 实现Taxi类的成员函数setCustomers*/void Taxi::setCustomers(bool c) {c=customers;}/* 实现Taxi类的成员函数hasCustomers*/void Taxi::printTaxi() const{cout<<getClassName()<<"\n"<<"\tNumber of doors:"<<getNumberOfDoors()<<"\n\tNumber of cylinders: "<<getNumberOfCylinders()<<"\n\tTransmission Type: "<<getTransmissionType()<<"\n\tColor: "<<getColor()<<"\n\tFuel level: "<<getFuelLevel()<<"\n";if (customers)cout<<"\t The Taxi has passengers.\n";elsecout<<"\t The Taxi has no passengers.\n"; }#endif#ifndef TRUCK_H#define TRUCK_H#include <iostream>using namespace std;#include "vehicle.h"class Truck: public Vehicle{public:Truck (int a,int b,char *c,int d,double f);bool hasCargo() const;void setCargo( bool);virtual void printTruck() const;private:bool cargo;};#endif#include "truck.h"Truck::Truck (int a,int b,char *c,int d,double f):Vehicle (a,b,c,d,f){cargo=false;setClassName("Truck");};/* 实现Truck类构造函数*/bool Truck::hasCargo() const { return cargo; }void Truck::setCargo(bool c) { cargo = c;}void Truck::printTruck() const{cout<<getClassName()<<"\n"<<"\tNumber of doors: "<<getNumberOfDoors()<<"\n\tNumber of cylinders: "<<getNumberOfCylinders()<<"\n\tTransmission type: "<<getTransmissionType()<<"\n\tColor: "<<getColor()<<"\n\tFuel level: "<<getFuelLevel()<<"\n";if (cargo)cout<<"\tThe truck is carrying cargo.\n";elsecout<<"\tThe truck is not carrying cargo.\n";}//driver for vehicle#include <iostream>using namespace std;#include "vehicle.cpp"#include "taxi.cpp"#include "truck.cpp"int main(){Vehicle car(2, 6, "blue",3,14.6);Taxi cab(4,6,"yellow",5,3.3);Truck mack(2,16,"black",8,7.54);mack.setCargo(1);car.printVehicle();cab.printTaxi();mack.printTruck();return 0;}【运行结果截图】2. 练习题1【程序代码】#include<iostream>using namespace std;class Shape //基类Shape的定义{public:Shape(){}~Shape(){}double getArea()const;};class Circle: public Shape // 派生类Circle定义{public:Circle (double = 0.0 );~Circle (){};double getArea() const; // 返回面积private:double radius; // 圆半径};class Rectangle : public Shape { // 派生类Rectangle定义public:Rectangle( int = 0, int = 0); // 构造函数~Rectangle(){}double getArea() const; // 返回面积private:int a,b; // 矩形的长和宽};double Shape::getArea() const{return 0.0;} // Shape类getArea函数的定义Circle::Circle( double radiusValue ){radius= radiusValue ;} //Circle类构造函数double Circle::getArea() const{return 3.14159 * radius * radius;} //Circle类getArea函数定义Rectangle::Rectangle( int aV alue, int bValue ){ a=aV alue; b=bValue;} // Rectangle类构造函数double Rectangle::getArea() const{return a * b;} // Rectangle类getArea函数定义void main(){ Circle circle ( 3.5 ); // 创建Circle类对象Rectangle rectangle (5, 10 ); // 创建Rectangle类对象cout<<"The area of the circle is "<<circle.getArea()<<endl;cout<<"The area of the rectangle is "<<rectangle.getArea()<<endl; }【思考题及问题】修改程序,增加计算正方形面积的功能。