C++考试,急求编程题答案,要完整程序!
#pragma once
#include <iostream>
using namespace std;
class Vehicle
{
public:
Vehicle(void);
~Vehicle(void);
int MaxSpeed;
int Weight;
void Run();
void Stop();
};
#include "Vehicle.h"
Vehicle::Vehicle(void)
{
}
Vehicle::~Vehicle(void)
{
}
void Vehicle::Run()
{
cout<<"自行车正在以"<<MaxSpeed<<"的速度运动"<<endl;
}
void Vehicle::Stop()
{
cout<<"停止运动"<<endl;
}
//自行车类
#pragma once
#include "vehicle.h"
class bicycle :
public Vehicle
{
public:
bicycle(void);
~bicycle(void);
int height;
void SetMaxSpeed(int n);
};
#include "bicycle.h"
bicycle::bicycle(void)
{
}
bicycle::~bicycle(void)
{
}
void bicycle::SetMaxSpeed(int n)
{
MaxSpeed = n;
}
#include "bicycle.h"
bicycle cBicycle;
void main()
{
cBicycle.SetMaxSpeed(5);
cBicycle.Run();
cBicycle.Stop();
system("pause");
}
class Vehicle
{
public:
Vehicle();
~Vehicle();
proctected:
int MaxSpeed;
int Weight;
bool Run();
bool Stop();
}
class bicycle : public Vehicle
{
public:
bicycle();
~bicycle();
proctected:
int height;
bool SetMaxSpeed(int );
}
bool bicycle::SetMaxSpeed(int speed)
{
MaxSpeed = speed;
return true;
}
int main()
{
bicycle cBicycle;
return 0;
}
顺序为:先父类构造,再子类构造; 先子类析构,再父类析构
运行不出来呀
运行不出来呀
//C++ CODE
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle () {
cout << "Vehicle construct" << endl;
}
virtual ~Vehicle () {
cout << "Vehicle deConstruct" << endl;
}
void Run () {}
void Stop () {}
protected:
int m_maxSpeed;
int m_weight;
};
class Bicycle : public Vehicle {
public:
Bicycle () {
cout << "Bicycle construct" << endl;
}
virtual ~Bicycle () {
cout << "Bicycle deConstruct" << endl;
}
void setMaxSpeed (int n);
private:
int m_height;
};
void Bicycle::setMaxSpeed (int n) {
m_maxSpeed = n;
}
int main (int argc, char** argv) {
#define HEAP
//--Way 1: in the stack
#ifndef HEAP
Bicycle bicyc();
//--Way 2: in the heap
#ifdef HEAP
Bicycle* bicyc = new Bicycle();
bicyc->setMaxSpeed(100);
delete bicyc;
bicyc = NULL;
#endif
return true;
}