Skip to main content

C++ Basics

Introduction to C++

Complete lesson for Introduction to C++ using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with C++ OOP and STL where relevant.

L1

Basics — Introduction to C++

Section: C++ Basics. Start C++ with history, features, compilers, C vs C++ differences, and your first Hello World.

What is C++?

  • C++ is a general-purpose language created by Bjarne Stroustrup as an extension of C with classes.
  • It supports procedural, object-oriented, and generic programming.
  • Used in systems, games, finance, embedded, compilers, and high-performance apps.

Differences between C and C++

  • C++ builds on C ideas but is not a strict superset — some C code needs changes to compile as C++.
  • Use this table as a quick map of everyday differences you will meet in the course.
FeatureCC++
ParadigmProceduralMulti-paradigm (procedural, OOP, generic)
I/Oprintf / scanf (stdio.h)cin / cout (iostream), plus C-style I/O
Stringschar arrays + string.hstd::string preferred; C-strings still work
Memorymalloc / freenew / delete, smart pointers preferred
FunctionsNo overloadingFunction overloading allowed
Default argumentsNot supportedSupported
ReferencesNot availableReferences (&) as aliases
Classes / OOPStructs only (no methods by default)Classes, inheritance, polymorphism
Access controlNo public/private keywordspublic / private / protected
TemplatesNot availableFunction and class templates
ExceptionsNo language exceptionstry / catch / throw
NamespacesNo namespacesnamespace and std
bool typeOften int / macros historicallyBuilt-in bool (true / false)
STLNo STLContainers, algorithms, iterators
Comments/* */ (C89); // from C99// and /* */
CompatibilityC standardMostly C-compatible, not 100%

History of C++

C++ did not appear overnight. It grew from C as Bjarne Stroustrup added object-oriented ideas while keeping C’s speed. The timeline below shows how the language moved from “C with Classes” (1979) to the name C++ (1983), early books and core OOP features (1985), templates/STL era, the first ISO standard C++98, then modern C++ with C++11 and later standards up to today.

History of C++ language timeline: 1979 C with Classes by Bjarne Stroustrup, 1983 renamed C++, 1985 first C++ book and OOP features, 1989 templates and STL, 1991 ANSI/ISO work, 1998 C++98, 2003 refinements, 2011 modern C++ with auto lambdas smart pointers, then C++14 through C++23, and C++ today in AI games and high-performance systems.
History of C++ — from “C with Classes” to modern ISO standards (C++11 and beyond)
  • 1979–1983: Stroustrup starts “C with Classes”; language renamed C++.
  • 1985–1998: OOP features expand; first book; path to ISO C++98.
  • C++11 onward: modern C++ (auto, lambdas, smart pointers, and later modules/ranges).
  • Today: still used for games, systems, embedded, and high-performance apps.

First program

  • Save as hello.cpp, then compile with: g++ hello.cpp -o hello and run ./hello (or hello.exe on Windows).
#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    return 0;
}
L2

Advanced — Introduction to C++

Deeper rules

  • Prefer a modern dialect: compile with -std=c++17 or later unless a project forces an older standard.
  • C++ is not a strict superset of C — void* conversions, designated initializers rules, and keyword clashes differ.
  • One definition rule (ODR): each non-inline function/variable should have exactly one definition across the program.
  • main must return int; falling off the end of main implicitly returns 0 in C++.
  • Prefer RAII and the standard library over manual new/delete and C-style arrays for application code.
  • Headers vs sources: put declarations in .h/.hpp, definitions in .cpp (templates are a special case).
  • Name lookup and overload resolution are compile-time; virtual dispatch is runtime — different costs and rules.
  • Undefined behaviour (dangling pointers, out-of-bounds, signed overflow) is not “sometimes wrong” — it is unconstrained.

How a C++ Program Executes

A .cpp file does not run by itself. Your code goes through clear stages: write source → preprocess headers → compile to object code → link libraries → load and run from main → show output. Understanding this pipeline helps you fix compile errors, link errors, and runtime mistakes separately.

