简体   繁体   中英

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. So far I have written the code as follows, but I think it doesn't have OOP properties.

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.

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:

1) In main you are asking for data which is only related to the object you create later. The class should know which parameter it needs and not any unrelated method outside.

2) I can't see any real objects at all! What I can see is C code with the use of 'class' in front. The "object" you create has nothing it makes it a object.

So create a base class with a speaking name like: FlyingMode and one! method which is maybe called FuelConsuming. Make this method in the base class virtual and delete it!

Derive classes from this base class and overwrite FuelConsuming. The class names could be: Landing, Rising .... and so on.

Make your constructors of the classes interacting with the gui itself. Normally you should use a serializer, but at first make the cin/cout in the constructor. 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!

EDIT: You should have a look at your code. Why you need 5 times the exact same code? Looks like a design problem! Give C++11 and inheriting constructors a chance.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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