DEV Community

Cover image for Learn C++: An Introduction for Beginners
Godot
Godot

Posted on • Originally published at godot.community

Learn C++: An Introduction for Beginners

C++ is a powerful, versatile, and widely-used programming language that has remained relevant in the world of software development for over three decades. In this short post, we'll introduce you to the basics of C++ and help you build a strong foundation to embark on your programming journey.

Brief history of C++

C++ was created by Bjarne Stroustrup in 1985 as an extension of the C programming language. Stroustrup wanted to add object-oriented features to C, which led to the birth of C++. Since then, C++ has undergone several revisions and updates, with the most recent version being C++20.

Importance of C++ in modern programming

C++ is an essential language in today's programming landscape for several reasons:

  • It is highly efficient and provides fine-grained control over system resources.
  • It supports both procedural and object-oriented programming paradigms.
  • It has a vast standard library (STL) that simplifies complex tasks.
  • It is widely used in industries such as game development, embedded systems, and high-performance computing.

Overview of the post

This post is divided into several sections, each focusing on key aspects of C++ programming:

  1. Setting up the environment
  2. Basic C++ syntax
  3. Functions
  4. Object-oriented programming
  5. Standard Template Library (STL)
  6. Input and output
  7. Error handling
  8. Advanced topics
  9. Best practices and debugging

By the end of this post, you'll have a solid understanding of C++ fundamentals and be ready to tackle more advanced topics.

Setting up the environment

Before we start coding, let's set up our development environment.

Choosing an IDE

An Integrated Development Environment (IDE) is a software application that provides a comprehensive set of tools for coding, debugging, and building software. Some popular C++ IDEs include:

  • Visual Studio: A powerful and feature-rich IDE from Microsoft.
  • Code::Blocks: A free, open-source, and cross-platform IDE.
  • CLion: A commercial IDE from JetBrains with advanced features and excellent support.

Choose the IDE that best fits your needs and preferences.

Installing the compiler

A compiler is a program that translates your C++ code into machine code, which can then be executed by a computer. Some common C++ compilers are:

  • GCC: The GNU Compiler Collection is a widely-used, open-source compiler that supports multiple platforms.
  • Clang: A compiler based on LLVM, known for its fast compilation times and helpful error messages.
  • Microsoft Visual C++: A compiler included with Visual Studio, optimized for Windows development.

Install the compiler that is compatible with your chosen IDE and operating system.

Creating your first C++ project

Once you have your IDE and compiler set up, it's time to create your first C++ project. Follow these general steps:

  1. Open your chosen IDE and create a new project.
  2. Select "C++" as the project type.
  3. Choose a name and location for your project.
  4. Add a new C++ source file to your project (usually with a .cpp extension).

Basic C++ syntax

Now that we have our environment set up, let's dive into the basics of C++ syntax.

Structure of a C++ program

A typical C++ program consists of the following elements:

  • Preprocessor directives: These are commands that instruct the compiler to perform specific tasks before compiling the code. For example, #include is used to include header files.
  • Function declarations and definitions: These are used to declare and define functions, which are blocks of code that perform specific tasks.
  • Variables and data types: These are used to store and manipulate data in your program.
  • Control structures: These are used to control the flow of your program, such as if, else, and switch statements.
  • Loops: These are used to repeatedly execute blocks of code, such as for, while, and do-while loops.

Here's a simple example of a C++ program:

#include <iostream>

int main() {
  std::cout << "Hello, World!" << std::endl;
  return 0;
}
Enter fullscreen mode Exit fullscreen mode

Variables and data types

In C++, we use variables to store and manipulate data. Each variable has a specific data type that determines the kind of data it can hold. Some common C++ data types are:

  • int: Integer values (e.g., -2, 0, 42)
  • float: Single-precision floating-point values (e.g., 3.14f, -0.001f)
  • double: Double-precision floating-point values (e.g., 3.141592653589793, -0.000001)
  • char: Single characters (e.g., 'a', 'Z', '9')
  • bool: Boolean values (true or false)

Here's an example of declaring and initializing variables in C++:

int age = 30;
float weight = 65.5f;
double pi = 3.141592653589793;
char initial = 'A';
bool is_adult = true;
Enter fullscreen mode Exit fullscreen mode

