1. Homepage of Dr. Zoltán Porkoláb
    1. Home
    2. Archive
  2. Teaching
    1. Timetable
    2. Bolyai College
    3. C++ (for foreign studenst)
    4. Imperative programming (BSc)
    5. Multiparadigm programming (MSc)
    6. Programming (C in English)
    7. Project tools (BSc)
    8. Software technology lab
    9. Theses proposals (BSc and MSc)
  3. Research
    1. CodeChecker
    2. CodeCompass
    3. Templight
    4. Projects
    5. Publications (up to 2011)
    6. PhD students
  4. Affiliations
    1. Dept. of Programming Languages and Compilers
    2. Ericsson Hungary Ltd

Classes

In this chapter we describe how to implement (non templated) classes in the C++ programming language. This chapter is not intended to teach basic object-oriented programming concepts,w e suppose that the reader is familiar with class, encapsulation, interface and implementation. In this chapter we teach how these can be implemented in C++.

Files

In C++ a non templated class is placed into two portions: a header file represents the knowledge the clients of the class should know about the class, and a source file, where the class methods and free functions related to the class are implemented. Some of the class methods are inline methods and implemented in the header file.

Clients of the class should include the class’s header file and they should link to the class’s object file compiled from the source. The implementation can be splitted into more sources, but the usual practice is to have one header file and one source file. (Class templates have a different schema: both the interface and implementation is placed to header file(s).)

The following picture describes the files for a class called XClass and a client called main.

 _________________________
|            -----------  |
|            | xclass.h | |
|            ------------ |
|            /         \  |
|           /       ___ \_|
|          /        |    \
|   --------------  |  ------------
|   | xclass.cpp |  |  | main.cpp |
|   --------------  |  ------------
|        |          |      |
|        |          |      |
|        |          |      |
|   -------------   |  ------------
|   |  xclass.o |   |  |  main.o  |
|   -------------   |  ------------
|________ \_________|     /
           \             /
            \           /
             \         /
              ---------
              | a.out |
              ---------

Members

A class is introduced by either the class or struct keyword. The only difference between using class or struct is the default visibility of the class.

Classes form a namespace with its own name. All members are declared inside this namespace. When membersof a class referred from inside a membre function the member names are visible by default (except they are hidden by some locally declared name). When members are referred outside of the class namespace (e.g. from an other class or from a namespace function) they should be qualified by the class name.

1
2
3
4
5
6
7
8
9
struct XClass
{
  void func();
  int  i;
};
void XClass::func()
{
  i = 5;  // here inside Xclass namespace "i" means "Xclass::i"
}

Language elements declared inside a class are called members. Members of a C++ class can be data members (or in other name: attributes), member functions (in C++ the expression method is sometimes restricted to virtual functions), type declarations and some other language elements, like constants, enums, etc.

Static data members and member functions has a special behaviour, we will discuss later.

Data members

The non static data members (sometimes called as attribute or field) form the memory area of the objects belonging to the class. Data members can be of any type visible in the class, including pointers, arrays, etc. The lifetime of the data members are the same as the object itself, the creation order is the same as the order of declaration (even when the constructor initializer list is different). As usual, the destruction order is the opposit of the construction order.

Class data members can be const. Const data members are immutable under the lifetime of the objects, but they might have different value in each objects.

Data members declared as mutable can be changed even for constant objects or inside contant member functions.

1
2
3
4
5
6
7
class XClass
{
            int  ifield; // data field 
            int *iptr;
  const     int  id;     // const, must be initialized
  mutable mutex  mut;    // mutable field
};

A data member declaration does not denote an actual memory area: it is rather an offset inside an object. It becames a real memory area only when a real object is defined, and the data member designates a certain part inside that object. As a consequence, normal pointers can not be set to a data member itself, it is only a member of a specific object can be pointed by a usual pointer.

There are, however, pointer to member which can be assigned to a member, with the meaning as the offset inside the object.

Member functions

The non static member functions are functions called on an object. In every non static member function a pointer to the actual object called this can be used. The body of a membre function is inside the namespace of the class, i.e. names used inside the function body are belonging primarily to the class namespace.

Member functions are implemented as “real” functions with a hidden extra first parameter: the this pointer. The this pointer is declared as a no-const pointer to the class for normal member functions, and as a pointer to const in constant member functions. The actual argument of the this parameter is set to a pointer to the actual object on which the member function is called. All the members (data or function) accessed inside a member function is accessed via the this pointer.

1
2
3
4
5
6
7
8
9
10
11
12
13
void XClass::func(int x)  // non-const member function
{
  // pointer this is declared here as "XClass *"
  ifield = 5; // same as this->ifield (if ifield is declared in XClass)   
  id = 6;     // ERROR: id is const member, must not changed
  mut.lock(); // m is XClass::m if m is declared in XClass
};
void XClass::func(int x) const  // const member function
{
  // pointer this is declared here as "const XClass *"
  ifield = 5; // ERROR: same as this->ifield, but this points to const  
  mut.lock(); // mut can be modified since it is mutable
};

This ensures two things:

  1. only constant member functions can be called on constant objects.

  2. inside a constant member function (whether it is called on a const or non-const object) no data member can be modified.

1
2
3
4
5
      XClass obj;  // non-const object
const XClass cobj; // const object

 obj.func(1);  // call XClass::f(int), passing this as XClass*
cobj.func(1);  // call XClass::f(int) const, passing this as const XClass*

it is usual to define the minimal necessary set of member functions on a class. The rest of the convenience operations can be defined as non member – so called – namespace function.

1
2
3
4
void print(const XClass &xc)
{
  // calling public methods on XClass
}

Operators defined on classes are discussed in the next chapter.

Visibility

There are three visibility categories of class members: public, protected and private. Each category is denoted with the label of the same keyword. Visibility categories can be repeated and may appear in any order. When the class is introduced by the class keyword, the default visibility area is private, in case of using struct it is public.

1
2
3
4
5
6
7
8
9
10
class XClass
{
  // private for class, public for stuct
public:
  // visible from everywhere
protected:
  // visible only for derived classes and friend
private:
  // visible only for class members and friends
};

There is a special way to access non-public data members and calling non-public member functions: friends. Friend functions have the unlimited access to all data members to the class. Friend classes are classes which have all members as friend functions to the class.

1
2
3
4
5
6
7
8
9
class XClass
{
  friend void nonMemberFunc();
  friend class OtherClass;
};
void nonMemberFunc()
{
  // can access all XClass members
}

We can place the friend declarations in any visibility sections.

Special member functions

There are special member functions in the class we can define.

Constructor

The constructor has the same name as the class. The constructor is called when a new object has been created and is responsible to initialize it. The programmer may define multiply constructors overloaded by their parameters. The one without parameters is called default constrator.

Copy constructor

The copy constructor has also the same name as the class but has a special signature: receives a single parameter of the same class by reference. The copy constructor is responsible to initialize the newly created object from an other object of the same class.

The signature of the copy constructor is usually const reference of the type to ensure that the source of the initialization is not modified, but there are exceptions: smart pointers may declare the parameter as non-const.

Assignment operator

An assignment operator is used when a new value is about to assigned to an existing object. The parameter list is optional, but it is usually the same as of the copy constructor. Although, the return value can be freely defined, the most design rules requires returning the freshly assigned object by reference.

Destructor

Destructor is an optional single method with no parameter. A destructor is called when an object goes out of life. A destructor is mainly repsonsible to automatically free allocated resources of the object.

Move constructor (since C++11)

Similar to the copy constructor but has a non-const right value reference parameter to initialize the object moving the value from the source (and such way stealing the resources from it).

Move assignment (since C++11)

Similar to assignment operator, but has a right value reference parameter to assing new value to an existing object moving the value from the source (and such way stealing the resources from it).

1
2
3
4
5
6
7
8
9
class XClass
{
  XClass();                          // default constructor
  XClass(const XClass &);            // copy constructor
  XClass(XClass &&);                 // move constructor since C++11
  XClass& operator=(const XClass &); // assingment operator
  XClass& operator=(XClass &&);      // move assingment operator since C++11
  ~XClass();                         // destructor
};

