qfedu-cpp-level/day4/homework/h4.cpp

46 lines
898 B
C++
Raw Normal View History

2023-07-27 21:04:06 +08:00
// 设计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;
}