定义一个或多个类,来描述以下需求:
汽车,有多个轮胎,一个发动机,品牌,型号, 价格, 行驶里程。
轮胎,有品牌,尺寸,气压。
发动机,有品牌,型号。
自编代码如下:(不是参考答案,只是我自己写的代码,但是也是可以跑通的)
Engine
.h
#pragma once
#include <string>using namespace std;class Engine
{
public:Engine();Engine(const string engineBrand, float engineVersion);~Engine();string getEngineBrand();float getEngineVersion();string discription();private:string engineBrand;float engineVersion;
};
.cpp
#include "Engine.h"
#include <sstream>Engine::Engine()
{
}Engine::Engine(const string engineBrand, float engineVersion)
{this->engineBrand = engineBrand;this->engineVersion = engineVersion;
}Engine::~Engine()
{
}string Engine::getEngineBrand()
{return engineBrand;
}float Engine::getEngineVersion()
{return engineVersion;
}string Engine::discription()
{stringstream ret;ret << "品牌-" << engineBrand << " 版本-" << engineVersion;return ret.str();
}
Tire
.h
#pragma once
#include <string>using namespace std;class Tire
{
public:Tire();Tire(string tireBrand);~Tire();string getTireBrand();float getPressure();float getSzie();string discription();private:string tireBrand;float pressure = 2.5;float size = 1.0;};
.cpp
#include "Tire.h"
#include <sstream>Tire::Tire()
{
}Tire::Tire(string tireBrand)
{this->tireBrand = tireBrand;
}Tire::~Tire()
{
}string Tire::getTireBrand()
{return tireBrand;
}float Tire::getPressure()
{return pressure;
}float Tire::getSzie()
{return size;
}string Tire::discription()
{stringstream ret;ret << "品牌-" << tireBrand << " 气压-" << pressure << " 尺寸-" << size;return ret.str();
}
Car
.h
#pragma once
#include <string>
#include "Engine.h"
#include "Tire.h"using namespace std;class Car
{
public:Car();Car(string carBrand, float carVersion, int carPrice, int carMiles, string engineBrand, float engineVersion, string tireBrand);~Car();string getCarBrand();float getCarVersion();int getCarPrice();int getCarMiles();Engine getEngine();Tire* getTire(int i);string discription();private:string carBrand;float carVersion;int carPrice;int carMiles=0;Engine engine;Tire tire[4];
};
.cpp
#include "Car.h"
#include <sstream>using namespace std;Car::Car()
{
}Car::Car(string carBrand, float carVersion, int carPrice, int carMiles, string engineBrand, float engineVersion, string tireBrand) :engine(engineBrand, engineVersion), tire{ tireBrand,tireBrand,tireBrand,tireBrand }
{this->carBrand = carBrand;this->carVersion = carVersion;this->carPrice = carPrice;this->carMiles = carMiles;
}Car::~Car()
{
}string Car::getCarBrand()
{return carBrand;
}float Car::getCarVersion()
{return carVersion;
}int Car::getCarPrice()
{return carPrice;
}int Car::getCarMiles()
{return carMiles;
}Engine Car::getEngine()
{return engine;
}Tire* Car::getTire(int i)
{if (i >= 1 || i <= 4) {return &tire[4];}return NULL;
}string Car::discription()
{stringstream ret;ret << "品牌-" << carBrand << " 型号-" << carVersion << " 价格-" << carPrice << " 里程数-" << carMiles << "\t\t引擎-" << engine.discription() << "\t\t轮胎-" << tire->discription();return ret.str();
}
main
.h
#include "Car.h"
#include <iostream>using namespace std;int main(void) {{Car car("宝马", 7.0, 950000, 56000, "宝马", 3.5, "米其林");cout << car.discription() << endl;}system("pause");return 0;
}