1. Problem It Solves
C and C++ share much syntax but are separate languages. C++ supports procedural, object-oriented, and generic programming together; classes, templates, and object destruction help organize data and manage resources.
2. Prerequisites
Basic variables, functions, arrays, and pointers.
An understanding that allocated memory needs someone responsible for releasing it.
3. Core Idea
C++ evolved from C and adds mechanisms such as references, function overloading, templates, exceptions, and a library of containers and algorithms. Useful C++ programs do not require an inheritance hierarchy.
A practical difference is resource ownership: std::vector<int> owns storage for its elements and releases it when destroyed. This connection between resources and object lifetime is called RAII.
4. Minimal Syntax
std::vector<int> values{1, 2, 3};
values.push_back(4);With <vector>, the container manages size and storage. In C, a dynamic array commonly requires a pointer, element count, capacity, and separate allocation/deallocation calls.
5. How It Works
Types and syntax: a valid C program is not necessarily valid C++. For example, C permits an implicit conversion from
void*toint*; C++ requires an explicit conversion.Code reuse: templates provide functions and containers for different types, checked at compile time.
Object lifetime: constructors establish objects and destructors release resources. Automatic objects are destroyed when their scope ends; exceptions can trigger cleanup of scopes being exited.
The library:
std::vector,std::string, and standard algorithms express intent at a higher level than manual pointer manipulation and loops.
See the C compatibility annex in the C++ draft for specific language differences.
6. Common Mistakes
Assuming that renaming
.cto.cppis enough to port a C program.Assuming C++ requires inheritance or that every member function call is virtual.
Assuming C++ garbage-collects all allocations: an unmanaged
newcan still leak.Assuming every C++ abstraction has no cost; cost depends on its design, types, and use.
7. When to Use It
When porting C code, start with memory ownership and type rules. In new C++ code, use containers and resource-owning objects to reduce manual cleanup paths, then measure costs when performance matters.
8. Simple Example
In the C++20 sample, a template adds elements without copying the vector:
template<class T>
T sum(const std::vector<T>& values) {
T result{};
for (const T& value : values) result += value;
return result;
}For std::vector<int>{1, 2, 3}, it prints 6. T must support initialization and +=; for signed integer types, the sum must also remain representable.
Complete sample code
Source file
dailycppinterview/147_what-is-cpp-and-how-is-it-different-from-c/main.cpp
// Real-World C++ Interviews Q147: What is C++? How is it different from C?
// Key: C++ is a general-purpose, multi-paradigm language that evolved from C and adds
// abstraction mechanisms such as classes, templates, RAII, overloading, exceptions, and a rich
// standard library. C and C++ share much syntax but are separate languages with different type
// rules, object and lifetime models, valid programs, and idioms; C++ is not merely C with
// classes.
#include <iostream>
#include <vector>
template<class T>
T sum(const std::vector<T>& values) {
T result{};
for (const T& value : values) result += value;
return result;
}
int main() {
const std::vector<int> values{1, 2, 3};
std::cout << sum(values) << std::endl;
}
9. Key Takeaways
Explain the difference through a concrete example: for a dynamic array, C++ can use vector to combine data, operations, and resource cleanup in one object.
10. Self-Check Question
Full question: What is C++? How is it different from C?
In sum, what separate jobs do the template, the const reference, and the vector's destructor perform? Give an example of valid C that is invalid in C++.