#include
using namespace std;
class Transport
{
protected:
int speed;
float load;
//构造函数
};
class Vehicle:public Transport
{
protected:
int wheels;
float weight;
public:
Vehicle()
{
speed=80;
load=5;
wheels=4;
weight=1000;
}
void Show()
{
cout<<"Type:Vehicle"<<endl;
cout<<"Speed:"<<speed<<"km/h"<<endl;
cout<<"Load:"<<load<<"persons"<<endl;
cout<<"Wheel:"<<wheels<<endl;
cout<<"Weight:"<<weight<<"kg"<<endl;
}
};
class Airplane:public Transport
{
protected:
int type;
int NE;
public:
Airplane()
{
speed=200;
load=106;
type='A';
NE=3;
}
void Show()
{
cout<<"Type:Airplane"<<endl;
cout<<"Speed:"<<speed<<"km/h"<<endl;
cout<<"Load:"<<load<<"persons"<<endl;
cout<<"type:"<<type<<endl;
cout<<"NE:"<<NE<<endl;
}
};
int main()
{
Vehicle s;
s.Show();
Airplane t;
没啥大毛病啊,帮你写了构造函数
#include <iostream>
using namespace std;
class Transport
{
protected:
int speed;
float load;
//构造函数
Transport(){
speed = 50;
load = 2;
}
Transport(int speed, float load) {
this->speed = speed;
this->load = load;
}
};
class Vehicle :public Transport
{
protected:
int wheels;
float weight;
public:
Vehicle()
{
speed = 80;
load = 5;
wheels = 4;
weight = 1000;
}
void Show()
{
cout << "Type: Vehicle" << endl;
cout << "Speed: " << speed << "km/h" << endl;
cout << "Load: " << load << "persons" << endl;
cout << "Wheel: " << wheels << endl;
cout << "Weight: " << weight << "kg" << endl;
}
};
class Airplane :public Transport
{
protected:
int type;
int NE;
public:
Airplane()
{
speed = 200;
load = 106;
type = 'A';
NE = 3;
}
void Show()
{
cout << "Type: Airplane" << endl;
cout << "Speed: " << speed << "km/h" << endl;
cout << "Load: " << load << "persons" << endl;
cout << "type: " << type << endl;
cout << "NE: " << NE << endl;
}
};
int main()
{
Vehicle s;
s.Show();
Airplane t;
t.Show();
}