01 - introduction

So far you have used built-in types and some of the user-defined types provided in the C++ standard library. You have also learned how to create simple user-defined types - structures and enumerations. This chapter is dedicated towards a much more powerful type mechanism - classes, which are a very significant building block in OOP (object-oriented programming). OOP is all about representing real-world things as objects that can have certain states and (potentially shared) behaviors.

C++ is a multi-paradigm language. OOP is one of its bigger features, but it's not required - in contrast, some languages force OO style of programming and their "hello world"s can not be written without creating a class. This conflicts with C++ philosophy and the goal that you don't pay for what you don't use. Most of the C++ standard library uses generic programming rather than OOP. Some of the most powerful design techniques combine aspects of traditional object-oriented programming, generic programming, functional programming, and some traditional imperative techniques[1].

This lesson is a more philosophical than others but further ones in this chapter showcase a lot of new code. It's important to not only understand how, but also why. You can perfectly memorize all OOP rules - most of them are very intuitive; write any program that satisfies given requirements but whether the code will be a good code is a very different topic. This is the place where you will realize you have almost limitless ways to complete a task and the true art of programming is in making choices what features to use and how. Common combinations of specific features to solve popular problems are known as design patterns.

Classes

So what exactly is a class? In simple terms, it's a container for closely related variables (called fields) which are intended to be used together. There can be class-specific functions (called methods) which are specifically designed to work on objects of the class type. These together with other class-specific features tie data with operations to create very reusable building blocks for bigger programs.

Classes can improve code readability and offer modular design. They also feature a slightly different syntax:

1
2
set_text(button, "hello"); // structural
button.set_text("hello");  // object-oriented

Isn't the second line more intuitive?

  • The structural approach uses a global function which takes (a reference to) a button and the text to set.

  • The OO approach uses a class-specific function which takes only the text to set. Method is invoked on the button object - note how . has to be used to access it. This is because methods are tied to classes and therefore have to be invoked on objects of certain types.

class vs struct

Does it mean that structures defined in previous lessons were technically classes?

Yes - C++ does not differentiate classes and structures. It's one thing that can be defined using 2 keywords - only default access is different. More on access in later lessons.

The main reason for this convention is clear expression of intent . In C, there is no class and struct has very limited functionality compared to C++. Thus if struct is used within C++, it's usually only for functionality that is available in C.

Access specifiers

Classes can limit access to their members (entities defined inside). For struct everything by default is public which means there are no constraints. Because of different default in classes, in order to have the same behavior as with struct, public has to be written explicitly. In the code below all definitions are equivalent.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct point
{
// (implicitly public)
	int x;
	int y;
};

struct point
{
public:
	int x;
	int y;
};

class point
{
public:
	int x;
	int y;
};