简体   繁体   English

具有类设计的C ++ OOP

[英]C++ OOP with class design

I have a task of calculating helicopter's necessary fuel intake. 我的任务是计算直升机的必要燃油摄入量。 There are four operations a helicopter can do: 直升机可以执行以下四个操作:

  • Holding 保持
  • Raising 提高
  • Landing 降落
  • Straight 直行

User selects the helicopter's actions by entering the parameters. 用户通过输入参数选择直升机的动作。 I need to create this system with OOP approach. 我需要使用OOP方法创建该系统。 So far I have written the code as follows, but I think it doesn't have OOP properties. 到目前为止,我已经编写了如下代码,但是我认为它没有OOP属性。

Any ideas on how to make this system object-oriented? 关于如何使该系统面向对象的任何想法? Here is the code: 这是代码:

#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;
}

Edit: I want to use most of the OOP principles, although it might seem unnecessary. 编辑:尽管似乎没有必要,我想使用大多数OOP原则。

SOLUTION: 解:

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;
          }
      }
};

On a first view you did a lot of non OOP things in your example: 在第一个视图上,您​​在示例中做了很多非OOP的事情:

1) In main you are asking for data which is only related to the object you create later. 1)在主要方面,您要求的数据仅与以后创建的对象有关。 The class should know which parameter it needs and not any unrelated method outside. 该类应该知道它需要哪个参数,而不是外面没有任何不相关的方法。

2) I can't see any real objects at all! 2)我根本看不到任何真实的物体! What I can see is C code with the use of 'class' in front. 我看到的是在前面使用“类”的C代码。 The "object" you create has nothing it makes it a object. 您创建的“对象”没有任何东西使其成为对象。

So create a base class with a speaking name like: FlyingMode and one! 因此,创建一个具有语音名称的基类,例如:FlyingMode和一个! method which is maybe called FuelConsuming. 该方法可能称为FuelConsuming。 Make this method in the base class virtual and delete it! 在基类中将此方法设置为虚拟,然后将其删除!

Derive classes from this base class and overwrite FuelConsuming. 从该基类派生类并覆盖FuelConsuming。 The class names could be: Landing, Rising .... and so on. 类的名称可以是:Landing,Rising ....等。

Make your constructors of the classes interacting with the gui itself. 使您的类的构造函数与gui本身进行交互。 Normally you should use a serializer, but at first make the cin/cout in the constructor. 通常,您应该使用序列化程序,但首先要在构造函数中使用cin / cout。 And for creating the instances of the mode classes you should read about a "factory". 对于创建模式类的实例,您应该阅读有关“工厂”的信息。

This is only a very small hint to do OOP in your little example! 在您的小示例中,这只是做OOP的很小的提示!

EDIT: You should have a look at your code. 编辑:您应该看看您的代码。 Why you need 5 times the exact same code? 为什么需要5倍完全相同的代码? Looks like a design problem! 看起来像是设计问题! Give C++11 and inheriting constructors a chance. 给C ++ 11和继承构造函数一个机会。

The next is, that you have a object ( helicopter ) but you ask again and again for start altitude and so on. 第二个是,您有一个物体(直升飞机),但是您一次又一次询问起始高度,依此类推。 Model the real world: There is a helicopter object which flies. 为真实世界建模:有一个直升机在飞行。 So collect your current state of the flight in the helicopter object. 因此,请在直升机对象中收集您当前的飞行状态。

You should read about design patterns in general. 您应该大致了解设计模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM