Introduction to C++
C++ is one of the most widely-used programming languages, known for its performance, versatility, and powerful features. It’s a language that bridges the gap between high-level and low-level programming, providing both the efficiency of assembly language and the flexibility of modern programming paradigms.
History of C++ and Its Evolution
C++ was created by Bjarne Stroustrup at Bell Labs in the early 1980s. Initially, it was developed as an extension to the C language, adding object-oriented capabilities to support complex systems and large-scale applications.
- 1983: C++ was first released as “C with Classes.”
- 1985: The name was officially changed to C++, signifying an "increment" over C, as "++" is the increment operator in programming.
- 1998: The ISO/IEC 14882 standard was published, giving C++ an official, standardized form.
- Modern C++: The language continues to evolve with major updates in 2011 (C++11), 2014 (C++14), 2017 (C++17), and 2020 (C++20), bringing new features and optimizations.
Features of C++
C++ has several key features that make it powerful and versatile:
- Object-Oriented Programming (OOP): C++ supports OOP concepts like classes and objects, inheritance, polymorphism, encapsulation, and abstraction, allowing for organized and modular code.
- High Performance: C++ is close to hardware, making it highly efficient, which is why it’s used in performance-critical applications like games, operating systems, and real-time processing systems.
- Rich Standard Library: The Standard Template Library (STL) provides powerful, reusable data structures and algorithms.
- Low-Level Manipulation: C++ allows direct memory access through pointers, making it suitable for system programming and embedded systems.
- Flexibility and Scalability: Its flexibility allows for various programming styles and the development of both small programs and large-scale applications.
Structure of a C++ Program
A basic C++ program consists of:
- Preprocessor Directives: Commands that begin with
#
, such as#include
, which imports libraries. - Main Function: Every C++ program has a
main()
function that serves as the entry point. - Code Statements: Statements that execute within the
main()
function or any user-defined functions.
Here’s a simple “Hello, World!” program in C++:
#include <iostream>
: Imports the standard input/output stream.int main()
: Defines the main function, which returns an integer value.std::cout << "Hello, World!";
: Outputs "Hello, World!" to the console.return 0;
: Indicates the program ended successfully.
Compiling and Running C++ Programs
C++ code is compiled, transforming the human-readable code into machine-readable instructions. Here’s how it works:
- Write the Code: Save your code in a
.cpp
file. - Compile the Code: Use a compiler, like GCC (GNU Compiler Collection), to compile the program.
- Command:
g++ program.cpp -o program
- Here,
program.cpp
is the file name, and-o program
specifies the output file.
- Command:
- Run the Program: Execute the output file.
- Command:
./program
Explanation:
- This program prompts the user to enter the length and width, calculates the area by multiplying the two, and displays the result.
- It uses
std::cin
for user input andstd::cout
for output, demonstrating basic I/O in C++.