This syntax guide was designed with heavy inspiration from the Coding with Harry C++ syntax guide.

    

Basics

The basic start for a C++ program is:

#include <iostream>
using namespace std;

int main() {
    return 0;
}

Declaring variables:

bool my_bool;
bool my_bool_initialized = false;
char my_char;
char my_char_initialized = 'A';
int my_int;
int my_int_initialized = 1;
double my_double;
double my_double_initialized = 3.5;
float my_float;
float my_float_initialized = -2.1;
string my_string;
string my_string_initialized = "Hello!";

Terminal output:

cout << [statement to print];
cout << variable_contents;
cout << "Hello!";

Terminal input:

cin >> variable_input;
getline(cin, string_input);

Comments:

// It's a single line comment

/* It's a multi-linecomment*/

Compiling:

g++ -Wall -Werror -Wpedantic -std=c++17 file1.cpp file2.cpp ...

    

Decisions

Decisions are decided based off of boolean values. In this guide, any time < condition > appears, it should be replaced fully (including the angle brackets) with something that expresses to a boolean value.

If statements:

if ( < condition > )
{
    //code
}

If-Else statements:

if ( < condition > )
{
    //code
}
else
{
    //more code
}

If-Else-If statements:

if ( < condition > )
{
    //code
}
else if ( < condition2 > )
{
    //more code
}

Integer switch statements. The case values can vary:

switch(integer_variable){
    case 1:
        //commands
        break;
    case 2:
        //commands
        break;
    //any further cases
    default:
        //commands
}

Character switch statements. The case values can vary:

switch(character_variable){
    case 'A':
        //commands
        break;
    case 'B':
        //commands
        break;
    // ...
    // any further cases
    // ...
    default:
        //commands
}

    

Functions

Functions are small packages of code that can be called multiple times. The general format of a function requires a return type (which is a data type), a function name, and a list of parameter inputs, like so:

<return type> FunctionName(<parameter 1>, <parameter 2>, ...);

There can only be one return type and it must be a data type. It can be any data type you have seen so far, or it can also be “void” which means the function returns nothing. There can be as many parameters as you like, and they must be written as <parameter i> = <data type of parameter i> <name of parameter i>.

Function prototypes are just the function declaration followed by a semicolon.Here are a few function prototype examples:

int AddNumbers(int numOne, int numTwo);
void PrintMenu(string menu);
double calcCircum(double radius);

When implementing the function, it is instead written with brackets:

<return type> FunctionName(<parameter list>){
    /* code for function */
    return <something of return type>;
}

When calling the function, you provide the list of arguments:

int sum = AddNumbers(firstVal, secondVal);
sum = (4, 5);

    

Strings

Declaring strings:

string my_string = "Hello World";

Getting the length of strings:

int string_length = myString.length();

Appending two strings:

string first_name = "Harry ";
string last_name = "Potter";
string full_name = first_name.append(last_name);

Accessing or changing characters:

char first_character = my_string[0];
my_string[1] = 'a'; //my_string now stores "Hallo World"

    

Loops

While loops repeat code as long as a condition is true:

while (< condition >)
{
    /* code block to be executed */
}

Do-While loops will execute the block of code, and then execute it again as long as a condition is true:

do
{
    /* code block to be executed */
} while (< condition >);

For loops will execute a block of code a given number of times. This is done by creating a variable, modifying that variable and go until a particular condition is false.

for (<create and initialize variable>; <condition>; <modify variable>){
    /* code block to be executed */
}

For example, to count from 0 to 9:

for (int i = 0; i < 10; i++){
    /* code block to be executed */
}

The keyword break terminates a loop:

break;

The keyword continue skips the rest of the current iteration of the loop:

continue;

    

Objects

To define a class:

class Class_Name {
    public: // Access specifier
        // member functions

    private:
        //data members
};

To create an object of that class:

Class_Name object_name;

Constructors describe how a new object’s data members should be initialized. It does not have a return type. It must match the class name.

class Class_Name {
    public: // Access specifier
        Class_Name(); //default constructor
        Class_Name(<parameter list>); //parameterized constructor

    private:
        //data members
};

To call a member function on a particular object:

Class_Name object_name;
object_name.functionName();

    

Libraries

To use a library you need to know the library name and include it.

#include<LibraryName>
#include "libraryFileYouWrote.h"

   

Math

The library is called <cmath>.

The square root function:

double square_root = sqrt(169);

The power function (returns the value of x raised to the power y):

double power = pow(x, y);

   

File I/O

The library is called <fstream>.

Creating and writing to a text file:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    // Create and open a text file
    ofstream my_file("filename.txt");

    // Write to the file
    my_file << "File Handling in C++";

    // Close the file
    my_file.close();
}

Opening a file and reading one line:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    // Open a file to read from
    ifstream my_file("filename.txt");

    string file_line; 

    getline(my_file, file_line); //stores the first line in the file in file_line

    // Close the file
    my_file.close();
}