C++ program execution steps: 1 write source code in a .cpp file, 2 preprocessing of include directives, 3 compilation into object code with syntax checking, 4 linking with libraries to create an executable, 5 execution starting from main, 6 output on the console such as Hello C++.
C++ program execution steps — from writing hello.cpp to console output
  • Write: create a .cpp source file in an editor or IDE.
  • Preprocess: handle #include / #define and expand headers.
  • Compile: check syntax and produce an object file (.o / .obj).
  • Link: combine object code with libraries into an executable (.exe / a.out).
  • Execute: OS loads the program; run starts at main.
  • Output: statements print results (for example Hello, C++!).
L3

Tricky Interview Points

Q1) What is C++?

Answer C++ is a general-purpose programming language created as an extension of C with classes. It supports procedural, object-oriented, and generic programming.

Q2) Who developed C++?

Answer Bjarne Stroustrup developed C++ at Bell Labs. It was first known as “C with Classes”.

Q3) Is C++ only an object-oriented language?

Answer No. C++ is multi-paradigm: you can write procedural code, OOP with classes, and generic code with templates.

Q4) Is C++ a strict superset of C?

Answer No. C++ is mostly compatible with C, but not 100%. Some valid C programs need changes to compile as C++.

Q5) Name a few important differences between C and C++.

Answer C++ adds classes/OOP, references, function overloading, templates, exceptions, namespaces, bool, and the STL. I/O commonly uses cin/cout instead of only printf/scanf.

Q6) Which header is used for basic input and output in C++?

Answer `<iostream>`. You typically use `std::cout` for output and `std::cin` for input.

Q7) What is the difference between printf/scanf and cin/cout?

Answer In C, I/O uses printf/scanf from stdio.h. In C++, the preferred style is cin/cout from iostream (C-style I/O can still be used).

Q8) What does `#include <iostream>` do in a Hello World program?

Answer It includes the standard iostream library so the program can use input/output facilities like `std::cout`.

Q9) Why do we write `std::cout` instead of just `cout`?

Answer `cout` lives in the `std` namespace. `std::cout` uses namespace qualification. You may also write `using namespace std;` in a .cpp file and then use `cout`.

Q10) What is the role of `main` in a C++ program?

Answer `main` is the entry point of an executable program. Execution starts from `main`.

Q11) How do you compile and run a simple C++ program?

Answer Save the file as something like `hello.cpp`, compile with `g++ hello.cpp -o hello`, then run `./hello` (or `hello.exe` on Windows).

Q12) Give examples of features C++ has that C does not (from this lesson).

Answer Examples: classes and OOP, references, function overloading, default arguments, templates, exceptions, namespaces, built-in bool, and the STL.

L4

Simple Programs

Hello World with namespace qualification

#include <iostream>

int main() {
    std::cout << "Hello, C++!\n";
    return 0;
}

Hello with using directive

#include <iostream>
using namespace std;

int main() {
    cout << "Welcome to C++\n";
    return 0;
}
L5

Problem Solving Tricks

  • Always enable warnings early: g++ -Wall -Wextra -Wpedantic -std=c++17 file.cpp -o app.
  • If you know C, learn the differences table first — many “bugs” are C habits that are illegal or risky in C++.
  • Prefer std::cout << ... over printf until you need formatted C I/O for a specific reason.
  • Keep using namespace std; out of headers; it pollutes every file that includes them.
  • One executable → one main. Linker “multiple definition of main” means two translation units both define it.
  • Match the language dialect to the book/course (-std=c++17 vs C++20 features like ranges).
L6

Practice Programs List

  1. Write a C++ program that prints your name and a welcome message using std::cout.
  2. Compile the same program with -Wall -Wextra -std=c++17 and fix every warning.
  3. Rewrite a tiny C printf Hello World as idiomatic C++ with iostream.
  4. Create two .cpp files: one with a helper function, one with main, and link them with g++.
  5. List eight differences between C and C++ from memory, then check against the lesson table.
  6. Write a program that prints the C++ standard macro value (__cplusplus) and explain what it means.
  7. Intentionally write int *p = malloc(4); in a .cpp file, observe the error, then fix it the C++ way.
  8. Build Hello World once with g++ and once with clang++ (if available) and compare commands.