[英]C++ OOP with class design
我的任务是计算直升机的必要燃油摄入量。 直升机可以执行以下四个操作:
用户通过输入参数选择直升机的动作。 我需要使用OOP方法创建该系统。 到目前为止,我已经编写了如下代码,但是我认为它没有OOP属性。
关于如何使该系统面向对象的任何想法? 这是代码:
#include <iostream>
#include <stdio.h>
using namespace std;
class helikopter {
float result;
public:
void holding_flight(float time, float fuel);
void raising(float end, float start, float pace, float fuel);
void landing(float end, float start, float pace, float fuel);
void straight(float mesafe, float hiz, float yakit);
void calculate();
//helikopter();
};
/*helikopter::helikopter(void){
result=0;
}*/
void helikopter::holding_flight(float time, float fuel) {
result += time * fuel * 60;
}
void helikopter::raising(float end, float start, float pace, float fuel){
result += (end - start)/pace * fuel;
}
void helikopter::landing(float end, float start, float pace, float fuel) {
result += (start - end)/pace * fuel;
}
void helikopter::straight(float mesafe, float hiz, float yakit) {
result += mesafe/hiz * yakit;
}
void helikopter::calculate() {
cout <<"Total required fuel : "<< result << "kg/second"<< endl;
}
int main(void) {
float a, b;
float c, d, e, f;
char op;
while(op != 'x') {
helikopter h;
cout << "Enter the move : " << endl;
cout << "1 ---> Holding Flight" << endl;
cout << "2 ---> Raising" << endl;
cout << "3 ---> Landing " << endl;
cout << "4 ---> Straight Flight " << endl;
cout << "5 ---> Calculate fuel" << endl;
cout << "x ---> Exit " << endl;
op = std::getchar();
if(op == '1') {
cout << "Enter time : ";
cin >> a; cout << "Enter fuel rate: ";
cin >> b; h.holding_flight(a, b); }
if(op == '2') {
cout << "Enter ending altitude: ";
cin >> c;
cout << "Enter starting altitude: ";
cin >> d;
cout << "Enter raising pace: ";
cin >> e;
cout << "Enter fuel rate: ";
cin >> f;
h.raising(c, d, e, f); }
if(op == '3') {
cout << "Enter ending altitude: ";
cin >> c;
cout << "Enter starting altitude: ";
cin >> d;
cout << "Enter landing pace: ";
cin >> e;
cout << "Enter fuel rate: ";
cin >> f; h.landing(c,d,e,f); }
if(op == '4') {
cout << "Enter ending altitude: ";
cin >> a;
cout << "Enter starting altitude: ";
cin >> b;
cout << "Enter fuel rate: ";
cin >> c;
h.straight(a, b, c); }
if(op == '5') {
h.calculate(); }
if(op == 'x') {
cout << "System will exit..." << endl; }
else {
//if(op==(1|2|3|4|5)){}
//else cout << "Wrong selection." << endl;
}
}
return 0;
}
编辑:尽管似乎没有必要,我想使用大多数OOP原则。
解:
class FlyingMode {
protected:
float time, fuel_rate, start, end, pace, distance;
public:
FlyingMode(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0){
time = a;
fuel_rate = b;
start = c;
end = d;
pace = e;
distance = f;
}
virtual int calcFuel(){
return 0;
}
};
class Holding: public FlyingMode{
public:
Holding(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel(){
return (time * fuel_rate * 60);
}
};
class Raising: public FlyingMode{
public:
Raising(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel (){
if(start < end && pace != 0 ){
return ((end - start)/pace * fuel_rate);
}else{
return 0;
}
}
};
class Landing: public FlyingMode{
public:
Landing(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel (){
if(start > end && pace != 0 ){
return ((start - end)/pace * fuel_rate);
}else{
return 0;
}
}
};
class Straight: public FlyingMode{
public:
Straight(float a=0, float b=0, float c=0,
float d=0, float e=0, float f=0):FlyingMode(a, b, c, d, e, f) { }
int calcFuel (){
if(distance != 0 || pace != 0 ){
return (distance/pace * fuel_rate);
}else{
return 0;
}
}
};
在第一个视图上,您在示例中做了很多非OOP的事情:
1)在主要方面,您要求的数据仅与以后创建的对象有关。 该类应该知道它需要哪个参数,而不是外面没有任何不相关的方法。
2)我根本看不到任何真实的物体! 我看到的是在前面使用“类”的C代码。 您创建的“对象”没有任何东西使其成为对象。
因此,创建一个具有语音名称的基类,例如:FlyingMode和一个! 该方法可能称为FuelConsuming。 在基类中将此方法设置为虚拟,然后将其删除!
从该基类派生类并覆盖FuelConsuming。 类的名称可以是:Landing,Rising ....等。
使您的类的构造函数与gui本身进行交互。 通常,您应该使用序列化程序,但首先要在构造函数中使用cin / cout。 对于创建模式类的实例,您应该阅读有关“工厂”的信息。
在您的小示例中,这只是做OOP的很小的提示!
编辑:您应该看看您的代码。 为什么需要5倍完全相同的代码? 看起来像是设计问题! 给C ++ 11和继承构造函数一个机会。
第二个是,您有一个物体(直升飞机),但是您一次又一次询问起始高度,依此类推。 为真实世界建模:有一个直升机在飞行。 因此,请在直升机对象中收集您当前的飞行状态。
您应该大致了解设计模式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.