⚡0 XP🔥0

Introduction to C++

Lesson 1 of 12 ⏱ 5 min read ✎ 2 exercises ❓ 5-question quiz ▶ Runs C++

What C++ adds to C, your first program with iostream, cin and cout, namespaces, the bool type and how C++ code is compiled.

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#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
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:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Full names: std::cout and std::endl
Tip

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:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
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:

▶ Try it YourselfC++
Ctrl + Enter to run
Output
Hi Rohan!
Full name: Rohan Mehta Joshi (17 characters)

bool, and other things C++ adds#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
1 0
true false
ratio = 3.14286

Formatting output with iomanip#

▶ Try it YourselfC++
Ctrl + Enter to run
Output
3.14
      Item   Price
       Pen   10.50
Book        249.00

setw(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; getline reads a full line.
  • Use std::string, bool and auto; format with <iomanip>.
  • Compile with g++ -std=c++17.

Practice Exercises

Write your code, then press Check. Your program is tested automatically.

1

Greeting with cin and cout

Read a first name and an age, then print Hello Asha, next year you will be 21. Test input Asha 20.

Test input:
Asha 20
Expected output:
Hello Asha, next year you will be 21
2

Temperature converter

Read a temperature in Celsius (decimal) and print it in Fahrenheit with 1 decimal using fixed and setprecision(1), e.g. 37.5 C = 99.5 F. Test input 37.5.

Test input:
37.5
Expected output:
37.5 C = 99.5 F

Quick Quiz

Q1.

Who created C++?

Q2.

What does std:: in std::cout mean?

Q3.

Which operator is used with cout?

Q4.

cin >> name; with a std::string name and input Ravi Kumar stores...

Q5.

Which is true about C++ and C?