C++ vs C Differences
In this page:
Procedural vs. Object-Oriented
C is a procedural language built around functions operating on data structures passed explicitly between them.
C++ extends C's syntax with object-oriented programming, letting you bundle data and the functions that operate on it together inside a single class.
Example: Procedural vs. Object-Oriented
// Include std::cout and std::cin
#include <iostream>
// Define the type Car
class Car {
// Members below can be accessed from outside the class
public:
void drive() { std::cout << "Driving" << std::endl; }
};
// Program execution starts in main()
int main() {
Car car;
car.drive();
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Standard Input Output
C performs input and output through library functions like printf() and scanf() from stdio.h, which rely on format specifiers (%d, %s) that the compiler can't type-check.
C++'s iostream introduces cout and cin, which use operator overloading to infer the correct type automatically and catch type mismatches at compile time.
Example: Standard Input Output
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare age and set it to 25
int age = 25;
// Print to the console with cout
std::cout << age << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Memory Management
C manages heap memory with the malloc() and free() library functions, which work with raw, untyped memory and never call constructors or destructors.
C++'s new and delete operators are language-level constructs that allocate memory and automatically invoke the relevant object's constructor or destructor.
Example: Dynamic Memory Management
// Include std::cout and std::cin
#include <iostream>
// Define the type Point
class Point {
// Members below can be accessed from outside the class
public:
Point() { std::cout << "Constructed" << std::endl; }
};
// Program execution starts in main()
int main() {
// Allocate an object on the heap with new
Point *p = new Point();
// Free the heap memory with delete
delete p;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Function Overloading
In C, every function in a given scope must have a unique name, since the compiler resolves calls purely by name.
C++ allows function overloading -- multiple functions sharing a name but differing in parameter types or count -- letting the compiler pick the right one based on the arguments you pass.
Example: Function Overloading
// Include std::cout and std::cin
#include <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }
// Program execution starts in main()
int main() {
show(5);
show(5.5);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Reference Variables
C++ introduces reference variables, which act as a permanent alias for another variable without requiring pointer syntax.
This lets you pass large objects to functions and modify them in place, avoiding both the overhead of a deep copy and the syntax of explicit pointer dereferencing that C requires.
Example: Reference Variables
// Include std::cout and std::cin
#include <iostream>
void increment(int &x) {
x += 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
increment(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Using
mallocandfreein C++ for objects, whennewanddeletecall constructors and destructors. - Assuming C and C++ code always compile the same way, such as with
void*conversions. - Using
printfwhencoutis more type-safe.
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- C++ vs C Differences
- C++ Interview Questions
- C++ Debugging Techniques
- C++ Input Validation
- C++ Namespaces
- C++ Header Files
- C++ Multi-file Programming
- C++ static_cast
- C++ dynamic_cast
- C++ const_cast
- C++ reinterpret_cast
- C++ Threads (std::thread)
- C++ Mutex & Locks
- C++ async & future
- C++ Mini Project — Calculator
- C++ Mini Project — Student Management
- C++ Interview Questions Advanced