← Back to C++ Course | Chapter 17: Advanced C++ | Lesson 1 of 17

C++ vs C Differences

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

cpp
#include <iostream>

class Car {
public:
	void drive() { std::cout << "Driving" << std::endl; }
};

int main() {
	Car car;
	car.drive();
	return 0;
}

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

cpp
#include <iostream>

int main() {
	int age = 25;
	std::cout << age << std::endl;
	return 0;
}

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

cpp
#include <iostream>

class Point {
public:
	Point() { std::cout << "Constructed" << std::endl; }
};

int main() {
	Point *p = new Point();
	delete p;
	return 0;
}

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

cpp
#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;
}

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

cpp
#include <iostream>

void increment(int &x) {
	x += 1;
}

int main() {
	int num = 5;
	increment(num);
	std::cout << num << std::endl;
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.