第十四周实验内容1

更新时间:2024-04-13 08:06:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

第十四周实验内容:继承与派生

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

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 #include #include \

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<

/*实现Vehicle类成员函数setColor的定义*/

void Vehicle::setFuelLevel( double amount) {

//假设满箱为20加仑

if (amount > 0.0 && amount <=20.0) fuelLevel=amount; else fuelLevel=5.0; }

// caller is responsible for deleting memory

const 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 memory

const char *Vehicle::getClassName() const { return className;}

************************************************************** //taxi.h

#ifndef TAXI_H #define TAXI_H #include using namespace std; #include \

class Taxi: public Vehicle {

public:

Taxi(double);

/* 为成员函数hasCustomers写声明*/ /* 为成员函数setCustomers写声明*/ void printTaxi() const; private:

bool customers; };

#endif

*************************************************************************** //taxi.cpp

#include \

Taxi::Taxi(double f)

:Vehicle ( 4, 6, \

{ customers = false; setClassName(\}

/* 实现Taxi类的成员函数setCustomers*/

/* 实现Taxi类的成员函数hasCustomers*/

void Taxi::printTaxi() const { cout<

*********************************************************************** //truck.h

#ifndef TRUCK_H #define TRUCK_H

#include using namespace std;

#include \

class Truck: public Vehicle {

public: Truck (double); bool hasCargo() const; void setCargo( bool); void printTruck() const;

private: bool cargo; };

#endif

************************************************************************* //truck.cpp

#include \

/* 实现Truck类构造函数*/

bool Truck::hasCargo() const { return cargo; }

void Truck::setCargo(bool c) { cargo = c;}

void Truck::printTruck() const { cout<

************************************************************************* //driver for vehicle

#include using namespace std;

#include \#include \#include \

int main()

{ }

Vehicle car(2, 6, \Taxi cab(3.3); Truck mack(7.54);

/* 编写代码使mack载货*/

/* 编写代码打印所有对象,包括Vehicle对象、Taxi对象、Truck对象*/ return 0;

修改后代码:

// vehicle.h

#ifndef VEHICLE_H #define VEHICLE_H

#include

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 #include

#include \

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(\ setColor(color); fuelLevel=initialFuel; }

Vehicle::~Vehicle() {

delete [] className; }

void Vehicle::printVehicle() {

cout<

<<\ <<\\

<<\ <<\

<<\}

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; else

of

cylinders:

fuelLevel=5.0; }

// caller is responsible for deleting memory

char *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 memory char

*Vehicle::getClassName()

const

{

return

className;} #include using namespace std; #include \

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::Taxi(int a,int b,char *c,int d,double f) :Vehicle ( a,b,c,d,f) {

customers = false; setClassName(\}

函数数函

bool Taxi::hasCustomers() const{return customers;}/* 实现Taxi类的成员函数setCustomers*/

void Taxi::setCustomers(bool c) {c=customers;}/* 实现Taxi类的成员函数hasCustomers*/

void Taxi::printTaxi() const {

cout<

cout<<\ else

cout<<\} #endif

#ifndef TRUCK_H #define TRUCK_H

#include using namespace std; #include \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::Truck (int a,int b,char *c,int d,double f):Vehicle (a,b,c,d,f) {

cargo=false;

setClassName(\};/* 实现Truck类构造函数*/

bool Truck::hasCargo() const { return cargo; }

void Truck::setCargo(bool c) { cargo = c;}

void Truck::printTruck() const {

cout<

<

cout<<\ else

cout<<\ }

//driver for vehicle #include using namespace std;

#include \#include \#include \ int main() {

Vehicle car(2, 6, \ Taxi cab(4,6,\ Truck mack(2,16,\mack.setCargo(1);

car.printVehicle(); cab.printTaxi(); mack.printTruck();

return 0; }

【运行结果截图】

2. 练习题1 【程序代码】

#include 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 aValue, int bValue ) { a=aValue; 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<<\

cout<<\ }

【思考题及问题】

修改程序,增加计算正方形面积的功能。

提示:在Rectangle类的基础上创建派生类Square,代码如下: class Square: public Rectangle {

public:

Square(float len); ~Square(){} };

Square::Square(float len):Rectangle(len,len) {} 在main函数中增加如下语句: Square square(5);

cout<<\

3. 练习题2 【程序代码】

1) #include 2) using namespace std;

3) class Point //基类Point类的定义 4) {

5) public: //公有函数成员

6) void InitP(float xx=0, float yy=0) {X=xx;Y=yy;}

7) void Move(float xOff, float yOff) {X+=xOff;Y+=yOff;} 8) float GetX() {return X;} 9) float GetY() {return Y;}

10) private: //私有数据成员 11) float X,Y; 12) };

13) class Rectangle: public Point //派生类声明部分 14) {

