Basics

In the previous blogs I have just dived straight into programming in C++. This blog is going to focus around getting a better understanding of the language itself, including the various components that make up the language. It will cover the basics so for an experienced C++ programmer this blog will not be very interesting. If you are like me and new to C++ however, this blog may be beneficial.


Standard C++ File Structure

I found a great stackoverflow post on directory structure for a C++ library, which covers this topic. According to the post the file structure is as follows.

It is good to note what the person states about choose a directory layout that makes sense for you (and your team). Do whatever is most sensible for your chosen development environment, build tools, and source control.


Fundamental Types

Sourced from cppreference fundamental types.



Assignment Operators

Sourced from cppreference assignment operators


Increment/Decrement Operators

Sourced from cppreference increment/decrement operators


Arithmetic Operators

Sourced from cppreference arithmetic operators


Logical Operators

Source from cppreference logical operators


Comparison Operators

Sourced from cppreference comparison operators


Member Access Operators

Sourced from cppreference member access operators


Other Operators

Sourced from cppreference other operators


If Statements

if (a > b) {
    // ...
}
if (a > b) {
    // ...
} else {
    // ...
}
if (a > b) {
    // ...
} else if (a < b) {
    // ...
} else {
    // ...
}
int result = (a > b) ? true : false;

Switch Statements

int op = 3;
switch (op) {
    case 0:
        cout << "+";
    case 1:
        cout << "-";
    case 2:
        cout << "*";
    case 3:
        cout << "/";
    case 4:
        cout << "%";
}

Loop Statements

int i = 0;
while (i < 7) {
    i++;
}
int i = 0;
do {
    i++;
} while (i < 7);
for (int i = 0; i < 7; i++) {
    // ...
}

Pointers and References

int v = 42;
int* ptr = &v;

std::cout << ptr << std::endl; // Outputs the memory address of v (0x7ffdf796773c)
std::cout << *ptr << std::endl; // Outputs the value of v
int v = 42;

std::cout << &x << std::endl; // Outputs the memory address of x (0x7fffe6d57bb4)

Classes and Objects

#include <iostream>

// Base class
class Person
{
    private: // Access specifier

        std::string name;   // (string) Attribute
        int age;            // (int) Attribute

    public: // Access specifier

        /* Getters and Setters */
        void setName(std::string n)
        {
            this->name = n;
        }

        std::string getName()
        {
            return this->name;
        }

        void setAge(int a)
        {
            this->age = a;
        }

        int getAge()
        {
            return this->age;
        }

        /* Methods */
        void printInfo(); // Method declaration inside the class

        void speak(std::string word) // Method definition inside the class
        {
            std::cout << word << std::endl;
        }

        /* Constructors */
        Person(){}; // Default constructor

        // Constructor declaration inside the class
        Person(std::string name, int age); 
};

// Constructor definition outside the class
Person::Person(std::string name, int age)
{
    this->setName(name);
    this->setAge(age); //age = age; // Causes bug
}

// Method definition outside the class
void Person::printInfo()
{
    std::cout << "My name is " << getName() << std::endl;
    std::cout << "My age is " << getAge() << std::endl;
}

// Subclass that inherits from Person
class Programmer: public Person
{
    private:
        std::string language;

    public:
        /* Getters and Setters */
        void setLanguage(std::string l)
        {
            this->language = l;
        }

        std::string getLanguage()
        {
            return this->language;
        }

        /* Methods */
        void cry()
        {
            std::cout << "Why wont you work :*(\n";
        }
 
        void printInfo()
        {
            std::cout << "My name is " << getName() << std::endl;
            std::cout << "My age is " << getAge() << std::endl;
            std::cout << "My programming language is " << getLanguage();
        }

        /* Constructors */
        Programmer(){};
        Programmer(std::string name, int age, std::string language)
        {
            this->setName(name);
            this->setAge(age);
            this->setLanguage(language);
        };
};

int main()
{
    Person manager("Bob", 23); // Person object using constructor with params
    manager.speak("Hello world!");
    manager.printInfo();

    Programmer cppProgrammer; // Programmer object using default constructor
    cppProgrammer.setName("Alice");
    cppProgrammer.setAge(33);
    cppProgrammer.setLanguage("C++");
    cppProgrammer.printInfo();
    cppProgrammer.cry();

    return 0;
}