No history yet

Introduction to C++

What Is C++?

C++ began its life in the early 1980s as an extension of the C programming language. A Danish computer scientist named Bjarne Stroustrup wanted to add features from another language he admired, Simula, into the powerful and efficient C language. His goal was to bring object-oriented programming capabilities to C, allowing developers to build larger, more complex applications more easily.

The result was initially called "C with Classes." It was officially renamed C++ in 1983. The "++" is a clever nod to the increment operator in C, which adds one to a variable's value. This name signifies that C++ is an incremental improvement upon C.

Over the decades, C++ has evolved continuously. It's managed by an international standards committee that releases updates every few years, adding modern features while maintaining backward compatibility. This evolution has kept it relevant for building high-performance applications, from video games and web browsers to operating systems and financial trading software.

Lesson image

Core Characteristics

C++ is a compiled language. This means that before your program can run, a special program called a compiler reads your source code and translates it into machine code that the computer's processor can execute directly. This is a key reason for its high performance, as the translation happens once, ahead of time.

It is also a statically-typed language. In simple terms, this means you must declare the type of each variable (like an integer, a character, or some text) and the compiler checks for type errors before the program even runs. This helps catch bugs early in the development process.

Finally, C++ is known as a multi-paradigm language. It doesn't force you into one style of programming. You can write in a simple, step-by-step procedural style like C, or you can use object-oriented, generic, or even functional programming paradigms. This flexibility is one of its greatest strengths.

Anatomy of a Program

The traditional first program for anyone learning a new language is "Hello, World!" It's a simple program that just prints that phrase to the screen. Let's look at the C++ version and break it down piece by piece.

// This is a simple C++ program.

// Include the input/output stream library
#include <iostream>

// The main function is where the program starts
int main() {
    // Print "Hello, World!" to the console
    std::cout << "Hello, World!" << std::endl;

    // Return 0 to indicate success
    return 0;
}

Let's dissect this simple program.

#include <iostream>: This is a preprocessor directive. It tells the compiler to include the contents of the iostream header file. This file contains the declarations for input and output operations, like printing to the console. Without it, the compiler wouldn't know what std::cout is.

int main(): This is the main function. Every C++ program must have a main function, as it's the official starting point for execution. When you run your program, the code inside the curly braces {} of main is what gets executed. The int at the beginning means the function is expected to return an integer value when it finishes.

std::cout << "Hello, World!" << std::endl;: This line does the actual work.

  • std::cout is the standard character output stream, which usually directs to the console.
  • The << operator is the stream insertion operator; it "inserts" the data on its right into the stream on its left.
  • std::endl is a manipulator that inserts a newline character and flushes the stream.

return 0;: This statement ends the main function. It sends the integer value 0 back to the operating system, which is the standard way of signaling that the program completed successfully.

Think of main() as the front door to your program. The operating system knows to start there, and return 0; is like politely closing the door on your way out.

Understanding Namespaces

You probably noticed the std:: prefix on cout and endl. std stands for standard, and it's a namespace. A namespace is a way to group related code and prevent naming conflicts.

Imagine two different libraries both define a function called print(). If you tried to use both libraries, how would the compiler know which print() function you mean? Namespaces solve this. One library might place its function in the graphics::print() namespace, and another might use text::print().

The C++ Standard Library places all its components, like cout, inside the std namespace. By writing std::cout, we are being explicit and telling the compiler to use the cout found within the std namespace. This avoids confusion and keeps code organized, especially in large projects.

Now, let's test what you've learned about the building blocks of a C++ program.

Quiz Questions 1/6

C++ was originally developed by Bjarne Stroustrup as an extension of which programming language?

Quiz Questions 2/6

What does it mean that C++ is a statically-typed language?

Understanding these fundamental pieces—the compiler, the main function, headers, and namespaces—is the first step to writing powerful C++ applications.