C++ tutorials  /  Introduction to C++
Chapter 1 · C++

Introduction to C++

C++ is a general-purpose, statically typed, compiled programming language developed by Bjarne Stroustrup at Bell Labs starting in 1979 as an extension of the C language, originally called 'C with Classes'. It supports multiple programming paradigms including procedural, object-oriented, and generic programming, giving developers fine-grained control over system resources and memory while also offering high-level abstractions.

Think of C++ as the C language with superpowers added on top. C gives you speed and low-level control, and C++ adds features like classes, objects, templates, and exception handling that let you write large, organized, reusable programs without giving up performance. It compiles directly to machine code, so programs run fast, and it lets you decide exactly how much control you want over memory and hardware.

C++ powers software you use every day without realizing it. Google Chrome and Mozilla Firefox use C++ for their rendering engines because it needs to process web pages at lightning speed. Video game engines like Unreal Engine are written in C++ so games can render complex 3D graphics at 60+ frames per second. Adobe Photoshop uses C++ to apply filters to massive images instantly. Even the MySQL and MongoDB database engines are built in C++ for high-performance data handling. In competitive programming platforms like Codeforces and CompileX itself, C++ is the most popular language because its execution speed gives contestants an edge on strict time limits.

Before C++, developers had to choose between the raw speed of C (but with no support for organizing large codebases using objects) or slower high-level languages that offered better structure but sacrificed performance. C++ solved this by combining low-level memory control with high-level object-oriented features, allowing developers to build both blazing-fast system software (like operating systems and drivers) and large-scale, maintainable applications (like enterprise software and game engines) using a single language.

  • Procedural Programming Support: C++ fully supports the procedural style inherited from C, where programs are organized as a sequence of functions and procedures that operate on data, making it easy to write straightforward, top-down programs.
  • Object-Oriented Programming (OOP): C++ introduces classes and objects, enabling encapsulation, inheritance, and polymorphism so developers can model real-world entities and build modular, reusable, and maintainable code.
  • Generic Programming: Through templates, C++ allows functions and classes to operate on any data type without rewriting code, forming the basis of the Standard Template Library (STL) used for containers like vectors, maps, and sets.
  • Low-Level Systems Programming: C++ allows direct memory management via pointers, manual allocation/deallocation, and hardware-level access, making it suitable for operating systems, embedded systems, and device drivers.
#include <iostream> using namespace std; int main() { // Your code starts here cout << "Hello, World!" << endl; return 0; }
A new programmer wants to understand why C++ is chosen over other languages for performance-critical applications like game engines, financial trading systems, and embedded devices, and wants to write their very first working C++ program to confirm their development environment is set up correctly.
Your First C++ Program
A minimal C++ program that prints 'Hello, World!' to the console, demonstrating the basic structure every C++ program follows.
cpp
#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; return 0; }
Hello, World!
The '#include <iostream>' line imports the input-output stream library needed for 'cout'. 'using namespace std;' lets us use standard library names like 'cout' without prefixing them with 'std::'. The 'main()' function is the entry point of every C++ program — execution always starts here. 'cout << "Hello, World!" << endl;' sends the string to the console followed by a newline. Finally, 'return 0;' tells the operating system the program finished successfully.
C++ with Basic Variables and Output
A slightly extended example showing how C++ combines variable declarations with output, reflecting its statically typed nature.
cpp
#include <iostream> using namespace std; int main() { string language = "C++"; int yearCreated = 1985; cout << language << " was released in " << yearCreated << "." << endl; return 0; }
C++ was released in 1985.
This example declares a 'string' variable and an 'int' variable, showcasing C++'s static typing where each variable's type is fixed at compile time. The '<<' operator chains multiple values into a single output stream, demonstrating operator overloading, one of C++'s signature OOP features.
  • Forgetting to Include Necessary Headers: Beginners often use 'cout', 'string', or 'vector' without including '<iostream>', '<string>', or '<vector>' respectively, resulting in compiler errors like 'cout was not declared in this scope'. Always include the correct header for the features you use.
  • Omitting 'using namespace std;' or Misusing It: New learners either forget 'using namespace std;' and get undeclared identifier errors, or overuse it in large projects, which can cause naming conflicts between the standard library and their own custom code. In production code, it is better to use 'std::' prefixes explicitly or scope-limit the 'using' directive.
  • Confusing C++ with C: Beginners often assume all C code compiles identically in C++, but C++ has stricter type checking, different default handling of certain features (like implicit void* casts), and reserved keywords (like 'class', 'new', 'delete') that are valid identifiers in C but not in C++.
  • Not Returning a Value from main(): While some compilers implicitly return 0 if 'main()' falls through without an explicit return, relying on this behavior is bad practice and non-portable across all compilers and standards, and can cause confusion when debugging exit codes in scripts or automated build pipelines.
  • Prefer std:: Prefix Over Blanket 'using namespace std;': In real-world and production-grade C++ projects, avoid 'using namespace std;' at the global scope, especially in header files, since it can cause naming collisions across large codebases. Instead, use 'std::cout', 'std::vector', etc., or limit 'using' directives to a local function scope.
  • Always Compile with Warnings Enabled: Use compiler flags like '-Wall -Wextra' with g++ to catch subtle bugs, uninitialized variables, and type mismatches early, since C++'s flexibility can silently allow risky code that leads to undefined behavior.
  • Understand the C++ Standard You Are Targeting: C++ has evolved through multiple standards (C++98, C++11, C++14, C++17, C++20, C++23), each adding significant features like lambdas, smart pointers, and concepts. Always know which standard your compiler targets, since code using modern features (e.g., 'auto', range-based for loops) won't compile under older standards without the right flags (e.g., '-std=c++17').
  • Start with Simple, Readable Code Before Optimizing: New C++ learners often try to write 'clever' low-level optimized code too early. It's better to first write clear, correct, well-structured code and only optimize critical sections after profiling, since premature optimization often introduces bugs and reduces readability.
