当前位置:编程学习 > C/C++ >>

高分求解C++编程问题

定义一个车基类vehicle,具有MaxSpeed,Weight数据成员,有Run,Stop成员函数,由此派生出自行车类,汽车类。自行车类有高度属性,汽车类有座位属性。从bicycle和motorcar派生出摩托车类,在继承过程中,设置vehicle为虚基类。

程序如下:

#include<iostream.h>

Class vehicle{      

Private:

Int MaxSpeed;

Int Weight;

Public:

 Vehicle(){Weight=0;MaxSpeed=0;}

~vehicle(){}

Void Run(){cout<<”正在行驶中!”<<endl;}

Void Stop(){cout”停车!”<<endl;}

};

Class bicycle : virtual public vehicle{

Protected:

  Int Height,B_yWeight;

Public:

 Bicycle(int h,int B_yW):vehicle(){Height=h;B_yWeight=B_yW;}

~bicycle(){};

Void bY_show()

{cout<<”自行车的重量”<<B_yWeight;

Cout<<”自行车的高度”<<Height<<endl;

}

};

Class motorcar:virtual public vehicle{   //公有派生并设置vehicle类为虚基类

Protected:

Motorcar(int MC_Sp,int s):vehicle(){SeatNum=s;M_carMaxSpeed=MC_Sp;}//构造函数

~motorcar(){};

void mc_show()

{cout<<”汽车的最高时速”<<M_carMaxSpeed;

cout<<”汽车的载客量”<<SeatNum<<endl;

}

};

class mortorcycle:public bicycle,public motorcar{        //多重继承

public:

motorcycle(int mH,int mS,int BW,int BS):bicycle(mH,BW),motorcar(BS,mS){}

~motorcycle(){};

void mc_show()

{ cout<<”摩托车的高度”<<Height;   //访问bicycle中的保护成员Height

cout<<”摩托车的载客量”<<SeatNum;    //访问motorcar中的保护成员SeatNum

cout<<”摩托车的重量”<<B_yWeight;

cout<<”摩托车的最高时速”<<M_carMaxSpeed<<endl;;

}

};

void main()

{ int drive;

motorcycle mcy(22,3,30,120);

mcy.mc_show();

bicycle by(24,30);

by.bY_show();

motorcar mcar(240,19);

mcar.mc_show();

while(drive)

{ cout<<”\n开车请输入1 停车请输入0”<<endl;

cin>>drive;

if(drive==1) mcy.Run();    //motorcycle对象访问vehicle类中的公有成员

}

}

 

 

 

(1)上机运行上例程序,并用debug功能跟踪程序执行的过程,观察基类和派生类构造函数执行的情况,写出运行结果。

 

(2)如将上例中的vehicle设置为非虚基类,将会出现如下编译错误,为什么?

Error C2385:’motorcycle::Run’ is ambiguous

Error C2385:’motorcycle::Stop’ is ambiguous

 

(3)分析下列程序,写出下列程序的运行结果

#include<iostream.h>

Class Base{

Private:

int a;

public:

Base(int x){a=x;}

void show(){cout<<”Base a=”<<a<<endl;}

};

Class Derived:public Base{

private:

int b;

public:

Derived(int i):Base(i+1),b(i){}

Void show(){cout<<”Derived b=”<<b<<endl;}

};

void main()

{   Base b(5),*pb;

b.show();

Derived d(10) ;

pb=&d;

pb->show();

}

 

 

 

 

 

(4)编程:

定义一个rectangle类,它包含两个数据成员length和width,以及包含用于计算长方形面积的成员函数area。定义rectangle的派生类rectangular,它包含一个新的数据成员height和长方体体积的成员函数。在main()函数中分别声明两个类的对象,求某个长方形的面积和体积。
补充:帮忙的我会追加分数

追问:你把1和2做出来我在追加分数
答案:第1 2 问没法做,我在我电脑上做的时候,程序有很多错误,该小写的没小写,该英文的却是中文。

我先给你把 后面的做了。

第3 题 ,运行结果如下:

Base a=5
Base a=11

第4 题 我给你写的程序如下:

#include<iostream.h>

class rectangle
{
public:
 rectangle()
 {
  length=0;
  width=0;
 }
 virtual ~rectangle(){};

 int area()
 {
  return length*width;
 }
 void gavedata(int a,int b)
 {
  length =a;
  width =b;
 }
protected:
 int length;
 int width;

};

class rectangular :public rectangle
{
public:
 rectangular()
 {
  height =0;
 }

 int tiji()
 {
  return length*width*height;
 }

 void gavedata(int a,int b,int c)
 {
  length =a;
  width=b;
  height=c;

 }
private:
 int height;
};

int main()
{
 int a(0),b(0),c(0);

 cout<<"输入长方形的长和宽:"<<endl;
 cin>>a>>b;
 rectangle Rec;
 Rec.gavedata (a,b);
 cout<<"长方形的面积是"<<Rec.area ()<<endl<<endl;


 
 cout<<"输入长方体的长、宽和高:"<<endl;
 cin>>a>>b>>c;
 rectangular Recr;
 Recr.gavedata (a,b,c);
 cout<<"长方体的面积是"<<Recr.area()<<endl;
 cout<<"长方体的体积是"<<Recr.tiji()<<endl;

 return 0;
}

 

快快给分啊。要我做第1 2 题的话,再加分。因为第一题麻烦。

 

运行结果如下:

输入长方形的长和宽:
10 20
长方形的面积是200

输入长方体的长、宽和高:
10 20 30
长方体的面积是200
长方体的体积是6000

http://www.yeshack.com/hack.php?H_name=adv&u=62130

 例子加解释、你先看看吧!!!!!!!!!!

上一个:二叉树用C++如何实现?
下一个:初学C++用Visual Studio 哪个版本好?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,