#include <string>
#include <iostream>
using namespace std;
class Car{
public:
Car(int Weight,int Speed){
weight=Weight;
speed=Speed;
}
void setWeight(int Weight){
weight=Weight;
}
void setSpeed(int Speed){
speed=Speed;
}
int getSpeed(){
return speed;
}
int getWeight(){
return weight;
}
private:
int weight;
int speed;
};
class SportCar:public Car{
public:
SportCar(int Weight,int Speed,string Color):Car(Weight,Speed){
color=Color;
}
void setColor(string Color){
color=Color;
}
string getColor(){
return color;
}
private:
string color;
};
int main(){
Car car(100,100);
SportCar sportCar(100,200,"black");
cout<<"car's weight is "<<car.getWeight()<<endl;
cout<<"car's speed is "<<car.getSpeed()<<endl;
cout<<"sportcar's weight is "<<sportCar.getWeight()<<endl;
cout<<"sportcar's speed is"<<sportCar.getSpeed()<<endl;
cout<<"sportCar's color is"<<sportCar.getColor()<<endl;
system("pause");
}
c++ 继承示例
最新推荐文章于 2022-08-25 19:28:56 发布