Classes#

We’ll think of a class as a user-defined object that contains:

  1. Member variables, i.e variables that belongs to the class.

  2. Class methods, i.e functions that belong to the class.

They provide a flexible way to package code that facilitates creation of readable and easy-to-use C++ programs.

Note

For more examples of how to write and use classes, see the code examples here.

Division of files#

We recommend that you structure your code as follows:

  1. Place the declaration and defintion of your class in a header file class_name.hpp.

  2. Place definitions of all class constructors and class methods in a source file named class_name.cpp.

Don’t worry, we’ll explain how shortly.

General structure of the class code#

The #include guard#

To prevent multiple inclusions when we’re dealing with many header and source files, we create a so-called #include guard (a.k.a. define guard, a.k.a. header guard) in each header file. The generic structure of an #include guard looks like this:

#ifndef __filename_hpp__
#define __filename_hpp__

... (the rest of the file) ...

#endif

Always do this for header files. The exact name you use (here __filename_hpp__) doesn’t matter, as long as it is unique to this header file.

Generic syntax of a class header file#

Let’s say we want to create a class MyClass. The generic structure of a header file MyClass.hpp for this class would be

#ifndef __MyClass_hpp__   
#define __MyClass_hpp__

class MyClass 
{
private:
  // Declaration of variables only accessible from within the class

public:
  // Declaration of constructors, e.g.
  MyClass(arguments);

  // Declaration of destructors, copy constructors, ...

  // Declarations of other class methods, e.g.
  void some_function(arguments);

}; // <-- Note that class bodies end with a semicolon!

#endif

(We won’t need to worry about destructors, copy constructors and such just yet…)

Generic syntax of class source file#

The corresponding source file (MyClass.cpp) would have this structure:

#include "MyClass.hpp"

// Definitions of constructors
MyClass::MyClass(arguments)
{
  // ...
}

// Definitions of copy constructor, ...

// Definitions of other class methods, e.g. 
void MyClass::some_function(arguments)
{
  // ...
}

Practical example: A straight line.#

Let’s create a class Line that does one simple thing: it returns the value of a straight line of the form y = c0 + c1*x at some point x.

The straight line: header file#

Following the general structure above, we create a header file Line.hpp as follows:

#ifndef __Line_hpp__
#define __Line_hpp__

class Line 
{
protected:
  double c0_, c1_; // Parameters of a line: y = c0 + c1*x

public:
  // Constructor
  Line(double c0, double c1);

  // Method for evaluating y(x) at some point x
  double eval(double x);
};

#endif

A couple notes:

  1. We have defined coefficents c0_ and c1_ that will store the coefficients of the straight line. The variables are called member variables of the class.

  2. The trailing underscore on the member variables is there to distinguish them from other variables used in the class. This is just a convention, in this case following the Google styleguide for C++.

The straight line: source file#

In the source file, we provide defitions for the constructor Line(double c0, double c1), which creates an instance (object) of the class, and our only class method, double eval(double x). Note that in the source file we have to prefix the function names with the class namespace Line::. We end up with the following simple source file, Line.cpp:

#include "Line.hpp"

// Definition of the constructor
Line::Line(double c0, double c1)
{
  // Use the input variables (c0, c1) to assign values to the class memeber variables (c0_, c1_)
  c0_ = c0;
  c1_ = c1;
}

// Definition of the eval function
double Line::eval(double x)
{
  // Return the value of a straight line at a point x
  return c0_ + c1_*x;
}

The straight line: using the class#

The main program that uses the Line class can be as simple as

#include "Line.hpp"

int main() 
{
  // Create a Line object called my_line, with coefficients c0 = 1.5, c1 = 2.5
  Line my_line = Line(1.5, 2.5); 

  // Evaluate the line y = c0 + c1*x at point x = 2
  double y = my_line.eval(2);

  return 0;
}

Inheritance: base classes and derived classes#

To be continued…