Constructors

Constructors

Defining and using constructors

#include <iostream>
#include <cstdlib>
using namespace std;


class Robot {
private:
  int x;   // coordinate
  bool ok; // true = in a working condition
public:

  // Constructors:
  Robot(); // the default constructor
  Robot(int coord); // a constructor with one parameter
  Robot(int coord, bool status); // with two parameters

  int get_x();
};

int main() {

  Robot r1; // the default constructor gets called

  Robot r2(15);
  
  Robot r3(-10, false);

  Robot r4 = Robot(16);

  Robot r5;
  r5 = Robot(17);

  cout << "r1 is at " << r1.get_x() << endl;
  cout << "r2 is at " << r2.get_x() << endl;
  cout << "r3 is at " << r3.get_x() << endl;
  cout << "r4 is at " << r4.get_x() << endl;
  cout << "r5 is at " << r5.get_x() << endl;
}

/************** IMPLEMENTATION ***************/

Robot::Robot() : x(777), ok(true) {}

Robot::Robot(int coord) {
  x = coord;
  ok = true;
}

Robot::Robot(int coord, bool status) : x(coord), ok(status)
{
  if (x < 0) x = 0;
}

int Robot::get_x() {
  return x;
}

r1 is at 777
r2 is at 15
r3 is at 0
r4 is at 16
r5 is at 17

The program about robots.

Notice that there are three constructors. The default constuctor is called when you just declare an array of Robots. Then, we explicitly call another constructor to individually initialize each of the robots.

Three constructor definitions use different approaches to initializing the member variables: it can be done in the function body or before it, in the latter case the body itself can be empty, or you may add some extra comutation there.

There is one const member function accept that is called by the function find_coord. Since this function takes the Robot r as a constant reference, it can call only constant member functions of the class Robot. So, it could call the method accept, but, for example, would not be able to call move_right or disable.

#include <iostream>
#include <cstdlib>
using namespace std;

enum Ground {FLOOR, TRAP};
const int SIZE = 20;
const int NUM = 5;

class Robot {
private:
  int x;   // coordinate
  bool ok; // true = in a working condition
public:

  // Constructors:
  Robot(); // the default constructor
  Robot(int coord); // a constructor with one parameter
  Robot(int coord, bool status); // with two parameters

  int get_x();
  void move_left();
  void move_right();
  bool accept(Ground g) const; // constant member function
  void disable();
};

// finds a suitable position in the map
// this function is gets the parameter Robot r by reference
// passed as constant, so we cannot change it
int find_coord(Ground map[SIZE], const Robot &r);

int main() {
  srand(time(NULL));

  Ground map[SIZE];
  // make a 1D map that looks like this:
  //
  //  %..................%
  //
  //  wher % = trap, and . = floor
  //
  for(int i = 0; i<SIZE;i++) {
    map[i] = FLOOR;
  }
  map[0] = TRAP;
  map[SIZE-1] = TRAP;

  Robot rob[NUM]; // default constructor gets executed here
                  // for each element of the array
  for(int i = 0; i<NUM; i++) {
    // call another constructor here to individually initialize
    // each of the robots
    rob[i] = Robot(find_coord(map, rob[i]), true);
  }

  // let the robots move to the right for 100 steps,
  // eventually all of them get trapped and deactivated.
  for(int t = 0 ; t < 100; t++) {
    
    for (int i = 0; i < NUM; i++ ) {
      rob[i].move_right();
      Ground g = map[rob[i].get_x()];
      if (! rob[i].accept(g)) 
        rob[i].disable();
    }

  }
 
  for(int i = 0; i<NUM; i++) {
    cout << rob[i].get_x() << ' ';
  }

}


/************** IMPLEMENTATION ***************/

Robot::Robot() : x(0), ok(true) {}

Robot::Robot(int coord) {
  x = coord;
  ok = true;
}

Robot::Robot(int coord, bool status) : x(coord), ok(status)
{
  if (x < 0) x = 0;
}

bool Robot::accept(Ground g) const {
  if (g == FLOOR) return true;
  return false;
}

int Robot::get_x() {
  return x;
}

void Robot::move_left() {
  if (ok) x --;
}

void Robot::move_right() {
  if (ok) x ++;
}

void Robot::disable() {
  ok = false;
}

// Finding a suitable location that is accepted by the robot,
// using rejection sampling
int find_coord(Ground map[SIZE], const Robot &r) {
  while(true) {
    int i = rand() % SIZE;
    // we can call only constant methods of the class Robot
    // such as the member function `accept`,
    // we would not be able to call `move_left` for example.
    if (r.accept(map[i])) return i;
  }
}

19 19 19 19 19