MarzleyTech Learn

Home / Learn / C++ / C++ introduction: what C++ is, where it's used, compilers, your first program, iostream, types, strings, control flow and functions

C++ introduction: what C++ is, where it's used, compilers, your first program, iostream, types, strings, control flow and functions

C++ began in the 1980s as "C with classes", created by Bjarne Stroustrup, and grew into one of the most powerful languages in the world. It keeps C's speed and low-level control, and adds classes, templates, a huge standard library (strings, vectors, maps, algorithms) and modern features that make code safer and more expressive. C++ runs game engines (Unreal), browsers (Chrome, Firefox), databases, trading systems, Adobe software, robotics, embedded systems and much of the software where performance matters. It's also the favourite language of competitive programmers and a common university course.

Why C++?

StrengthMeaning
PerformanceCompiles to fast native code; precise control over memory
Abstraction without costClasses and templates that compile to efficient code
Standard library (STL)Ready-made containers (vector, map) and algorithms (sort, find)
Huge reachGames, desktop apps, embedded, finance, scientific computing
Competitive programmingFast execution and the STL make it the most popular contest language

Where C++ is used

AreaExamples
GamesUnreal Engine, many console and PC games
Browsers and big appsChrome, Firefox, Adobe Photoshop, Microsoft Office components
Systems and databasesMySQL, MongoDB, parts of operating systems
Embedded and roboticsArduino (C/C++), drones, automotive software
FinanceLow-latency trading systems
Science and AISimulations; the fast cores of machine learning libraries

C++ vs C

CC++
Procedural (functions)Procedural + object-oriented + generic programming
printf/scanfcout/cin streams (printf still works)
char arrays for stringsstd::string
Manual arrays and mallocstd::vector, smart pointers, RAII
Small standard libraryLarge STL: containers, algorithms, utilities

Most C code compiles as C++, but modern C++ style is quite different and safer.

Setup

PlatformOption
WindowsMinGW-w64 (g++), Visual Studio Community (MSVC), Code::Blocks; VS Code with the C/C++ extension
macOSXcode command-line tools (Clang): xcode-select --install
Linuxsudo apt install build-essential (g++)
OnlineThis hub's Run button; onlinegdb.com; Compiler Explorer
Terminal
g++ -std=c++17 -Wall -Wextra hello.cpp -o hello
./hello

-std=c++17 (or c++20) chooses the language version; -Wall -Wextra enables warnings.

Your first program

C++ · runs live in the interactive lesson
#include <iostream>
using namespace std;

int main() {
    cout << "Habari, Kenya!" << endl;
    cout << "Learning C++ on the Marzley learning hub." << endl;
    return 0;
}
  • #include <iostream>: input/output streams.
  • using namespace std;: lets you write cout instead of std::cout. It's convenient in small programs; in larger projects, prefer writing std:: explicitly to avoid name clashes.
  • cout << ...: sends output to the screen; << chains values.
  • endl ends the line ("\n" is a faster alternative).
  • main returns 0 for success.

Variables and types

C++ · runs live in the interactive lesson
#include <iostream>
using namespace std;

int main() {
    int students = 45;
    long long population = 53000000LL;
    double fee = 12500.75;
    char grade = 'A';
    bool paid = true;
    auto town = string("Kisumu");      // auto: the compiler infers the type
    const double VAT = 0.16;           // constant

    cout << "Students: " << students << ", population " << population << endl;
    cout << "Fee: " << fee << ", grade " << grade << ", paid " << boolalpha << paid << endl;
    cout << town << " VAT: " << VAT * 1000 << " on KSh 1000" << endl;
    cout << "int uses " << sizeof(int) << " bytes, double " << sizeof(double) << endl;
    return 0;
}
TypeUse
int, long longWhole numbers (use long long for big totals)
doubleDecimals
charOne character
booltrue/false
std::stringText
autoLet the compiler deduce the type

Strings

C++ · runs live in the interactive lesson
#include <iostream>
#include <string>
using namespace std;

int main() {
    string first = "Wanjiku";
    string last = "Kamau";
    string full = first + " " + last;          // concatenation

    cout << full << " has " << full.length() << " characters" << endl;
    cout << "First letter: " << full[0] << endl;
    cout << "Surname starts at index " << full.find("Kamau") << endl;
    cout << "First 3 letters: " << full.substr(0, 3) << endl;

    string phone = "0712345678";
    if (phone.size() == 10 && phone[0] == '0') {
        string intl = "254" + phone.substr(1);
        cout << "M-Pesa format: " << intl << endl;
    }
    return 0;
}

