C vs C++: what's the difference?
How C++ builds on C with classes, references, RAII, templates and the standard library, and when each language is used.
C++ started as "C with classes" and almost all C code is also valid C++. What C++ adds:
- Classes with constructors and destructors, so objects clean up after themselves (RAII).
- References, a safer alternative to many pointer uses.
- The standard library:
std::string,std::vector,std::map, algorithms and smart pointers, so you rarely manage memory by hand. - Templates for generic code, and exceptions for error handling.
C is still the language of operating system kernels, embedded firmware and libraries that every other language calls. C++ dominates games, browsers, databases, trading systems and high-performance applications. Learning C first makes C++'s features make sense, which is why this course does it in that order.
Example
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> names{"Ada", "Linus"};
names.push_back("Bjarne");
for (const auto &n : names) std::cout << n << " ";
std::cout << "\n";
return 0;
}
Output:
Ada Linus Bjarne