What is C++?#
C++ was created by Bjarne Stroustrup at Bell Labs, starting in 1979 as "C with Classes". It keeps everything that makes C fast and adds powerful tools for building large programs:
- Object-oriented programming - classes, objects, inheritance, polymorphism.
- Generic programming - templates that work with any type.
- The Standard Template Library (STL) - ready-made, fast data structures and algorithms.
- Safer, easier features -
string, references,bool, function overloading, exceptions.
C++ powers game engines (Unreal), browsers (Chrome, Firefox), databases (MySQL, MongoDB), trading systems, and it is the most popular language in competitive programming.
C++ को C का "बड़ा भाई" समझिए। C की सारी speed और control इसमें है, साथ में classes और objects (OOP), templates और STL जैसी चीज़ें भी हैं, जिनसे बड़े programs कम code में और व्यवस्थित तरीके से लिखे जाते हैं।
Your first C++ program#
Hello, C++! 2 + 3 = 5
| Line | Meaning |
|---|---|
#include <iostream> |
the input/output stream library (cin, cout) - no .h in C++ headers |
using namespace std; |
use names from the standard library without writing std:: each time |
cout << ... |
send values to the screen; << can be chained |
endl |
new line (and flush the output); "\n" also works and is faster |
Without using namespace std; you write the full names:
Full names: std::cout and std::endl
In small programs and contests using namespace std; is fine. In big projects and header files, professionals prefer writing std:: so names from different libraries never clash.
Input with cin#
cin >> variable reads a value. No format specifiers and no & - C++ knows the type:
Sum: 42 Price with 18% GST: 589.988
The string type and getline#
C++ has a real string type - no more char arrays and strcpy:
Hi Rohan! Full name: Rohan Mehta Joshi (17 characters)
bool, and other things C++ adds#
1 0 true false ratio = 3.14286
Formatting output with iomanip#
3.14
Item Price
Pen 10.50
Book 249.00setw(n) sets the width of the next item only; fixed + setprecision(n) fixes the number of decimals.
Compiling C++#
Files end in .cpp. Use g++ instead of gcc:
g++ -std=c++17 -Wall main.cpp -o main
./main
On Windows, Code::Blocks, Dev-C++ and VS Code (with MinGW) all work. Visual Studio Community is a full professional IDE.
C vs C++ at a glance#
| Feature | C | C++ |
|---|---|---|
| Paradigm | procedural | procedural + object-oriented + generic |
| I/O | printf / scanf |
cout / cin (C functions still work) |
| Strings | char arrays + string.h |
std::string |
| Data + functions together | no (struct holds data only) |
yes - classes |
| Overloading | no | functions and operators |
| References | no | yes (int &r = x;) |
| Error handling | return codes | exceptions (try/catch) |
| Library | small standard library | huge STL |
Summary#
- C++ = C + classes, templates, STL and safer features.
cout <<prints,cin >>reads;getlinereads a full line.- Use
std::string,boolandauto; format with<iomanip>. - Compile with
g++ -std=c++17.