What is C++ and how does it differ from C?
C++ is a general-purpose, statically typed, compiled language created by Bjarne Stroustrup as an extension of C. While C is purely procedural, C++ adds object-oriented programming (classes, inheritance, polymorphism), generic programming via templates, exception handling, and a richer standard library (STL). C++ also enforces stricter type checking than C.
Why is C++ still widely used in 2024 despite the rise of newer languages?
C++ remains dominant in domains requiring maximum performance and control, such as game engines, real-time systems, embedded devices, high-frequency trading, and operating systems, because it offers near-hardware-level performance combined with modern abstractions like templates and smart pointers. Its zero-overhead abstraction philosophy means you don't pay a performance cost for features you don't use, which newer garbage-collected languages typically cannot match.
What are the four main pillars of Object-Oriented Programming that C++ supports?
The four pillars are Encapsulation (bundling data and methods within classes and restricting direct access via access specifiers), Abstraction (hiding complex implementation details behind simple interfaces), Inheritance (allowing a class to derive properties and behavior from another class), and Polymorphism (allowing objects to be treated as instances of their parent class, with behavior resolved at compile-time via overloading or runtime via virtual functions).
What does 'compiled language' mean, and why does it matter for C++?
A compiled language is translated directly into machine code by a compiler before execution, as opposed to interpreted languages that are executed line-by-line by an interpreter at runtime. Because C++ is compiled ahead of time into native machine code, programs typically run significantly faster than interpreted languages like Python, since there is no runtime translation overhead.
What is the significance of the 'main()' function in a C++ program?
The 'main()' function is the mandatory entry point of every executable C++ program; execution begins here regardless of where other functions are defined in the file. It must return an 'int' value, where a return of 0 conventionally signals successful execution to the operating system, and non-zero values signal an error condition.
Write a C++ program that declares three variables — your name (string), age (int), and GPA (double) — and prints them in a single formatted sentence using cout.
#include <iostream> using namespace std; int main() { string name = "Alex"; int age = 21; double gpa = 3.8; cout << name << " is " << age << " years old with a GPA of " << gpa << "." << endl; return 0; }
Explain, in your own words, why C++ is considered a 'middle-level' language, bridging low-level and high-level programming.
C++ is called a middle-level language because it combines the low-level capabilities of languages like C (direct memory access via pointers, manual memory management, hardware-level control) with high-level features typically found in languages like Java or Python (classes, objects, exception handling, generic programming via templates, and an extensive standard library). This dual nature allows developers to write both system-level software close to the hardware and large-scale, abstracted application software using the same language.
Modify the classic 'Hello, World!' program to instead ask the user for their name using cin and greet them personally.
#include <iostream> using namespace std; int main() { string userName; cout << "Enter your name: "; cin >> userName; cout << "Hello, " << userName << "! Welcome to C++." << endl; return 0; }

C++ is a powerful, statically typed, compiled, multi-paradigm language created by Bjarne Stroustrup as an extension of C, blending low-level hardware control with high-level object-oriented and generic programming features. It remains the language of choice for performance-critical software like game engines, browsers, databases, and embedded systems, and forms the foundation for understanding core computer science concepts such as memory management, OOP, and the STL. Mastering the basics — from the 'main()' function and headers to variables and output — is the essential first step toward becoming proficient in systems-level and competitive programming.

© 2026 CompileX. Maintained by Aditya Kumar Sharma.