DEV Community

Cover image for Introduction to C++
Madhav Ganesan
Madhav Ganesan

Posted on • Updated on

Introduction to C++

C++ (or “C-plus-plus”)

History:

C++ was developed by Danish computer scientist Bjarne Stroustrup at AT&T Bell Labs in 1979 as an extension of the C programming language. It emerged from an analysis of the UNIX kernel to explore how it could be distributed across a network. The initial version of C++ was publicly released in 1983.

Image description

Paradigms:

Procedural Programming
Procedure is like a function that doesn't return anything;

#include <iostream>

void greet() {
    std::cout << "Hello, World!" << std::endl;
}

int main() {
    greet();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Object Oriented Programming

#include <iostream>
#include <string>
using namespace std;

class Animal {
public:
    Animal(const string& name) : name(name) {}
    void speak() const {
        cout << "My name is " << name << endl;
    }
private:
    string name;
};

int main() {
    Animal dog("Doggy");
    dog.speak();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Functional Programming

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4, 5};


    for (const auto &n : vec) {
        cout << n << " ";
    }

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Low-level Programming

#include <iostream>

int main() {
    int a = 5;
    int *p = &a;
    std::cout << "Value of a: " << a << std::endl;
    std::cout << "Address of a: " << p << std::endl;
    std::cout << "Value at address p: " << *p << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Libraries and Frameworks:

External Libraries:

Third-party libraries (e.g., Boost for C++).

Standard Library:

Built-in libraries provided by the language (e.g., STL in C++).

The C++ Standard Library is a collection of classes and functions, which are part of the ISO C++ standard and come bundled with every C++ compiler. It provides a wide range of functionalities, including containers, algorithms, iterators, input/output facilities, and more.

std namespace is used differentiate functions present in different libraries such as , etc.

Components of the C++ Standard Library

Containers: vector, list, deque, set, map, array, etc.
Algorithms: sort, find, accumulate, copy, etc.
Iterators: iterator, reverse_iterator, ostream_iterator, etc.
Function Objects: std::less, std::greater, custom functors.

Streams: std::cin, std::cout, std::cerr, std::ifstream, std::ofstream.
Stream Manipulators: std::endl, std::setw, std::setprecision.

String Class: std::string, std::wstring.

Smart Pointers: std::unique_ptr, std::shared_ptr, std::weak_ptr.
Pairs and Tuples: std::pair, std::tuple.
Functional Utilities: std::function, std::bind, std::mem_fn.
Memory Management
Allocators: std::allocator.
Memory Functions: std::allocate_shared, std::make_unique.

Threads: std::thread.
Mutexes and Locks: std::mutex, std::lock_guard.

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <memory>

int main() {
    // Containers and algorithms
    std::vector<int> vec = {1, 2, 3, 4, 5};
    std::sort(vec.begin(), vec.end(), std::greater<int>());
    int sum = std::accumulate(vec.begin(), vec.end(), 0);

    // Output the results
    std::cout << "Sorted vector in descending order: ";
    for (const auto& val : vec) {
        std::cout << val << " ";
    }
    std::cout << std::endl;
    std::cout << "Sum of elements: " << sum << std::endl;

    // Smart pointers
    std::unique_ptr<int> ptr = std::make_unique<int>(10);
    std::cout << "Value of unique_ptr: " << *ptr << std::endl;

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Performance

C++ is a compiled language where source is translated into machine code by a compiler. Compiled languages generally have better performance while interpreted languages may be slower.

Compiler:

Turbo C++

Type System:

Static Typing: Types are checked at compile-time.
Strong Typing: Strict type rules are enforced.
Static Type Checking: Ensures type correctness before execution.

Abstraction:

Both high-level and low-level abstraction

Important facts:

All member functions and variables of a class are private by default.

Usage:

Both C and C++ are widely used in system programming, including operating systems, embedded systems, and hardware-level programming.


Feel free to reach out if you have any questions or need further assistance. 😊📁✨

Top comments (2)

Collapse
 
anderspersson profile image
Anders Persson

Nice article, and if you like to get the code example better use, syntax highligts,
there is a great post about it
dev.to/imjoshellis/quick-tip-how-t...

Sometimes the syntax, can be a tricky to find, but i think C++ use CPP

Collapse
 
madgan95 profile image
Madhav Ganesan

Thx for your suggestion mate✌️
Edited it..