top of page

A Primer on C++ : History, Differences, and Syntax


Haruki Robotics Lab 29 Jul 2023


From the smartphone in your pocket to the video games you play, C++ has made a significant impact on the development of programming as we know it. This article will take you on a brief journey through the history of C++ , highlight how it differs from Swift and Python, and outline its distinctive syntax.



A Brief History of C++


C++ was developed in the 1980s by Danish computer scientist Bjarne Stroustrup at Bell Labs. Stroustrup's primary motivation was to add object-oriented programming (OOP) capabilities to the already popular C language, enhancing its flexibility and power.


The language was initially named "C with Classes," but was later renamed in 1983. The "++" is a nod to the increment operator in C, symbolizing the evolution from C to . The language was officially standardized by the International Organization for Standardization (ISO) in 1998, with multiple updates to the standard released since then.


How is C++ Different from Swift and Python?


While C++ , Swift, and Python are all high-level programming languages, they have different design philosophies and use-cases.


1. Memory Management: Unlike Swift and Python, which use automatic memory management, allows for manual memory management. This means that you, as the programmer, have direct control over when memory is allocated or deallocated. This feature provides more flexibility but also requires more diligence to prevent memory leaks and other issues.


2. Type System: Swift and Python are strongly typed languages, meaning you cannot perform operations inappropriate to the type of data, whereas is statically typed, requiring you to declare the type of variables before you use them.


3. Performance: generally provides better performance than Swift and Python. This is because programs are compiled to machine code, which usually results in faster execution times. This makes a preferred choice for system software, game development, and other performance-critical tasks.


4. Learning Curve: Python and Swift are generally considered easier to learn than C++ . This is due in part to C++ 's manual memory management and its complex syntax, which can be challenging for beginners.


An Outline of Syntax

C++ syntax is unique and flexible, allowing for both procedural and object-oriented programming styles. Here are some basic elements:


- Variables and Data Types: Similar to other languages, C++ has data types such as integers (`int`), floating point numbers (`float`), characters (`char`), and booleans (`bool`). Variables must be declared with their type before they can be used.

int age = 30;
float average = 10.5;
char grade = 'A';
bool is_valid = true;

- Control Structures: C++ uses control structures like `if`, `else`, and `switch` for decision making, and `for`, `while`, and `do-while` for loops.

// If-else structure
if (age > 18) {
    cout << "Adult";
} else {
    cout << "Not an adult";
}

// For loop
for(int i = 0; i < 10; i++){
    cout << i;
}

- Functions: Functions in C++ must be declared with a return type, a name, and a list of parameters.

// A function that adds two integers
int add(int x, int y) {
    return x + y;
}

- Classes and Objects: C++ supports object-oriented programming, and classes are the blueprints for creating objects.

class Person {
public:
    string name;
    int age;

    void sayHello() {
        cout << "Hello, my name is " << name;
    }
};



Comments


bottom of page