Struct vs Class

Is C++ Basically C With Classes?

Is C++ basically C with classes? Not quite. It began that way, but it has grown into a separate language with templates and its own standard library.

On this page
  1. The short answer
  2. Where the name came from
  3. What C++ adds beyond classes
  4. Where C code does not carry over
  5. Structs in both languages
  6. What this means if you know C

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::vector and std::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++.

The complete guide to struct vs class

Struct vs Class in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

C++ Header File Example

A complete C++ header file example: a .h file with declarations, a .cpp file with definitions, a main program, and how to compile and run them.

Header Files 3 min read

Inheritance in C++

A practical guide to inheritance in C++: the syntax, access specifiers, constructor order, virtual functions and common mistakes, with short code examples.

Inheritance 7 min read