0

I'm new to c++ and i'm practicing myself on classes and objects. I've made a program which includes operator overloading and it has an "explicit constructor". I've tried it with a lots of different values and it's working properly, but my explicit constructor differs itself a bit from my teacher's answer. Teacher's answer:

class Ventilator {
int maxrot;
int rot;
int vert;
bool horiz;
public:
explicit Ventilator(int maxrot=500, int rot=0, int vert=0, int horiz=false): maxrot(abs(maxrot)),
   rot(rot<0?0:rot>abs(maxrot)?abs(maxrot):rot), vert(vert<-15||vert>15?0:vert),
   horiz(horiz)  { }

Mine is:

class Ventillator{
 int rotation;
 int vertikal;
 int maxrot;
int horiz;
public:
explicit Ventillator(int rotation = 10, int maxrot=100, int vertikal = 11, bool horiz = true):
    rotation(abs(rotation)), maxrot(abs(maxrot)), vertikal(abs(vertikal)), horiz(horiz){

    if (this->rotation > this->maxrot) this->rotation=this->maxrot;
    if (this->rotation < 0) this->rotation = 0;
    if (this->vertikal <= 15 && this->vertikal >=-15) this->vertikal=vertikal;
    else
        this->vertikal = 0;
    }

Aren't both ways quite the same? Isn't he just using special operators in order the code to be more compact?

specbk
  • 99
  • 2
  • 13

2 Answers2

2

The two examples achieve the same effect in this case. Using the initializer list only is (I would argue) generally more elegant because

  • It means the member can be const
  • The member is initialized to the correct value straight away. This can be important if another member initializer or the constructor body can throw.

However, I would argue that expressions with two ternary operators (?:) can be hard to read - I would probably have written a helper function to return the value to initialize rotation with.

  • I think i got it, thanks a lot! – specbk Jun 12 '16 at 16:03
  • Ternary operators are especially hard to read when you format them like your spacebar is broken! Judicious use of whitespace is critical. But I totally agree with you about writing a helper function. I do this frequently in my own code, but I've always kind of wondered about it, because I hardly ever see it in other people's code. – Cody Gray - on strike Jun 12 '16 at 16:56
1

No, there is some difference, just ideological. Your solution is better because it is more readable—you can teach your teacher how to write beautiful code :)

Also, you rewrite that code like this:

#include <algorithm>
class Ventillator
{
    int rotation;
    int vertikal;
    int maxrot;
    int horiz;
    inline int place_in_range(int num, int max, int min)
    {
        return ( (std::max) ( ( (std::min)(max, num) ), min));
    }
    inline bool is_in_range(int num, int max, int min)
    {
        return num <= max && min >= num;
    }

public:
    explicit Ventillator(int rotation = 10, int maxrot=100, int vertikal = 11, bool horiz = true):
        rotation(place_in_range(abs(rotation), maxrot, 0)), maxrot(abs(maxrot)), vertikal( (is_in_range(vertikal, 15, -15) ? abs(vertical) : 0 )), horiz(horiz)
    {
    }
};
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Inline
  • 2,566
  • 1
  • 16
  • 32