beginner15 min

Introduction to C++: What It Adds Over C

iostream vs stdio, std::string vs C strings, and why C++ exists alongside C.

What you'll learn

  • Explain what C++ adds over C: classes, templates, a larger standard library
  • Contrast iostream's std::cout with C's printf, and std::string with a C char array
  • Identify the parts of a minimal C++ program: #include <iostream>, main, std::cout

Explanation

C++ began as "C with classes" and grew into a language that keeps C's low-level control and performance while adding object-oriented programming (classes), generic programming (templates), and a much larger standard library -- the Standard Template Library (STL) you'll meet later in this course. Game engines, high-frequency trading systems, and large performance-critical applications commonly use C++ specifically because it lets you write high-level, organized code without giving up direct control over memory and performance when you need it.

Where C uses printf/scanf from <stdio.h> with format-specifier strings, C++ commonly uses <iostream>'s std::cout (output) and std::cin (input) with the << and >> operators instead: std::cout << "Hello, C++!" << std::endl;. There's no format string to keep in sync with your argument types -- the compiler already knows each value's type and formats it correctly, which removes an entire category of C's format-specifier mismatch bugs.

C++ also adds std::string (from <string>) as a real string type, distinct from a raw C char array: it tracks its own length, resizes itself as needed, and supports + for concatenation directly -- no manual null-terminator bookkeeping or fixed buffer sizing required, though C strings and C-style arrays remain fully valid and sometimes necessary in C++ too, especially at the boundary with C libraries.

Because a real C++ compiler has to handle templates and a large standard library, this platform can't safely compile or execute C++ in your browser (see this course's guided-lab notice on every lesson) -- even more so than for plain C. Every lesson instead gives you real C++ source code to read and predict, exactly the skill you'll rely on constantly when working with someone else's C++ codebase.

Guided lab

Predict: A minimal C++ program

C++Not executed
This lab does not run in your browser or on VisaSparkSchools's servers. Read the code, predict what it does, then reveal the real expected output.

Read this program and predict exactly what it prints when compiled and run.

#include <iostream>
#include <string>

int main() {
    std::string message = "Hello, C++!";
    std::cout << message << std::endl;
    std::cout << "2 + 2 = " << 2 + 2 << std::endl;
    return 0;
}

Stuck? Get a hint.

Common mistakes

  • Assuming std::cout needs a format-specifier string the way printf does -- it doesn't; the compiler already knows each argument's type.
  • Treating std::string exactly like a C char array, forgetting it manages its own length and resizing automatically.
  • Forgetting `#include <iostream>` (for std::cout) or `#include <string>` (for std::string) and being confused by the resulting compile error.

Knowledge check

Knowledge check

1. What does C++ add on top of what C already provides?
2. What operator does std::cout use to output a value?
3. How does std::string differ from a raw C char array?

Takeaway

C++ keeps C's performance and control while adding classes, templates, and a real std::string/std::cout-based standard library that removes much of C's manual bookkeeping.

Summary

C++ extends C with object-oriented and generic programming; std::cout/std::cin replace format-specifier-driven printf/scanf, and std::string replaces manual char-array string handling.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.