Operators and expressions

Operators are special symbols that perform operations on operands (variables or values). Some common C++ operators are:

  • Arithmetic operators: +, -, *, /, %
  • Relational operators: <, >, <=, >=, ==, !=
  • Logical operators: &&, ||, !

Expressions are combinations of variables, values, and operators that result in a single value. Here's an example of using operators and expressions in C++:

int a = 10;
int b = 20;
int sum = a + b;
bool is_greater = a > b;
Enter fullscreen mode Exit fullscreen mode

Control structures (if, else, switch)

Control structures are used to control the flow of your program based on certain conditions. Some common C++ control structures are:

  • if: Executes a block of code if a specified condition is true.
  • else: Executes a block of code if the condition in the preceding if statement is false.
  • switch: Executes a block of code based on the value of a specified expression.

Here's an example of using control structures in C++:

int x = 42;

if (x > 0) {
  std::cout << "x is positive" << std::endl;
} else {
  std::cout << "x is non-positive" << std::endl;
}

switch (x % 2) {
  case 0:
    std::cout << "x is even" << std::endl;
    break;
  case 1:
    std::cout << "x is odd" << std::endl;
    break;
}
Enter fullscreen mode Exit fullscreen mode

Loops (for, while, do-while)

Loops are used to repeatedly execute blocks of code based on certain conditions. Some common C++ loops are:

  • for: Executes a block of code a specified number of times.
  • while: Executes a block of code as long as a specified condition is true.
  • do-while: Executes a block of code at least once and then repeatedly as long as a specified condition is true.

Here's an example of using loops in C++:

// for loop
for (int i = 0; i < 10; ++i) {
  std::cout << i << std::endl;
}

// while loop
int count = 0;
while (count < 10) {
  std::cout << count << std::endl;
  ++count;
}

// do-while loop
int num;
do {
  std::cout << "Enter a positive number: ";
  std::cin >> num;
} while (num <= 0);
Enter fullscreen mode Exit fullscreen mode

Functions

Functions are blocks of code that perform specific tasks and can be called by name. They can accept input (parameters) and return output (return values).

Defining and declaring functions

Functions are defined using the following syntax:

return_type function_name(parameter_list) {
  // function body
}
Enter fullscreen mode Exit fullscreen mode

For example, here's a simple function that adds two numbers and returns the result:

