Designing a Class

The most basic component of an Object Oriented design is the Class. When you start designing a system using object oriented techniques you usually start by identifying the classes that are required for the system. Once all of your classes are identified you can then move on to designing the classes themselves.

You don't have to start completely from scratch when designing a new class because each class will have certain core functionality in common with all or many of the other classes simply by its being a "class" rather than belonging to some other class of objects. What we can do therefore is to start by considering those parts of a class which most or all classes can be expected to have and define a class "template" containing those items and which we can therefore use as the starting point for our actual specific class design. For the purposes of this template design we can call the class myclass and we'll assume that we are going to code in C++. If you are going to use a different language then you will need to translate the following to that language before you start using it as the basis of your subsequent class design.

Let's begin by defining the class. The class can either be a base class or it can be derived from another class. Let's assume that it is derived since to define a base class we need simply delete the derivation code.

class myclass : public parentclass{

The next step is to add in those functions which each class usually needs to be able to perform. Let's add a default constructor (to create a member of the class when no information about the object is yet known), a copy constructor (to create a new object from an existing one), and a destructor (to delete objects once they are finished with). Just about all of our classes should need these functions. We also need the ability to copy objects when the destination already exists so we'll need to define the = operator for this. Let's also add some comments to remind us where to put some of the other declarations that we will need in our classes and how they work.

class myclass : public parentclass
   //define friend classes here
      // default constructor
      // copy constructor
      myclass(myclass& obj);
      // destructor
      // copy operation
      myclass& operator=(myclass& obj);
      // other generally accessible functions go here
      // define static member functions here these apply
      // to the class as a whole rather than a specific
      // object
      // define friend functions and operators here
      // insert const on the end of any function
      // that should not alter any values
      // insert = 0 on the end of any abstract functions
      // (to be fully defined in a derived class,
      // also define the return type as virtual
      // other functions and data members only accessible
      // to this class and classes derived from this
      // class go here
      // define static data members here
      // functions and data members required only for
      // the internal functioning of the class go here

This gives us a basic class "template" that we can use as the starting point for creating our classes that will help us to ensure that basic functionality is not overlooked and everything gets inserted in the right place.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow