Tuesday, September 5, 2006
Classes and Objects

Topics

Constructors and Destructors
   
 - Constructors and destructors are special methods that are used to initialize the object, and to clean up the object just before it goes away.
   
 - Constructors have the same syntax as any other function except they have the following two restrictions:

        1. They have no return type at all, not even void
   
    2. They must have the exact same name as the class name

     - Constructors are used to initialize the instance variables inside the class.
   
 -
In good object design, you always initialize all instance variables to logical values when the object is constructed.  This means the object always starts in a consistent, logical state.
     - The constructor with no parameters is called the "default" constructor.
     - Constructors can also be written with parameters.  These are usually overloaded functions.  Overloaded functions are functions with the same name but a different number of parameters, or different data types for the parameters, or a different order of the data types in the parameters..
     - If you don't write any constructors at all, the compiler will write a default constructor with an empty pair of curly braces, {}.  If you write any constructors, with any parameters, then the compiler will only use the constructors you write. 

     - Destructors are exactly like constructors, except they have two additional restrictions:

        1. They have a tilde character, ~, in front of their name.
   
    2. They can have no parameters, although they must have the empty parentheses, ().  Therefore they cannot be overloaded, and there can be only one destructor, the default destructor.

     - Destructors are often just empty functions, because they are usually used for releasing resources such as files or memory allocations, which is done only in specific types of classes.    
   
 - Destructors are optional, but some C++ experts say you should write one anyway, even if it is empty.  The compiler will write a default, empty destructor for you if you don't write it.
 

Constructor example -- in the .h file:

    class Rectangle
    {
    public:

        Rectangle();   // default constructor
        Rectangle(int, int); // overloaded constructor
       
        ~Rectangle();  // destructor

        void setWidth(int);
        void setHeight(int);
        int getWidth();
        int getHeight();

        int calcArea();

    private:
        int width, height;
    };

 


Constructor definitions the .C file:

    Rectangle::Rectangle(int h, int w)
    {
        setHeight(h);
        /* has the same effect as:
        if (h > 0)
            height = h;
        else
            height = 0;
        */
       
        setWidth(w);
        /* has the same effect as:
        if (w > 0)
            width = w;
        else
            width = 0;
        */
    }

    Rectangle::Rectangle()
    {
        height = 0;
        width = 0;
    }

    Rectangle::~Rectangle()
    {
    }
 

   - The default constructor is now called when the object is declared.  This is a hidden function call, that is not directly apparent in the C++ code.
  
- Notice the constructors initialize the instance variables in much the same way the
setHeight and setWidth functions do.  It is often smart to call the setHeight and setWidth functions from within the constructors.  It is usually better to call a function than to duplicate its code in another function.
  
- Now our objects always have logical values no matter how they are created.  If the programmer does not set the height and width with
setHeight and setWidth, the default constructor will set the height and width to 0. 
  
- Now lets use the overloaded Constructor to initialize our objects.  Notice the parentheses and parameters in the object declaration.  An object declaration is a constructor call.  Always.

#include <iostream>
#include "Rectangle.h"

using namespace std;

int main()
{
    Rectangle box(10, 20);

    Rectangle square(12, 12);

    cout << "The area of a box of width " <<
        box.getWidth() << " and height " <<
        box.getHeight() << " is " <<
        box.calcArea() << "." << endl;

    cout << "The area of a square of width " <<
        square.getWidth() << " and height " <<
        square.getHeight() << " is " <<
        square.calcArea() << "." << endl;

    return 0;
    
}
 

The Public Interface of a class
   
 - If you look in the header file you can see the prototypes of all the methods of the class.
     - The
 public:  methods, taken together, are called the "Public Interface" of the class. 
     - During normal programming, a programmer using your class only has access to your public functions.  The programmer need not worry about how the functions work, and should never have to worry about variables inside your class.
     - A programmer using your class can view it just as set of services provided through the Public Interface, nothing more.
     - In a well designed class, a programmer using your class can never corrupt it. 
     - The .h and .c or .cpp files allow a C or C++  program to be distributed by giving out the .h and .obj files only.  The .obj files are the compiled .c or .cpp files.   Both the compiler and the programmer using your code only need the .h files to complete their work.  The linker can combine all of the .obj files, the programmer's new compiled code and your compiled code, to create a complete program.  You never have to distribute the source code in the .c or .cpp files.
 

Readings

Deitel and Deitel, Chapter 3, all except section 3.11.

Back to Csc 125 - Computer Science II, Programming in C++
  Scott Badman   Office:  B132   Phone:  353-2250   sbadman@parkland.edu  

Parkland College, 2400 W. Bradley Avenue, Champaign, IL 61821