15) public: //新增公有函数成员 16) void InitR(float x, float y, float w, float h) 17) { X=x; Y=y; //访问基类私有数据成员 18) W=w;H=h; 19) }

20) float GetH() {return H;} 21) float GetW() {return W;}

22) private: //新增私有数据成员

23) 24) 25) 26) 27) 28) 29) 30) float W,H; };

void main() {

Rectangle rect; cout<

17行 Rectangle类公有继承Point基类,其成员函数无法访问基类下的私有成员数据 该程序段应改为InitR(x,y);

28行 Rectangle类公有继承Point基类,其对象无法访问基类下的私有成员数据

该程序段应改为 rect.InitR(4,5,6,7); 通过成员函数进行访问 10行改为Protected:

该程序段应改为InitR(x,y);

28行 Rectangle类公有继承Point基类,其对象无法访问基类下的保护成员数据

该程序段应改为 rect.InitR(4,5,6,7); 通过成员函数进行访问 10行改为 Public: 可编译

13行改为Protected:

17行 Rectangle类公有继承Point基类,其成员函数无法访问基类下的私有成员数据 该程序段应改为InitR(x,y);

28行 Rectangle类公有继承Point基类,其对象无法访问基类下的私有成员数据

该程序段应改为 rect.InitR(4,5,6,7); 通过成员函数进行访问 13行改为private:

17行 Rectangle类公有继承Point基类,其成员函数无法访问基类下的私有成员数据 该程序段应改为InitR(x,y);

28行 Rectangle类公有继承Point基类,其对象无法访问基类下的私有成员数据

该程序段应改为 rect.InitR(4,5,6,7); 通过成员函数进行访问

4. 自测练习1

阅读下面程序,写出运行结果并上机调试运行。 #include using namespace std; class A {public:

A(){cout<<”constructing A”<

class B:public A {public:

B(){cout<<”constructing B”<

~B(){cout<<”desstructing B”<

class C:public B {public:

C(){cout<<”constructing C”<

int main() {C c1; return 0; }

constructing A、 constructing B constructing C desstructing C desstructing B desstructing A

5.自测练习二

阅读下面程序,写出运行结果并上机调试运行。 #include using namespace std;

class B1 //基类B1,构造函数有参数

{ public: B1(int i) {val1=i;cout<<\ ~B1() {cout<<\private: int val1; };

class B2 //基类B2,构造函数有参数

{ public: B2(int j) {val2=j;cout<<\ ~B2() {cout<<\private: int val2; };

class B3 //基类B3,构造函数无参数

{ public: B3() {cout<<\ ~B3() {cout<<\};

class C: public B2, public B1, public B3 { public:

C(int a, int b): B1(a),B2(b){}

};

void main() { C obj(1,2); }

constructing B2 2 constructing B1 1 constructing B3 deconstructing B3 deconstructing B1 1 deconstructing B2 2

6.自测练习三

阅读下面程序,写出运行结果并上机调试运行。 #include using namespace std; class A

{ public: int nv;

A(int x) {nv = x; cout << \:\ void fun(){cout << \};

class B1:virtual public A {

public: int nv1;

B1(int x,int y):A(y){nv1= x; cout << \:\};

class B2:virtual public A {

public: int nv2;

B2(int x,int y):A(y) {nv2 = x; cout << \:\

};

class C:public B1,public B2 {

public:

C(int x, int y, int z): B1(y,x),B2(z,x),A(x) {cout << \:\};

void main() {

C c1(1,2,3);

c1.nv = 2; c1.fun(); }

constructor of A:1 constructor of B1:2 constructor of B2:3 constructor of C:1

Member of A:2 7.自测练习四

阅读下面程序,写出运行结果并上机调试运行。 #include using namespace std;

class Base1{public: Base1(){ cout <<\class Base2{public: Base2(){ cout <<\class Base3{public: Base3(){ cout <<\class Base4{public: Base4(){ cout <<\

class Derived :public Base1, virtual public Base2,public Base3, virtual public Base4 {

public:

Derived() :Base4(), Base3(), Base2(), Base1(){} };

void main() { Derived aa; }

Base 2 Base 4 Base 1 Base 3

8.自测练习五

阅读下面程序,找出语法错误并改正之;写出程序运行结果并上机调试运行验证之。 #include using namespace std; class A { public: int n; };

class B:public A{};

class C:public A{};

class D:public B,public C { int getn(){return n;} };

void main() { D d; d.n=10; cout<

改正:

#include using namespace std; class A { public: int n; };

class B:virtual public A{}; class C:virtual public A{}; class D:public B,public C { int getn(){return n;} }; void main() { } D d; d.n=10;

cout<

结果: 10

本文来源:https://www.bwwdw.com/article/ansp.html

Top