c++构造函数

来源:互联网 发布:html5动画制作软件 编辑:程序博客网 时间:2024/06/03 16:56
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点。//// ConsoleApplication6.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include<iostream>#include<string>#define full_tank 85class Car{public:std::string color;std::string engine;unsigned int gas;unsigned int wheel; void setColor (std::string col);void setengine(std::string eng);void setwheel(unsigned int whe);void fill_tank(float liter);int runing(void);void warning(void);};void Car::Car(void){color = "while";engine = "v8";wheel = 4;gas = full_tank;    //构造函数必须要与类名一致,就是对类的属性进行初始化。}void Car::setColor(std::string col){color = col;}void Car::setengine(std::string eng){engine = eng;}void Car::setwheel(unsigned int whe){wheel = whe;}void Car::fill_tank(float liter){gas+ = liter;}int Car::runing(void){char = i;std::cout << "我正在以1200000的时速往前移动。。。。你妹。。。\n";gas--;std::cout << "当前还剩" << 100 * gas / full_tank << "%" << "油量";if (gas < 10){std::cout << "请问要不要再来一瓶?(Y/N)";std::cin >> i;if ('Y' == i || 'y' == i){fill_tank(full_tank);}if (0 == gas){std::cout << "抛瞄了。。。。";return 1;}}return 0;}void Car::warning(void){std::cout << "WARNING!!" << "还剩" << 100 * gas / full_tank << "%" << "油量";}int main(){Car mycar;while (!mycar.runing()){;}return 0;}

原创粉丝点击