std::string manages its own memory, unlike C char arrays, which avoids many bugs.

Input

Run input examples on your own computer:

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string name;
    int age;
    cout << "Full name: ";
    getline(cin, name);          // reads a whole line including spaces
    cout << "Age: ";
    cin >> age;                  // reads one value
    cout << "Hello " << name << ", next year you'll be " << age + 1 << endl;
}

Mixing cin >> then getline needs cin.ignore() in between to skip the leftover newline.

Control flow

C++ · runs live in the interactive lesson
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int mark = 72;
    if (mark >= 80) cout << "A" << endl;
    else if (mark >= 65) cout << "B" << endl;
    else if (mark >= 50) cout << "C" << endl;
    else cout << "Below C" << endl;

    int option = 2;
    switch (option) {
        case 1: cout << "Check balance" << endl; break;
        case 2: cout << "Send money" << endl; break;
        default: cout << "Invalid" << endl;
    }

    for (int i = 1; i <= 3; i++) {
        cout << "7 x " << i << " = " << 7 * i << endl;
    }

    vector<int> marks = {67, 82, 45, 90};
    int total = 0;
    for (int m : marks) {            // range-based for loop
        total += m;
    }
    cout << "Average: " << (double)total / marks.size() << endl;

    int months = 0;
    double balance = 0;
    while (balance < 50000) {
        balance = balance * 1.01 + 4500;
        months++;
    }
    cout << "Months to save 50,000: " << months << endl;
    return 0;
}

Functions and references

C++ · runs live in the interactive lesson
#include <iostream>
#include <string>
using namespace std;

double vat(double amount, double rate = 0.16) {      // default argument
    return amount * rate;
}

void applyPayment(double &balance, double amount) {   // & = reference: changes the caller's variable
    balance -= amount;
}

string greet(const string &name) {                    // const reference: no copy, can't modify
    return "Hello, " + name;
}

int square(int x) { return x * x; }
double square(double x) { return x * x; }              // overloading: same name, different types

int main() {
    cout << vat(1000) << " " << vat(1000, 0.08) << endl;
    double fees = 25000;
    applyPayment(fees, 7500);
    cout << "Balance: " << fees << endl;
    cout << greet("Otieno") << endl;
    cout << square(5) << " " << square(2.5) << endl;
    return 0;
}

A reference (double &balance) is another name for an existing variable: simpler and safer than C pointers for "change the caller's value". const string & passes large objects efficiently without copying.

Formatting output

C++ · runs live in the interactive lesson
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    cout << fixed << setprecision(2);
    cout << setw(12) << "Item" << setw(10) << "Price" << endl;
    cout << setw(12) << "Laptop bag" << setw(10) << 2500.0 << endl;
    cout << setw(12) << "USB flash" << setw(10) << 900.0 << endl;
    return 0;
}

setw(n) sets the width of the next value (right-aligned by default); fixed with setprecision(2) shows two decimal places. With g++, you can also use left and right to change alignment.

Common beginner errors

ErrorCause
'cout' was not declared in this scopeMissing #include <iostream> or std::
expected ';'Missing semicolon
Integer division surprises7 / 2 is 3; use 7.0 / 2 or a cast
getline skippedLeftover newline after cin >>; use cin.ignore()
Out-of-range indexvector with [] doesn't check; use .at(i) while learning
Think about it: What's the difference between void addBonus(double salary) and void addBonus(double &salary) when the function adds 1000 to salary?Show answer

The first receives a copy, so the caller's salary doesn't change. The second receives a reference to the caller's variable, so adding 1000 changes the original.

Summary

  • C++ (Bjarne Stroustrup) adds classes, templates and a large standard library to C's speed; it powers games, browsers, databases, embedded and finance software.
  • Compile with g++ -std=c++17 -Wall; programs use iostream with cout/cin.
  • Types include int, long long, double, char, bool, std::string; auto infers types; const makes constants.
  • Control flow mirrors C, plus range-based for loops over containers like vector.
  • Functions support default arguments, overloading and references (&, const &); iomanip formats output.

Check yourself

  1. Who created C++? (full name)

    Show answer

    Bjarne Stroustrup

  2. Which object prints output in C++?

    Show answer

    cout

  3. Which header provides cout and cin?

    Show answer

    iostream

  4. Which function reads a whole line including spaces?

    Show answer

    getline

  5. What symbol in a parameter makes it a reference?

    Show answer

    &

  6. Which keyword lets the compiler infer a variable's type?

    Show answer

    auto

Lesson 1 of 7 in C++ · Printable course notes