FAQ 2.19 What are the basics of adding a constructor to a class?

graphics/new_icon.gif

A constructor (a.k.a. ctor) is a special member function that is called whenever an object of the class is created. This gives the class developer a chance to initialize the object's member data so that the rest of the member functions can assume that they have a coherent object to work with. Syntactically constructors are member functions with the same name as the class; they are not virtual, and they have no return type.

Like normal member functions, constructors are declared in the class's body, which normally appears in the class's header file. For example, the header file for class Car might be file Car.hpp. Here is an example showing the declaration of some constructors in header file Car.hpp:

 #ifndef CAR_HPP #define CAR_HPP class Car { public:   Car();                                             <-- 1   Car(int initRadioFreq, int horsepower);            <-- 2   // ... protected:   bool isRunning_;   bool radioOnAM_;   int  radioFreq_;   int  horsepower_; }; #endif 

(1) Declare a constructor

(2) Declare another constructor

The first constructor takes no parameters and is called whenever an object is created without parameters. For example, the first constructor is used to initialize the first two Car objects created in the following function , and the second constructor (the one that takes two parameters of type int) is used to initialize the third and fourth Car objects created in the following function f().

 void f() {   Car a1;                                            <-- 1   Car* p1 = new Car();   Car a2(880, 200);                                  <-- 2   Car* p2 = new Car(880, 200);   // ... } 

(1) The first ctor is used to initialize a1 and *p1

(2) The second ctor is used to initialize a2 and *p2

Constructors are often defined in the source file associated with the class. For example, the source file associated with class Car might be file "Car.cpp". Here is an example showing the definition of the first constructor in source file Car.cpp:

 #include "Car.hpp"                                   <-- 1 Car::Car()                                           <-- 2 : isRunning_ (false) , radioOnAM_ (true) , radioFreq_ (880) , horsepower_(150) {   // ... } 

(1) Get the Car class

(2) Define a constructor

The line Car::Car() tells the compiler that this is the definition of a constructor of class Car. Thus constructors are normally of the form X::X(/*...*/).

The line : isRunning_ (false) initializes the protected: data member isRunning_ to false; radioOnAM_, radioFreq_, and horsepower_ are initialized similarly. This list of initializations between the : and the { is allowed only in constructors and is called an initialization list. Since the goal of the constructor is to initialize the object to a coherent state, all of an object's member variables should be initialized in every constructor.

Since the second constructor takes parameters, it probably uses these parameters to initialize the member variables in the Car object. For example the two parameters might be used to initialize the radio's frequency and the car's horsepower:

 Car::Car(int initRadioFreq, int horsepower) : isRunning_ (false) , radioOnAM_ (false) , radioFreq_ (initRadioFreq) , horsepower_(horsepower) {   // ... } 


C++ FAQs
C Programming FAQs: Frequently Asked Questions
ISBN: 0201845199
EAN: 2147483647
Year: 2005
Pages: 566
Authors: Steve Summit

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net