Your Ad Here

Monday, November 3, 2008

Overloaded Constructors

It’s convenient to be able to give variables of type Distance a value when they are first created. That is, we would like to use definitions like

Distance width(5, 6.25);

which defines an object, width, and simultaneously initializes it to a value of 5 for feet and 6.25 for inches.

To do this we write a constructor like this:

Distance(int ft, float in) : feet(ft), inches(in)
{ }

This sets the member data feet and inches to whatever values are passed as arguments to the constructor. So far so good.

However, we also want to define variables of type Distance without initializing them, as we did in ENGLOBJ.

Distance dist1, dist2;

In that program there was no constructor, but our definitions worked just fine. How could they work without a constructor? Because an implicit no–argument constructor is built into the program automatically by the compiler, and it’s this constructor that created the objects, even though we didn’t define it in the class. This no–argument constructor is called the default constructor. If it weren’t created automatically by the constructor, you wouldn’t be able to create objects of a class for which no constructor was defined.

Often we want to initialize data members in the default (no–argument) constructor as well. If we let the default constructor do it, we don’t really know what values the data members may be given. If we care what values they may be given, we need to explicitly define the constructor. In ENGLECON we show how this looks:

Distance() : feet(0), inches(0.0) //default constructor
{ } //no function body, doesn’t do anything

The data members are initialized to constant values, in this case the integer value 0 and the float value 0.0, for feet and inches respectively. Now we can use objects initialized with the no–argument constructor and be confident they represent no distance (0 feet plus 0.0 inches) rather than some arbitrary value.

Since there are now two explicit constructors with the same name, Distance(), we say the constructor is overloaded. Which of the two constructors is executed when an object is created depends on how many arguments are used in the definition:

Distance length; // calls first constructor
Distance width(11, 6.0); // calls second constructor

Member Functions Defined Outside the Class
So far we’ve seen member functions that were defined inside the class declaration. This need not always be the case. ENGLCON shows a member function, add_dist(), that is not defined within the Distance class declaration. It is only declared inside the class, with the statement

void add_dist( Distance, Distance );

This tells the compiler that this function is a member of the class but that it will be defined outside the class declaration, someplace else in the listing.

In ENGLCON the add_dist() function is defined following the class declaration. It is adapted from the ENGLSTRC program in Chapter 4:

//add lengths d2 and d3
void Distance::add_dist(Distance d2, Distance d3)
{
inches = d2.inches + d3.inches; //add the inches
feet = 0; //(for possible carry)
if(inches >= 12.0) //if total exceeds 12.0,
{ //then decrease inches
inches –= 12.0; //by 12.0 and
feet++; //increase feet
} //by 1
feet += d2.feet + d3.feet; //add the feet
}

No comments: