News & UpdatesProgrammingWeb programming StoreMy Projects
Links
Affiliates

C++ Tutorial – 13 – Class

A class is a template used to create objects. To define one the class keyword is used followed by a name, a code block and a semicolon. The naming convention for classes is mixed case, meaning that each word should be initially capitalized.

class MyRectangle {};

Class members can be declared inside the class, the main two of which are fields and methods. Fields are variables and they hold the state of the object. Methods are functions and they define what the object can do.

class MyRectangle
{
  int x, y;
};

Class methods

A method belonging to a class is normally declared as a prototype inside of the class, and the actual implementation is placed after the class’s definition. The method’s name outside the class then needs to be prefixed with the class name and the scope resolution operator in order to designate to which class the method definition belongs.

class MyRectangle
{
  int x, y;
  int getArea();
};
 
int MyRectangle::getArea() { return x * y; }

Inline methods

If the method is short and you want to recommend to the compiler that the function’s code should be inserted (inlined) into the caller’s code, one way to do this would be to use the inline keyword in the method’s definition.

inline int MyRectangle::getArea() { return x * y; }

A more convenient way is to simply define the method inside of the class. This will implicitly recommend to the compiler that the method should be inlined.

class MyRectangle
{
  int x, y;
  int getArea() { return x * y; }
};

Object creation

The class definition is now complete. In order to use it you first have to create an object of the class, also called an instance. This can be done in the same way as variables are declared.

int main() 
{
  MyRectangle r; // object creation
}

Accessing object members

Before the members that this object contains can be accessed, they first need to be declared as public in the class definition, by using the public keyword followed by a colon.

class MyRectangle
{
 public:
  int x, y;
  int getArea() { return x * y; }
};

The members of this object can now be reached using the dot operator (.) after the instance name.

r.x = 10;
r.y = 5;
int z = r.getArea(); // 50 (5*10)

Any number of objects can be created based on a class, and each one of them will have its own set of fields and methods.

MyRectangle r2; // another instance of MyRectangle
r2.x = 25;      // not same as r.x

When using an object pointer, the arrow operator (->) allows access to the object’s members. This operator behaves as the dot operator, except that it dereferences the pointer first. It is used exclusively with pointers to objects.

MyRectangle r;
MyRectangle *p = &r; // object pointer
 
p->getArea();
(*p).getArea(); // alternative syntax

Forward declaration

Classes, just as functions, must be declared before they can be referenced. If there is a circular reference then changing the order in which the classes are defined will not solve the problem. Instead, what needs to be done in this situation is to declare a class prototype. This prototype declaration allows the class to be referenced before it is defined, as is done in the example below by using a pointer. Note that even with a prototype you still cannot create an object of a class before it has been defined.

class MyClass; // class prototype
 
class OtherClass
{
  MyClass* m;
};
 
class MyClass 
{
  OtherClass* o;  
};
Recommended additional reading:
Sams - Teach Yourself C++ in One Hour a Day