Back to July Calendar 

Previous Entry Next Entry→

September 09, 2005

I need some serious brushing up on data structures in C++.  So I turn to MIT.  Skipping on to here, we find the basic structure of object oriented programs with class structures, definitions and implementations. 

The example I'm interested in today is elemental to graphics: how to build up the structure and definition of a point in 2D.  The structure would typically be described in a header file called point.h:


// Declaration of class Point.
#ifndef _POINT_H_
#define _POINT_H_
#include <iostream.h>  // Note this is a pit antiquated, <iostream> is preferred
                       // followed by
                       // using namespace std;
class Point {
  // The state of a Point object. Property variables are typically
  // set up as private data members, which are read from and
  // written to via public access methods.

    float mfX;
    float mfY;
  // The behavior of a Point object.
    Point();                   // The default constructor.
    Point(float fX, float fY); // A constructor that takes two floats.
    Point(const Point& p);     // The copy constructor.
    ~Point();                  // The destructor.
    void print() {             // This function will be made inline by default.
        cout << "(" << mfX << "," << mfY << ")" << endl;
    void set_x(float fX);
    float get_x();
    void set_y(float fX);
    float get_y();
// end of class Point structure
#endif  // _POINT_H_

Then one sets about defining special constructors


// Definition class Point.
#include "point.h"

// A constructor which creates a Point object at (0,0).

Point::Point() {
    cout << "In constructor Point::Point()" << endl;
    mfX = 0.0;
    mfY = 0.0;

// A constructor which creates a Point object from two floats.
Point::Point(float fX, float fY) {
    cout << "In constructor Point::Point(float fX, float fY)" << endl;
    mfX = fX;
    mfY = fY;

// A constructor which creates a Point object from another Point object.
// This is what's called a copy constructor.
Point::Point(const Point& p) {
    cout << "In constructor Point::Point(const Point& p)" << endl;
    mfX = p.mfX;
    mfY = p.mfY;

// The destructor.
Point::~Point() {
   cout << "In destructor Point::~Point()" << endl;

// Modifier for x coordinate.
void Point::set_x(float fX) {
    mfX = fX;

// Accessor for x coordinate.
float Point::get_x() {
    return mfX;

// Modifier for y coordinate.
void Point::set_y(float fY) {
    mfY = fY;

// Accessor for y coordinate.
float Point::get_y() {
    return mfY;

Finally, an application illustrating what's up here.


// Test program for the Point class.
main() {
    Point a;
    Point b(1.0, 2.0);
    Point c(b);

    // Print out the current state of all objects.

    // Print out the current state of b.
    cout << endl;
    return 0;

Here's the output of the above program:

In constructor Point::Point()
In constructor Point::Point(float fX, float fY)
In constructor Point::Point(const Point& p)