next up previous contents index
Next: Inheritance Up: C++ Previous: Classes   Contents   Index


Constructors

Sometimes it is desireable, when setting up the object, to supply some initialization data at the time. For instance, in the previous example, we may want to suppy the name and the warm-bloodedness of the animal when we construct the object, rather than going through a dialogue. For this we can use a constructor - this is a method that has the same name as the class, and is called when the object is created.

The following essentially moves the functionality of the get_info method of the previous example to the constructor. The header file animal.cpp declares the constructor animal:

// file animal.h
#include <string.h>
class animal {
  char name[50];
public:
  bool warm_blooded;
  void print_info(void);
  animal(char *in_name, bool in_warm_blooded);
};
where the constructor appears in
// file animal.cpp
#include <iostream.h>
#include <string.h>
#include "animal.h"

animal::animal(char *in_name, bool in_warm_blooded) {
  strcpy(name, in_name);
  warm_blooded = in_warm_blooded;
}
void animal::print_info(void) {
  cout << endl << "name: " << name << endl;
  if (warm_blooded == true) {
    cout << "The animal is warm blooded" << endl;
  } 
  else {
    cout << "The animal is not warm blooded" << endl;
  }
}
Here, the constructor accepts two arguments - a character array and a boolean - and sets the corresponding object attributes to these. Then, when constructing the object:
// file jane.cpp
#include <iostream.h>
#include <string.h>
#include "animal.h"

int main(void) {
  animal jane("Janette", true);
  jane.print_info();
  return (0);
}
we can pass in the attributes when we create the object. If one prefers, one can use references in creating the object as
// file jane.cpp
#include <iostream.h>
#include <string.h>
#include "animal.h"

int main(void) {
  animal *jane;
  jane = new animal("Janette", true);
  jane->print_info();
  return (0);
}
for which the new method is used to create the object, and object attributes and methods are accessed through the arrow notation.

It is also possible to supply more than one constructor, accepting different argument lists, which will initialize an object in different ways, depending on how it is called. For example, we could have two constructors, one of which sets both the name and warm_blooded, and another that just sets the name:

// file animal.h
#include <string.h>
class animal {
  char name[50];
public:
  bool warm_blooded;
  void print_info(void);
  animal(char *in_name, bool in_warm_blooded);
  animal(char *in_name);
};
where the constructors are defined in
// file animal.cpp
#include <iostream.h>
#include <string.h>
#include "animal.h"

animal::animal(char *in_name, bool in_warm_blooded) {
  strcpy(name, in_name);
  warm_blooded = in_warm_blooded;
}
animal::animal(char *in_name) {
  strcpy(name, in_name);
}
void animal::print_info(void) {
  cout << endl << "name: " << name << endl;
  if (warm_blooded == true) {
    cout << "The animal is warm blooded" << endl;
  } 
  else {
    cout << "The animal is not warm blooded" << endl;
  }
}
The objects are then created as
// file jane_and_fred.cpp
#include <iostream.h>
#include <string.h>
#include "animal.h"

int main(void) {
  animal *jane, *fred;
  jane = new animal("Janette", true);
  jane->print_info();

  fred = new animal("Franklin");
  fred->warm_blooded = false;
  fred->print_info();
  return (0);
}
Note that we are able to set the warm_blood attribute from the main program because this attribute is declared as public in the main program - it is generally good practice to make public only those attributes and methods that are absolutely essential to the user (a corollary of Murphy's Law says that, if a user is able in principle to do something unexpected, a user will do that).
next up previous contents index
Next: Inheritance Up: C++ Previous: Classes   Contents   Index