46 lines
898 B
C++
46 lines
898 B
C++
// 设计Car类,包含color(颜色)、weight(重量) 两个属性和一个带color和weight两个参数的构造函数、以及driver() 驾车、start() 启动、stop() 停车的函数。
|
||
// 【提示】设计的类,可以在main() 自行测试
|
||
#include <iostream>
|
||
#include <cstring>
|
||
#include <cstdlib>
|
||
|
||
using namespace std;
|
||
|
||
class Car
|
||
{
|
||
public:
|
||
Car(const string &color, int weight) : color(color), weight(weight) {}
|
||
|
||
public:
|
||
void driver()
|
||
{
|
||
cout << "驾车 " << this->weight << " 车重" << endl;
|
||
}
|
||
void start()
|
||
{
|
||
cout << "启动 " << this->color << " 颜色的车" << endl;
|
||
}
|
||
void stop()
|
||
{
|
||
cout << "停车" << endl;
|
||
}
|
||
|
||
private:
|
||
string color; // 颜色
|
||
int weight; // 重量
|
||
};
|
||
|
||
void test()
|
||
{
|
||
Car car("blue", 1200);
|
||
car.start();
|
||
car.driver();
|
||
car.stop();
|
||
}
|
||
|
||
int main()
|
||
{
|
||
test();
|
||
return 0;
|
||
}
|