Book Image

The C++ Workshop

By : Dale Green, Kurt Guntheroth, Shaun Ross Mitchell
Book Image

The C++ Workshop

By: Dale Green, Kurt Guntheroth, Shaun Ross Mitchell

Overview of this book

C++ is the backbone of many games, GUI-based applications, and operating systems. Learning C++ effectively is more than a matter of simply reading through theory, as the real challenge is understanding the fundamentals in depth and being able to use them in the real world. If you're looking to learn C++ programming efficiently, this Workshop is a comprehensive guide that covers all the core features of C++ and how to apply them. It will help you take the next big step toward writing efficient, reliable C++ programs. The C++ Workshop begins by explaining the basic structure of a C++ application, showing you how to write and run your first program to understand data types, operators, variables and the flow of control structures. You'll also see how to make smarter decisions when it comes to using storage space by declaring dynamic variables during program runtime. Moving ahead, you'll use object-oriented programming (OOP) techniques such as inheritance, polymorphism, and class hierarchies to make your code structure organized and efficient. Finally, you'll use the C++ standard library?s built-in functions and templates to speed up different programming tasks. By the end of this C++ book, you will have the knowledge and skills to confidently tackle your own ambitious projects and advance your career as a C++ developer.
Table of Contents (15 chapters)

C++ Keywords

Keywords are words that are reserved by C++. Thus, we cannot use them in our applications for anything other than their intended purposes. For example, a common keyword is if, so you would not be able to define a variable or function of that name. It's these keywords that structure the C++ language, and it's through their use that we instruct our program on what it should be doing.

There are many keywords defined in the language, and covering them all at this early stage is not necessary. Instead, let's take a look at the keywords that we'll encounter over the coming chapters.

Some of these words define basic types, (bool, char, int, and so on), some of them are statements to define program flow (if, else, switch, and so on), and others define objects and scope (class, struct, namespace, and so on).

We'll be using these throughout the book, but for now we just need to know that these words are reserved by C++. You'll be able to tell because most modern text editors will highlight these words thereby making them stand out. Let's take a look at how keywords are distinguished in our code editor. Observe the following program:

// Keywords example.
#include <iostream>
#include <string>
int main() 
{
    // Data type keywords.
    int myInt = 1;
    double myDouble = 1.5;
    char myChar = 'c';
    bool myBool = true;
    // Program flow keywords.
    if (myBool) 
    {
        std::cout << "true";
    } 
    else 
    {
        std::cout << "false";
    }
    struct myStruct 
    {
        int myInt = 1;
    };
}

On the compiler window, the preceding code would appear as follows:

Figure 1.4: Keywords and their highlighting

Figure 1.4: Keywords and their highlighting

We can see that the keywords in this program are given special presentation in the editor, usually a different color, to denote their status. This will differ between IDEs.

Note

IDE stands for Integrated Development Environment and is the software that we use to develop our applications. Example IDEs include Visual Studio and CLion.

Keyword Examples

Running through each keyword individually isn't necessary. We'll cover them as we go, but we can quickly take a look at some common keyword groups and what they do.

Type keywords denote the basic variable types provided by C++. These include int, bool, char, double, and float:

    int myInt = 1;
    char myChar = 'a';
    bool myBool = true;
    double myDouble = 1.5;
    float myFloat = 1.5f;

Program flow keywords allow us to structure the logic of the application. These include if, else, then, and switch, as shown in the following snippet:

    if (expression)
    {
        // do this
    }
    else
    {
        // do this instead.
    }

Access modifiers determine what other classes and components can and can't see our C++ variables and functions. When building classes (something we'll look at shortly) we have three to choose from: public, protected, and private. The correct use of these modifiers plays a big part in building robust systems, ensuring our data and functionality isn't open to abuse or dangerous misuse. Here is an example:

class MyClass()
{
public:
    int var1; // Accessible to the class, everything that can see MyClass.
protected:
    int var2; // Accessible to the class, and any child classes.
private:
    int var3; // Accessible to the class only.
}

Modifier types change the properties of our variables. These include const, static, signed, and unsigned. By putting these in front of our variables and functions, we can change the way they behave in our application, as shown in the following example:

    unsigned int var1 = 1;      // Unsigned means it can only be positive.
    signed int var2 = -1;      // Signed can be both positive or negative.
    const std::string var3 = "Hello World"; // Const means the value                                             // cannot be modified
    static char var4 = 'c';     // Static means the value is shared                                 // between all instances of a given class.