Introduction to C++ & Strings
Alright, big moment — this week you are making the jump from C to C++. Now, do not worry, you are not throwing away everything you learned. C++ is basically C with superpowers. Almost all the C code you have written so far is also valid C++. Think of it like this: if C is a bicycle, C++ is that same bicycle but with an electric motor strapped on. Everything you already know still works, you just have some really nice extras now.
So what are these extras? Three big ones this week. First, cout and cin replace printf and scanf — and honestly, they are so much nicer. No more memorizing format specifiers like %d or %f — C++ figures out the type for you automatically. Second, the std::string class replaces those awkward C-style char arrays. Strings in C++ can grow on their own, you can smash them together with +, compare them with ==, and use handy methods like .length(), .substr(), and .find(). Third, you will learn about namespaces, which is C++'s way of keeping code organized so names do not clash — the std namespace is where all the standard library stuff lives.
| C Way | C++ Way | Why C++ Is Better |
|---|---|---|
printf("%d", x); | cout << x; | No format specifiers needed — type is detected automatically |
scanf("%s", name); | cin >> name; | No & needed, and no buffer overflow risk with string |
char name[50]; | string name; | No fixed size — strings grow and shrink as needed |
strcmp(a, b) == 0 | a == b | Direct comparison with == instead of a function call |
strcat(a, b); | a + b | Concatenation with the + operator |
By the end of this week, you will be writing cleaner, more modern code using C++ I/O streams, the string class, and namespaces. The tedious and error-prone C-style string handling? You are leaving that behind.