System Programming Topic
Introduction to C++
What is C++?
C++ is a high-performance, general-purpose programming language that combines object-oriented, procedural, and generic programming paradigms. It was designed as an extension of C with additional features like classes, objects, and inheritance.
Key Characteristics:
-
Multi-paradigm: Supports OOP, procedural, and generic programming
-
Compiled language: Source code is compiled directly to machine code
-
Systems programming language: Used for OS, drivers, game engines
-
Memory management: Offers both automatic and manual memory control
-
High performance: Close to hardware, minimal runtime overhead
Hello World in C++:
cpp
#include <iostream> // Header file for input/output
int main() { // Main function - entry point
std::cout << "Hello, World!" << std::endl;
return 0; // Return success code
}
History & Evolution
Timeline:
1979 - C with Classes (Bjarne Stroustrup at Bell Labs)
-
Added classes, inheritance, default arguments to C
-
Created to write simulations requiring object-oriented features
1983 - C++ Name Adopted
-
"++" is the increment operator in C (C incremented)
-
First commercial implementation
1985 - CFront 1.0 (First commercial release)
-
Reference manual "The C++ Programming Language" published
1989 - C++ 2.0
-
Multiple inheritance, abstract classes
-
Protected members, type-safe linkage
1998 - C++98 (ISO/IEC 14882:1998)
-
First standardized version
-
STL (Standard Template Library) included
2003 - C++03
-
Bug fixes and minor improvements
-
Technical corrigendum
2011 - C++11 (Major Update)
-
Auto keyword, lambda expressions
-
Move semantics, smart pointers
-
Range-based for loops, nullptr
2014 - C++14
-
Binary literals, digit separators
-
Generic lambdas, return type deduction
2017 - C++17
-
Structured bindings, if/switch with initializer
-
Filesystem library, std::optional, std::variant
2020 - C++20 (Major Update)
-
Concepts, ranges, coroutines
-
Modules, calendar/timezone library
2023 - C++23
-
std::print, std::mdspan
-
Stacktrace library, constexpr improvements
C++ vs C vs Java
Comparison Table:
| Feature | C | C++ | Java |
|---|---|---|---|
| Paradigm | Procedural only | Multi-paradigm (OOP, procedural, generic) | Pure OOP (everything is an object) |
| Memory Management | Manual (malloc/free) | Both manual and automatic (RAII) | Automatic (Garbage Collection) |
| Compilation | Compiled to native code | Compiled to native code | Compiled to bytecode, runs on JVM |
| Performance | Very high | Very high (close to C) | Good (slower due to GC and JVM) |
| Portability | Platform-dependent | Platform-dependent | Platform-independent (Write Once Run Anywhere) |
| Pointers | Full support | Full support | No pointers (references only) |
| Multiple Inheritance | Not applicable | Supported (with virtual inheritance) | Not supported (interfaces only) |
| Templates/Generics | Not available | Templates (compile-time) | Generics (type-erasure at runtime) |
| Exception Handling | Manual (error codes) | Try-catch-throw | Try-catch-throw (checked & unchecked) |
| Operator Overloading | No | Yes | No (except + for String) |
| Standard Library | C Standard Library | STL (Standard Template Library) | Java Class Library (JCL) |
| Header Files | Required | Required (until C++20 modules) | Not required (packages) |
Detailed Comparisons:
C++ vs C:
Similarities:
-
Same basic syntax and operators
-
Compiled to native machine code
-
Manual memory management possible
-
Direct hardware access
Differences:
cpp
// C - Procedural
#include <stdio.h>
struct Point {
int x, y;
};
double distance(struct Point p1, struct Point p2) {
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
}
int main() {
struct Point p1 = {0, 0};
struct Point p2 = {3, 4};
printf("Distance: %f\n", distance(p1, p2));
return 0;
}
// C++ - Object-Oriented
#include <iostream>
#include <cmath>
class Point {
private:
int x, y;
public:
Point(int x, int y) : x(x), y(y) {} // Constructor
double distanceTo(const Point& other) const {
return sqrt(pow(other.x - x, 2) + pow(other.y - y, 2));
}
};
int main() {
Point p1(0, 0);
Point p2(3, 4);
std::cout << "Distance: " << p1.distanceTo(p2) << std::endl;
return 0;
}
C++ vs Java:
Similarities:
-
Object-oriented features
-
Similar syntax for basic constructs
-
Exception handling
-
Standard libraries
Differences:
java
// Java
public class Main {
public static void main(String[] args) {
String s = "Hello"; // Reference type
System.out.println(s.length()); // Method call
// Automatic garbage collection
}
}
cpp
// C++
#include <iostream>
#include <string>
int main() {
std::string s = "Hello"; // Object on stack
std::cout << s.length() << std::endl; // Method call
// Manual memory management or smart pointers
int* arr = new int[10]; // Heap allocation
delete[] arr; // Manual cleanup
return 0;
}
Use Cases:
C:
-
Operating systems (Linux kernel)
-
Embedded systems
-
System utilities
-
Performance-critical applications where OOP overhead is undesirable
C++:
-
Game engines (Unreal Engine)
-
High-frequency trading systems
-
Web browsers (Chrome, Firefox rendering engines)
-
Database systems (MySQL, MongoDB)
-
Graphics and CAD software
Java:
-
Enterprise applications
-
Android apps (historically)
-
Web applications (Spring framework)
-
Big data technologies (Hadoop)
Key C++ Concepts with Examples
1. Object-Oriented Programming:
cpp
class Animal {
protected:
std::string name;
public:
Animal(const std::string& name) : name(name) {}
virtual void makeSound() const = 0; // Pure virtual function
virtual ~Animal() {} // Virtual destructor
};
class Dog : public Animal {
public:
Dog(const std::string& name) : Animal(name) {}
void makeSound() const override {
std::cout << name << " says: Woof!" << std::endl;
}
};
int main() {
Dog myDog("Buddy");
myDog.makeSound();
return 0;
}
2. Templates (Generic Programming):
cpp
template<typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
template<typename T, size_t N>
class Array {
private:
T data[N];
public:
T& operator[](size_t index) { return data[index]; }
const T& operator[](size_t index) const { return data[index]; }
};
int main() {
std::cout << max(5, 10) << std::endl; // int
std::cout << max(3.14, 2.71) << std::endl; // double
Array<int, 5> arr;
arr[0] = 42;
return 0;
}
3. Modern C++ Features (C++11 onwards):
cpp
// Auto keyword
auto x = 5; // int
auto y = 3.14; // double
// Lambda expressions
auto add = [](int a, int b) { return a + b; };
std::cout << add(5, 3) << std::endl;
// Range-based for loop
std::vector<int> numbers = {1, 2, 3, 4, 5};
for (const auto& num : numbers) {
std::cout << num << " ";
}
// Smart pointers (no manual delete)
auto ptr = std::make_unique<int>(42);
auto sharedPtr = std::make_shared<int>(42);
// Move semantics
std::vector<int> source = {1, 2, 3};
std::vector<int> destination = std::move(source); // Efficient transfer
4. STL (Standard Template Library):
cpp
#include <vector>
#include <map>
#include <algorithm>
#include <string>
int main() {
// Vector (dynamic array)
std::vector<int> vec = {5, 2, 8, 1, 9};
std::sort(vec.begin(), vec.end());
// Map (key-value pairs)
std::map<std::string, int> ages = {
{"Alice", 30},
{"Bob", 25}
};
// Find element
auto it = std::find(vec.begin(), vec.end(), 8);
if (it != vec.end()) {
std::cout << "Found: " << *it << std::endl;
}
return 0;
}
Learning Path:
Beginner:
-
Basic syntax and data types
-
Control structures (if, loops)
-
Functions and recursion
-
Arrays and pointers
-
Basic OOP (classes, objects)
Intermediate:
-
Advanced OOP (inheritance, polymorphism)
-
Templates and STL
-
Exception handling
-
File I/O
-
Operator overloading
Advanced:
-
Smart pointers and memory management
-
Move semantics and perfect forwarding
-
Multithreading and concurrency
-
Modern C++ features (C++11/14/17/20)
-
Design patterns in C++
Why Learn C++ Today?
-
Performance: Still the fastest general-purpose language
-
Control: Fine-grained control over memory and hardware
-
Industry Demand: Critical systems, game development, HFT
-
Foundation: Understanding C++ helps learn other languages better
-
Evolution: Modern C++ is safer and more expressive than older versions
C++ remains a powerful language that evolves while maintaining backward compatibility. Its "zero-cost abstractions" philosophy means you only pay for features you use, making it ideal for performance-critical applications.
Comments (0)
No comments yet. Be the first to comment!