The short answer
Is C++ basically C with classes? Not quite. C++ began as an extension of C that added classes. Bjarne Stroustrup started the work in 1979, and early on it was called “C with Classes.” In the early 1980s the language was renamed C++. Today it is a separate language with its own standard, and it includes far more than classes.
Where the name came from
The early language centered on classes, which gave it its first name. The first C++ compilers, such as Cfront, translated C++ code into C and then compiled that C. That history is one reason people described the language as C with a few extra features.
What C++ adds beyond classes
Modern C++ has many features that C does not have:
- templates, which write code that works across many types
- function and operator overloading
- references, which are aliases for existing objects
- namespaces, which keep names from colliding
- exceptions, for reporting and handling errors
- the standard library, including containers such as
std::vectorandstd::string - features added in C++11 and later, such as lambdas, smart pointers and move semantics
Virtual functions let one interface work with objects of different types, which is the basis of polymorphism in c.
Where C code does not carry over
Are C and C++ the same? No. They share much of their syntax, but they are separate languages with separate standards. Some valid C code does not compile as C++. This short program compiles with a C compiler but fails as C++:
#include <stdlib.h>
int main(void) {
int *p = malloc(10 * sizeof *p); // C++ needs a cast here
int class = 1; // class is a keyword in C++
free(p);
return 0;
}
The malloc line needs a cast in C++, and class is a keyword in C++ but an ordinary name in C. So a C file cannot always be renamed to .cpp and compiled unchanged.
Structs in both languages
Both languages have structs, but they differ. In C++, a struct can have member functions, access labels, inheritance and constructors. In C, it cannot. The comparison is in c struct vs class.
What this means if you know C
If you already know C, much of the syntax carries over. The new parts are the class features, templates and the standard library. Watch for the compatibility differences above when you move code from C to C++.