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

46 lines
898 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 设计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;
}