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

C++ vs C Differences

C++ grew from C but added new things like objects, references, and overloading, like a newer model of a car. This lesson compares them.

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

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

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

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

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using malloc and free in C++ for objects, when new and delete call constructors and destructors.
  2. Assuming C and C++ code always compile the same way, such as with void* conversions.
  3. Using printf when cout is more type-safe.

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.