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?