Special member functions need not to be defined. When a special member function is not defined the compiler generates one by the default memberwise copy semantic. Generation rules of the move special member functions are a bit more complex.

Constructors must not be declared as virtual function. Destructor can be, and in certain design must be defined as virtual function. We will discuss copy constructor and destructor in the chapter on POD classes.

Static members

Static members are global variables with static lifetime. They are placed into the namespace of a class for only logical purposes. Static data members are not phisically part of any objects: they are allocated outside of any objects. As their lifetime is static, they can be referred even when no object is exists of their class.

Inside the class deficition using the static keyword only declares the static data member. We must define it in exactly one source file (this is usually the one we implement the class methods).

Static member functions are global functions placed the namespace of the class. They also can be referred without existing objects of the class. Static member functions do not receive this parameter, therefore they must not refer to other members (data or function) without specifying the actual object.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class XClass
{
public:
  XClass *create();
private:
  XClass(int i) : id(nid++) { }  // not thread safe 
      const int id;
     static int nid;  // only declaration of static
  mutable mutex mut;
};
int XClass::nid = 0;  // definition of static

XClass *create()  // factory method: thread safe
{
  lock_guard<mutex> guard(mut);  // lock
  return new XClass();
} // unlock on dectructing guard

Even if static members are not phisically part of the objects can be declared public, private or protected. An example for public static member functon is a factory function, which role is to create objects of the class based on the _factory or abstract factory patterns. As static functions are members, they can access private members of their class.

An example class

Here we define a simple class to represent Date information storing year, month and day as integers and providing basic access and modification functions. Naturally, this class is just for demonstrational purposes: for real programs one should use std::chrono from the standard C++11 <chrono>.

First we place the declaration of the class into date.h header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef DATE_H
#define DATE_H

#include <iostream>
class Date
{
public:
  Date( int y, int m=1, int d=1); // constructor with default params

  int   getYear()  const;  // get year
  int   getMonth() const;  // get month
  int   getDay()   const;  // get day
  void  setDate( int y, int m, int d);   // set a date

  date&  next();       // set next date
  date&  add( int n);  // add n dates

  void get( std::istream& is);       // read a Date from stream
  void put( std::ostream& os) const; // write a Date to stream
private:
  void checkDate(int y, int m, int d); // check date, exit on fail
  int year;    // data members 
  int month;   // to represent
  int day;     // a date
};
#endif // DATE_H

Then we define the member functions in date.cpp source file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <cstdlib>

#include "date.h"

namespace /* anonym namespace, visible only in this source */
{
  const int day_in_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
}

Date( int y, int m, int d) : year(y), month(m), day(d) { }

void Date::setDate( int y, int m, int d)
{
  checkDate( y, m, d);
  year  = y;
  month = m;
  day   = d;
}
Date& Date::next()
{
  ++day;
  /* TODO: leap year */
  if ( day-1 == day_in_month[month-1])
  {
    day = 1;
    ++month;
  }
  if ( 13 == month )
  {
    month = 1;
    ++year;
  }
  return *this; // return reference to *this object 
}
Date& Date::add( int n)
{
  for (int i = 0; i < n; ++i)
  {
    next(); /* KISS */
  }
  return *this; // return reference to *this object 
}
void Date::get( std::istream& is)
{
  int y, m, d;
  if ( is >> y >> m >> d ) // have to check for success
  {
    setDate( y, m, d);
  }
}
void Date::put( std::ostream& os) const
{
  // better to build on accessors;
  os << "[ " << getYear()  << "."
             << getMonth() << "."
             << getDay()   << " ]";
}
void Date::checkDate( int y, int m, int d)
{
  // TODO: leap year
  if ( 0 == y )                         exit(1);
  if ( m < 1 || m > 12 )                exit(1);
  if ( d < 1 || d > day_in_month[m-1] ) exit(1);
}

To implement a correct version for the leap year problem is let to the reader as an excercise.

Financed from the financial support ELTE won from the Higher Education Restructuring Fund of the Hungarian Government.