C++

Hello world in C++ is as follows.

/* Instead of using namespaces, it is actually better
practice to use std::cout, since namespaces bring
extra baggage */
#include <iostream>
using namespace std;

int main() {
    cout << "Hello World";
    return 0; // 0 means program executed successfully
    }

C++ is a compiled language, so we have to compile the source code above into an executable binary.

g++ hello.cpp
./a.out

Common types in C++ include bool, char, int, float, double. Strings are available via the string C++ standard library, else defined as arrays of type char similar to C. The size of these types can be returned by sizeof(bool) for example. We can use typedefs to create a new name for an existing type.

typedef int cents;
cents expenses_account;

C++ has storage classes that define the scope and visibility of variables and functions. They are auto, register, static, extern, mutable, with auto being the default, for example cents expenses_account is the same as auto cents expenses_account.

Global and local variables are defined outside and inside function definitions respectively. The former are automatically initialized by the compiler if not defined.

Arithmetic, relational, logical and bitwise are similar to most if not all other programming languages. Some other operators include:

Operator Explanation Example
C ? X : Y Returns X if C is true (x>5)?x:y
. and -> To reference individual members of classes, structures and unions  
& Returns address of a variable &a
* Pointer to a variable *a

Functions have the following form.

#include <iostream>
// set a default value to second argument
int add(int x, int y=10) {   // return type, name of function, arguments with types
    int sum = x+y;
    std::cout << sum;
    return sum;
    }

Function arguments are local variables inside the function, and are destroyed when the function exits. Inside the function, these variables are called formal parameters. By default, C++ uses call by value to pass arguments, which means the actual value of the arguments is copied to the formal parameters, and changes made to them inside the function do not affect the argument. This is contrast to call by reference which copies the reference (memory address) of an argument to the formal parameter, and this reference is used to access the actual argument, meaning changes made to it inside the function affect the argument. To pass by reference, use the & operator. A short example is given here.

#include <iostream>
// set a default value to second argument
int add(int & x, int y=10) {   // return type, name of function, arguments with types
    int sum = x+y;
    std::cout << sum;
    return sum;
    }

Arrays in C++ are declared for e.g. as double monthly_income[12]. They can be directly initialized as int marks[4] = {4,3,5,4,5}, or int marks[] = {4,3,5,4,5}.

Thoughts

  • Based mainly on TutorialsPoint.
  • TODO Global variables as bad practice

Author: Nazaal

Created: 2022-03-13 Sun 21:45

Validate