This article is part 0 in a series: C++ introduction



A class is a blueprint from which a object is created.
The object is an instance of a class.
That’s the general (and most simple) explanation of a class I give if anyone ask.

So, why use classes? what use are they? We got global functions (in lots of languages), why not just let everything be global functions?

Well, there are many reasons to use classes and a more object oriented approach to programming.
For example, by using classes and objects, one can save the hassle of rewriting code, you can organize all the code that is connected into a single class. A class allows you to create objects, and multiple object can be created from the same class. This makes it possible to create multiple instances with common shared traits but with different values in it.
Take for example a person. A person is always a person, but two persons don’t necessarily share all the same traits.
Two people can have different names, or different eye colours, there are a lot of things that are not the same between two people, but we also share common traits, we all need to eat, we all have a brain and a heart (if we live). A Person is always a person, but lots of the data in the person is not the same as that in another person.

There are a lot of other things that classes do and allow us to do, and I will try to cover that later.

A very basic class

Lets start with a small example.
(I will do this first example as a single snippet, ie, no .h or .cpp files and no includes etc, just a general example).

class Person {
    public:
        // Default constructor.
        Person() {
        }

        // Copy-constructor.
        Person(const Person& copy) {
        }

        // Destructor.
        ~Person() {
        }
}

int main() {
    // Object instantiation, using the default constructor.
    Person janeDoe;
    return 0;
}

Dissecting the class

The above code snippet first describes the class Person then, in the main function (entry point of the program) a Person object is created. This person class don’t really do anything yet, but we will implement more in it later.

A class declaration always start with they keyword class. In C++, there is also something called struct (structs exists in other languages too, but they do not always work the same way as in C++, this post only covers C++) that can be used, structs are basically the same thing as a class, and I will cover this later. After the class keyword, the name of the class is written. In this case Person.
The bracket after the Name shows that this is where the class declaration scope starts (with a end bracket at the end of the declaration).

Access specifiers

After the bracket, there is an access specifier, in this case its public. There are three types of access specifiers: public, private and protected.

Public means that stuff outside of the class can access the data that is declared after the keyword.
In our case, its the constructor of the Person class, which we want to be public. Why? Well, if the constructor is not public, we wont be able to create an instance of the class.

Private means that stuff outside the class can NOT access the data. Only instances of the class itself (and friends, which I might cover in a much later post) can access the data inside.

Protected is a specifier that is used when dealing with inheritance, I wont cover this here, but I will talk more about it whenever I write a post about inheritance!

Constructor

Default constructor

So what is a constructor? The constructor is a special function that is called when the class is about be instantiated. The constructor is always called, even if its not written, but it’s always a good idea to write a constructor so that you got full control over what is done within it.
The one we are currently using in our Person class is the so called default constructor. The default constructor does not take any parameters.

So when the Person class is created, our constructor is called.

Although, our constructor does nothing as of now…

Copy constructor

After the default constructor we have another type of constructor, the second constructor takes a const reference to a Person object as a parameter, this constructor is called a copy-constructor.
Its used when we create a instance as a copy of another instance.

Destructor

After the two constructors we have another function, it kinda looks like the constructor, but prefixed by a ~ character.
This is the destructor.
The destructor could be said to be the opposite of the constructor, its where the clean-up of the instance is made when the instance is destroyed (when a temp variable runs out of scope, when a pointer is deleted and such).
All memory allocated is supposed to be released in the destructor, if not, a memory leak is likely created.

A class should always have a default constructor, a copy-constructor and a destructor. They could have more than those, constructors with parameters if one so wish, but these will often be used even if they are not declared.
It’s best practice to write them, even though the program will compile without them.

And that’s a very very basic class!

This post have been moved from the old blog.
Original publish date is Feb 7 2015.