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 <iostream>
class Car {
public:
void drive() { std::cout << "Driving" << std::endl; }
};
int main() {
Car car;
car.drive();
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 <iostream>
int main() {
int age = 25;
std::cout << age << std::endl;
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 <iostream>
class Point {
public:
Point() { std::cout << "Constructed" << std::endl; }
};
int main() {
Point *p = new Point();
delete p;
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 <iostream>
void show(int x) { std::cout << "int: " << x << std::endl; }
void show(double x) { std::cout << "double: " << x << std::endl; }
int main() {
show(5);
show(5.5);
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 <iostream>
void increment(int &x) {
x += 1;
}
int main() {
int num = 5;
increment(num);
std::cout << num << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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