CONSTRUCTORS

Choose a suite (any suite!): Spades - Defining Constructors, Hearts - Programmer Defined Member Functions, Diamonds - Copy Constructors, Clubs - Destructors

.
CONTENTS



DEFINING CONSTRUCTORS

To create an instance of a class we must use a creation instruction which is associated with that class. In C++ such an instruction is referred to as a constructor. A C++ constructor is a member function of some class which is automatically executed whenever an instance of that class comes into existence. In C++ a default constructor function is available for each built-in and programmer defined class. For example in the following C++ code an instance of the class Circle is created using the built-in constructor function automatically created during compilation:

class Circle {
        private:
                float Radius;
                float Area;
                float Circumference;
        };

void main(void)
{
Circle disc = Circle();
}

Note that the built-in constructor function has the same name as the class name to which it refers.


Programmer Defined Constructor Functions

Once we have created an instance of a class we will wish to assign values to the associated data members. We can either:

  1. Write public member functions, or
  2. Initialise objects on declaration.

The latter is more desirable, however, to do this we must write our own constructor function. A C++ example is given below:

class Circle {
        private:
                float Radius;
                float Area;
                float Circumference;
        public:
                Circle(float ra = 0.0, float ar = 0.0, float ci = 0.0);
        };

Circle::Circle(float ra, float ar, float ci)
{
Radius = ra;
Area = ar;
Circumference = ci;
}

void main(void)
{
Circle disc = Circle(3.5);
Circle disc = Circle(7.0,154.0,44.0);
}

Notes

  1. The :: operator used in the above code is referred to as the function scope operator. This operator is used to identify the class (its prefix operand) to which a member function belongs (its postfix operand).
  2. Before a programmer defined member function (constructor or otherwise) can be used its "prototype" must be incorporated into the associated class definition.
  3. The prototype goes in the public section of the class definition.
  4. Constructors do not have a return type (not even void).
  5. Default values of 0.0 have been included.

The above thus creates two instances, x and y, of the class Circle such that the first uses default values for the data members Area and Circumference.




WRITING MEMBER FUNCTIONS (METHODS)

Private data members can only be manipulated by public member functions. Thus to (say) output the private data associated with an object we must write an appropriate public member function to add to the above program:

PRESS
#include 

class Circle {
        private:
                float Radius;
                float Area;
                float Circumference;
        public:
                Circle(float ra = 0.0, float ar = 0.0, float ci = 0.0);
                void OutputCircleData(void);
        };

Circle::Circle(float ra, float ar, float ci)
{
Radius = ra;
Area = ar;
Circumference = ci;
}

void Circle::OutputCircleData(void)
{
cout << "Radius:  "  << Radius << "\n" ;
cout << "Area:    "  << Area << "\n" ;
cout << "Circumference: "  << Circumference << "\n" ;
}

void main(void)
{
Circle disc1 = Circle(3.5);
cout << "CIRCLE disc1\n========\n";
disc1.OutputCircleData();

Circle disc2 = Circle(7.0,154.0,44.0);
cout << "\nCIRCLE disc2\n========\n";
disc2.OutputCircleData();
}

We can define further member functions to (say) calculate area and circumference thus:

#define PI 3.1428571

void Circle::CalcArea(void)
{
Area = pi*Radius*Radius;
}

void Circle::CalcCircumference(void)
{
Circumference = pi*Radius*2;
}

Again we must include the appropriate function prototypes in the Circle class definition:

// circle.h

class Circle {
        private:
                float Radius;
                float Area;
                float Circumference;
        public:
                Circle(float ra = 0.0, float ar = 0.0, float ci = 0.0);
                void OutputCircleData(void);
                void CalcArea(void);
                void CalcCircumference(void);
        };

The CalcArea and CalcCircumference member functions may now be incorporated into some application code as follows:

PRESS
main()
{
Circle disc1 = Circle(3.5);
cout << "CIRCLE disc1\n========\n";
disc1.CalcArea();
disc1.CalcCircumference();
disc1.OutputCircleData();

Circle disc2 = Circle(7.0,154.0,44.0);
cout << "\nCIRCLE disc2\n========\n";
disc2.OutputCircleData();
}



C++ COPY CONSTRUCTORS

A copy constructor is special type of C++ constructor that allows initialisation of an object using another object of the same class through the use of an assignment operator (=). For example:

disc1 = disc2;

where x and y are instances of the the same class. As with ordinary constructors the C++ compiler creates a default copy constructor for each class. However, programmers are of course free to create their own copy constructors. The declaration of a copy constructor has the general form:

className(const className &objectName)

As with standard constructors the function name must be the class name. Note that the argument is an address (reference).


Example

PRESS
class Circle {
private:
        float Radius;
        float Area;
        float Circumference;
public:
        Circle(float ra = 0.0, float ar = 0.0, float ci = 0.0);
        Circle(const Circle &);
        void OutputCircleData(void);
};

Circle::Circle(const Circle &oldCircle)
{
Radius = oldCircle.Radius;
Area = oldCircle.Area;
Circumference = oldCircle.Circumference;
}

void main(void)
{
Circle disc1 = Circle(7.0,154.0,44.0);
cout << "\n(disc1)\n";
disc1.OutputCircleData();

Circle disc2 = disc1;
cout << "\n(disc2)\n";
disc2.OutputCircleData();
}

Here we have created an instance x which is an instance of the class Circle and then created a second instance y of the class Circle, with the same data member instantiations, using a copy constructor.




DESTRUCTORS

When a constructor creates an object the program tracks the object until it expires. At that time a special member function called a destructor is called, which will free memory etc. A destructor comprises a class name preceded by a "tilde" (~). Thus the destructor for our Circle class would be ~Circle(). Note that:

  1. A destructor cannot have any arguments.
  2. C++ automatically creates default destructors for each class, although programmers are free to write their own.



Return to oop home page or continue.




Created and maintained by Frans Coenen. Last updated 03 July 2001