Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Overview of C++ Language

1. Introduction

C++ is a general-purpose programming language created by Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". It has imperative, object-oriented and generic programming features, while also providing facilities for low-level memory manipulation.

2. History of C++

C++ was designed with a bias towards system programming and embedded, resource-constrained software and large systems, with performance, efficiency and flexibility of use as its design highlights. The language was standardized by the International Organization for Standardization (ISO) in 1998, with the latest standard version approved and published by ISO in December 2020 as ISO/IEC 14882:2020 (also known as C++20).

3. Features of C++

C++ includes several features not found in C, such as:

  • Object-oriented programming
  • Data abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance
  • Templates
  • Standard Template Library (STL)

4. Basic Syntax

Below is a simple example of a C++ program:

// Simple C++ program to display "Hello World"
#include <iostream>

using namespace std;

int main() {
    cout << "Hello, World!" << endl;
    return 0;
}
                

This program includes:

  • #include <iostream>: This is a preprocessor command that includes the standard input-output stream library.
  • using namespace std;: This line tells the compiler to use the standard namespace.
  • int main(): This is the main function where program execution begins.
  • cout << "Hello, World!" << endl;: This line outputs "Hello, World!" to the console.
  • return 0;: This statement terminates the main function and returns the value 0 to the calling process.

5. Data Types

C++ supports several data types, including:

  • int: Integer type
  • float: Floating point type
  • double: Double-precision floating point type
  • char: Character type
  • bool: Boolean type
  • void: Represents the absence of type

6. Variables

Variables in C++ are used to store data that can be manipulated later in the program. Below is an example:

#include <iostream>

using namespace std;

int main() {
    int a = 10;
    float b = 20.5;
    char c = 'A';
    
    cout << "Integer: " << a << endl;
    cout << "Float: " << b << endl;
    cout << "Character: " << c << endl;
    
    return 0;
}
                

7. Control Structures

C++ provides control structures such as if-else statements, loops (for, while, do-while), and switch-case statements. Here's an example of a for loop:

#include <iostream>

using namespace std;

int main() {
    for (int i = 1; i <= 5; i++) {
        cout << "i: " << i << endl;
    }
    return 0;
}
                

8. Functions

Functions are used to perform specific tasks and can be reused. Below is an example of a function in C++:

#include <iostream>

using namespace std;

void greet() {
    cout << "Hello, User!" << endl;
}

int main() {
    greet(); // Function call
    return 0;
}
                

9. Object-Oriented Programming

C++ supports object-oriented programming (OOP) which includes classes and objects. Here is an example:

#include <iostream>

using namespace std;

class Car {
public:
    string brand;
    string model;
    int year;
    
    void displayInfo() {
        cout << "Brand: " << brand << endl;
        cout << "Model: " << model << endl;
        cout << "Year: " << year << endl;
    }
};

int main() {
    Car car1;
    car1.brand = "Toyota";
    car1.model = "Camry";
    car1.year = 2021;
    car1.displayInfo();
    
    return 0;
}
                

10. Conclusion

C++ is a powerful language with a rich set of features that allows for low-level memory manipulation, object-oriented programming, and more. This overview has provided a basic understanding of the language and its capabilities. To become proficient in C++, it is essential to practice regularly and study more advanced topics such as templates, exception handling, and the Standard Template Library (STL).