int add(int a, int b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Function declarations (also called prototypes) are used to inform the compiler about a function's existence before it is defined. They have the following syntax:

return_type function_name(parameter_list);
Enter fullscreen mode Exit fullscreen mode

For example, here's the declaration for the add function:

int add(int a, int b);
Enter fullscreen mode Exit fullscreen mode

Function parameters and return values

Function parameters are variables that hold the input values passed to the function. They are specified in the function definition and declaration.

Return values are the output values produced by the function. They are specified using the return statement followed by an expression or value.

Here's an example of a function that calculates the area of a rectangle:

double area(double length, double width) {
  return length * width;
}
Enter fullscreen mode Exit fullscreen mode

Function overloading

Function overloading is the ability to define multiple functions with the same name but different parameter lists. The compiler chooses the appropriate function to call based on the number and types of arguments passed.

Here's an example of function overloading:

int add(int a, int b) {
  return a + b;
}

double add(double a, double b) {
  return a + b;
}
Enter fullscreen mode Exit fullscreen mode

Recursion

Recursion is the process of a function calling itself, either directly or indirectly. It is often used to solve problems that can be broken down into smaller, similar problems.

Here's an example of a recursive function that calculates the factorial of a number:

int factorial(int n) {
  if (n <= 1) {
    return 1;
  }
  return n * factorial(n - 1);
}
Enter fullscreen mode Exit fullscreen mode

Object-oriented programming

Object-oriented programming (OOP) is a programming paradigm that focuses on organizing code into "objects" that represent real-world entities. It is built on the concepts of classes, objects, inheritance, and polymorphism.

Introduction to OOP concepts

The main concepts in OOP are:

  • Classes: Templates for creating objects, consisting of data members and member functions.
  • Objects: Instances of classes, representing real-world entities.
  • Inheritance: The process of creating new classes that inherit the properties and methods of existing classes.
  • Polymorphism: The ability of a function or method to operate on multiple types of data or objects.

Classes and objects

Classes are defined using the class keyword, followed by the class name and a set of braces containing the class's data members and member functions.

Objects are instances of classes, created using the class's constructor.

Here's an example of a class definition and object creation:

class Dog {
public:
  std::string name;
  int age;

  void bark() {
    std::cout << "Woof!" << std::endl;
  }
};

int main() {
  Dog my_dog;
  my_dog.name = "Buddy";
  my_dog.age = 3;
  my_dog.bark();
}
Enter fullscreen mode Exit fullscreen mode

Constructors and destructors

Constructors are special member functions that are called when an object is created. They are used to initialize the object's data members. Destructors are special member functions that are called when an object is destroyed. They are used to perform any necessary cleanup.

Here's an example of a class with constructors and a destructor:

class Dog {
public:
  std::string name;
  int age;

  // Default constructor
  Dog() : name("Unnamed"), age(0) {}

  // Parameterized constructor
  Dog(std::string n, int a) : name(n), age(a) {}

  // Destructor
  ~Dog() {
    std::cout << "Dog destroyed" << std::endl;
  }

  void bark() {
    std::cout << "Woof!" << std::endl;
  }
};
Enter fullscreen mode Exit fullscreen mode

Inheritance

Inheritance is the process of creating new classes that inherit the properties and methods of existing classes. It allows for code reuse and modular design.

Here's an example of inheritance in C++:

class Animal {
public:
  std::string name;
  int age;

  void make_sound() {
    std::cout << "Generic animal sound" << std::endl;
  }
};

class Dog : public Animal {
public:
  void make_sound() {
    std::cout << "Woof!" << std::endl;
  }
};
Enter fullscreen mode Exit fullscreen mode

Polymorphism

Polymorphism is the ability of a function or method to operate on multiple types of data or objects. It allows for more flexible and modular code.

Here's an example of polymorphism in C++:

class Animal {
public:
  virtual void make_sound() {
    std::cout << "Generic animal sound" << std::endl;
  }
};

class Dog : public Animal {
public:
  void make_sound() {
    std::cout << "Woof!" << std::endl;
  }
};

class Cat : public Animal {
public:
  void make_sound() {
    std::cout << "Meow!" << std::endl;
  }
};

int main() {
  Animal *animals[] = {new Dog(), new Cat()};
  for (Animal *animal : animals) {
    animal->make_sound();
  }
}
Enter fullscreen mode Exit fullscreen mode

Standard Template Library (STL)

The Standard Template Library (STL) is a collection of template classes and functions that provide common data structures and algorithms for C++ programming.

Overview of STL

The main components of the STL are:

  • Containers: Data structures that store and organize data (e.g., vector, list, map).
  • Iterators: Objects that provide a way to access and traverse container elements.
  • Algorithms: Functions that perform operations on containers (e.g., sorting, searching).

Containers (vector, list, map, etc.)

Containers are data structures that store and organize data. Some common STL containers are:

  • vector: A dynamic array that can grow or shrink in size.
  • list: A doubly-linked list that allows for efficient insertions and deletions.
  • map: An associative container that stores key-value pairs, with unique keys.

Here's an example of using a vector container in C++:

#include <vector>

int main() {
  std::vector<int> numbers = {1, 2, 3, 4, 5};

  numbers.push_back(6); // Add an element to the end
  numbers.pop_back(); // Remove the last element
  int first = numbers.front(); // Get the first element
  int last = numbers.back(); // Get the last element
}
Enter fullscreen mode Exit fullscreen mode

Iterators

Iterators are objects that provide a way to access and traverse container elements. They are similar to pointers but are more flexible and can work with different container types.

Here's an example of using iterators with a vector container:

#include <vector>

int main() {
  std::vector<int> numbers = {1, 2, 3, 4, 5};
  std::vector<int>::iterator it;

  for (it = numbers.begin(); it != numbers.end(); ++it) {
    std::cout << *it << std::endl;
  }
}
Enter fullscreen mode Exit fullscreen mode

Algorithms

Algorithms are functions that perform operations on containers, such as sorting, searching, and transforming data.

Here's an example of using the sort algorithm with a vector container:

#include <vector>
#include <algorithm>

int main() {
  std::vector<int> numbers = {5, 3, 1, 4, 2};
  std::sort(numbers.begin(), numbers.end()); // Sort the vector in ascending order
}
Enter fullscreen mode Exit fullscreen mode

Input and output

C++ provides several ways to handle input and output, including console input/output, file input/output, and string manipulation.

Console input and output

Console input and output are performed using the cin and cout objects, which are part of the iostream library. They allow you to read and write data to and from the console.

Here's an example of using console input and output:

#include <iostream>

int main() {
  int age;
  std::cout << "Enter your age: ";
  std::cin >> age;
  std::cout << "You are " << age << " years old" << std::endl;
}
Enter fullscreen mode Exit fullscreen mode

File input and output

File input and output are performed using the ifstream and ofstream classes, which are part of the fstream library. They allow you to read and write data to and from files.

Here's an example of using file input and output:

#include <fstream>
#include <iostream>

int main() {
  std::ofstream outfile("output.txt");
  outfile << "Hello, file!" << std::endl;
  outfile.close();

  std::ifstream infile("output.txt");
  std::string line;
  getline(infile, line);
  std::cout << line << std::endl;
  infile.close();
}
Enter fullscreen mode Exit fullscreen mode

String manipulation

String manipulation is performed using the string class, which is part of the string library. It provides various functions and operators for working with strings.

Here's an example of using string manipulation:

#include <string>
#include <iostream>

int main() {
  std::string greeting = "Hello, World!";
  std::string name = "John";
  std::string personalized_greeting = greeting + " " + name;
  std::cout << personalized_greeting << std::endl;
}
Enter fullscreen mode Exit fullscreen mode

Error handling

Error handling is an essential aspect of programming that helps you deal with unexpected situations and prevent your program from crashing. In C++, error handling is primarily done using exceptions.

Exceptions

Exceptions are events that occur during the execution of a program when an error is encountered. They are thrown using the throw keyword and caught using try and catch blocks.

Here's an example of using exceptions for error handling:

#include <iostream>

double divide(double a, double b) {
  if (b == 0) {
    throw "Division by zero";
  }
  return a / b;
}

int main() {
  try {
    double result = divide(10, 0);
    std::cout << "Result: " << result << std::endl;
  } catch (const char *error) {
    std::cout << "Error: " << error << std::endl;
  }
}
Enter fullscreen mode Exit fullscreen mode

Try, catch, and throw

The try, catch, and throw keywords are used to handle exceptions in C++.

  • try: Encloses a block of code that might throw an exception.
  • catch: Defines a block of code that handles a specific type of exception.
  • throw: Throws an exception when an error is encountered.

Custom exception classes

You can create custom exception classes by inheriting from the std::exception class and overriding its what() method.

Here's an example of creating a custom exception class:

#include <iostream>
#include <stdexcept>

class DivisionByZeroException : public std::exception {
public:
  const char *what() const noexcept override {
    return "Division by zero";
  }
};

double divide(double a, double b) {
  if (b == 0) {
    throw DivisionByZeroException();
  }
  return a / b;
}

int main() {
  try {
    double result = divide(10, 0);
    std::cout << "Result: " << result << std::endl;
  } catch (const DivisionByZeroException &ex) {
    std::cout << "Error: " << ex.what() << std::endl;
  }
}
Enter fullscreen mode Exit fullscreen mode

Advanced topics

Now that we've covered the fundamentals, let's dive into some advanced C++ topics.

Pointers and memory management

Pointers are variables that store the memory address of another variable. They are used to access and manipulate memory directly, providing greater flexibility and control over your program's resources.

Here's an example of using pointers in C++:

int x = 42;
int *p = &x; // p points to the memory address of x
*p = 10; // Change the value of x through the pointer
Enter fullscreen mode Exit fullscreen mode

Memory management is the process of allocating and deallocating memory as needed by your program. In C++, you can use the new and delete operators to allocate and deallocate memory dynamically.

Here's an example of memory management in C++:

int *p = new int; // Allocate memory for an int
*p = 42; // Assign a value to the memory
delete p; // Deallocate the memory
Enter fullscreen mode Exit fullscreen mode

Templates

Templates are a powerful feature of C++ that allows you to write generic code that can work with different data types. They are used to create reusable classes and functions that can operate on any data type.

Here's an example of using templates in C++:

template<typename T>
T add(T a, T b) {
  return a + b;
}

int main() {
  int x = add<int>(1, 2);
  double y = add<double>(3.14, 2.71);
}
Enter fullscreen mode Exit fullscreen mode

Lambda expressions

Lambda expressions are a concise way to create anonymous functions (functions without a name). They are useful for creating short, one-time-use functions that can be passed as arguments to other functions.

Here's an example of using a lambda expression in C++:

#include <algorithm>
#include <vector>

int main() {
  std::vector<int> numbers = {5, 3, 1, 4, 2};
  std::sort(numbers.begin(), numbers.end(),
    [](int a, int b) { return a > b; }); // Sort in descending order using a lambda
}
Enter fullscreen mode Exit fullscreen mode

Multithreading

Multithreading is the process of executing multiple threads concurrently, allowing your program to perform multiple tasks simultaneously. C++ provides the <thread> library to work with threads.

Here's an example of using multithreading in C++:

#include <iostream>
#include <thread>

void print_hello() {
  std::cout << "Hello from thread!" << std::endl;
}

int main() {
  std::thread t(print_hello);
  t.join(); // Wait for the thread to finish
}
Enter fullscreen mode Exit fullscreen mode

Best practices and debugging

Writing clean, efficient, and maintainable code is essential for any programmer. In this section, we'll discuss best practices and debugging techniques for C++ programming.

Code organization

Organizing your code into separate functions, classes, and files makes it easier to read, understand, and maintain.

  • Functions: Break your code into small, reusable functions that perform specific tasks.
  • Classes: Organize related data and functions into classes to model real-world entities.
  • Files: Separate your code into different files based on their functionality, and use header files for declarations.

Naming conventions

Using consistent naming conventions for variables, functions, and classes makes your code easier to read and understand.

  • Variables: Use lowercase letters and underscores (e.g., my_variable).
  • Functions: Use lowercase letters and underscores (e.g., my_function).
  • Classes: Use PascalCase (e.g., MyClass).

Debugging techniques

Debugging is the process of finding and fixing errors in your code. Some common debugging techniques include:

  • Code review: Review your code for syntax errors, logic errors, and other issues.
  • Print statements: Add print statements to your code to display variable values and track the program's execution.
  • Breakpoints: Use breakpoints in your IDE to pause the execution of your program and inspect its state.
  • Step-through debugging: Use step-through debugging in your IDE to execute your program one line at a time and observe its behavior.

Performance optimization

Optimizing your code for performance can help your program run faster and use fewer resources. Some general performance optimization tips include:

  • Use efficient algorithms: Choose algorithms with lower time complexity to reduce the execution time of your program.
  • Minimize memory usage: Use appropriate data structures and memory allocation techniques to minimize memory usage.
  • Profile your code: Use profiling tools to identify performance bottlenecks in your code and optimize them.

Conclusion

Congratulations! You've now learned the fundamentals of C++ programming, including setting up your development environment, understanding basic syntax, working with functions and classes, using the Standard Template Library, handling input and output, and more.

Review of key concepts

Here's a quick review of some key concepts we covered:

  • C++ is a powerful, versatile, and widely-used programming language.
  • It supports both procedural and object-oriented programming paradigms.
  • The Standard Template Library (STL) provides a collection of template classes and functions for common data structures and algorithms.
  • Error handling in C++ is primarily done using exceptions.
  • Advanced topics in C++ include pointers, memory management, templates, lambda expressions, and multithreading.

Resources for further learning

To continue learning and expanding your C++ knowledge, consider the following resources:

Next steps in your C++ journey

Now that you have a solid foundation in C++, you can start exploring more advanced topics, such as:

  • Graphical user interface (GUI) programming with libraries like Qt or GTK.
  • Game development with engines like Godot Engine, Unreal Engine or Unity (with C++ scripting).
  • Networking and web development with libraries like Boost.Asio or Poco.
  • Embedded systems and IoT programming with platforms like Arduino and Raspberry Pi.

Keep practicing and building projects, and you'll become a proficient C++ programmer in no time!

Top comments (0)