定义一个汽车类car,成员如下:数据成员(颜色,型号,座位数),函数成员有输出汽车信息show()。定义该类的对象,实现对以上函数的调用。
#include <iostream>
using namespace std;
typedef enum
{
BLACK = 0,
WHITE,
RED,
YELLOW
} Color;
class Car
{
public:
Car() {}
Car(Color color, string& model, int seats) :
color_(color), model_(model), seats_(seats) {}
Car(Color color, const char * model, int seats) :
color_(color), model_(model), seats_(seats) {}
void show()
{
static const char *colorStr[] = {"Black", "White", "Red", "Yellow"};
cout << "Color: " << colorStr[color_] << ", Model: " << model_ << ", Seats: " << seats_ << endl;
}
private:
Color color_;
string model_;
int seats_;
};
int main(int argc, char *argv[])
{
Car car(Color::BLACK, "Tesla Model 3", 5);
car.show();
return 0;
}