CPP
programming languages that we learn
Contents
C++
A System Programming Language
Cpp or C++ is a versatile and powerful programming language, known for the combination of low level from C and the modern high-level features added. Cpp is an extension of C, created by Bjarne Stroustrup, in the earlies of 80s, and since them is widely used and it’s known for the ability to handle complex tasks with efficiency.
Basic Sintax
- Variables
int my_var = 42;
or
- Functions
int add(x, y int) {
return x + y;
}
- Comments
// this is a single-line comment
/* this is a
multi-line
comment
*/
Assigning Variables
- Int
int a = 10;
long long b = 1000;
auto c = 5; // Type inference
- Float
double pi = 3.14159;
auto e = 2.71828; // Type inference
- Boolean
bool is_cpp_cool = true;
auto is_python_better = false; // Type inference
- Char
char letter_a = 'a';
auto unicode_heart = '❤'; // Type inference
- String
#include <string>
std::string greeting = "Hello, world!";
auto language = std::string("C++"); // Type inference
- Array
#include <array>
std::array<int, 3> numbers = {1, 2, 3};
char vowels[] = {'a', 'e', 'i', 'o', 'u'}; // Type inference
- Tuple
#include <tuple>
auto person = std::make_tuple("Alice", 30, true); // Type inference
auto point = std::make_tuple(10, 20); // Type inference
- HashMap
#include <unordered_map>
#include <string>
std::unordered_map<std::string, int> scores;
scores["Alice"] = 42;
scores["Bob"] = 56;
- Set
#include <set>
std::set<int> unique_numbers;
unique_numbers.insert(1);
unique_numbers.insert(2);
unique_numbers.insert(3);
- Queue
#include <queue>
std::queue<int> q;
q.push(1);
q.push(2);
q.push(3);
- Stack
#include <stack>
std::stack<int> s;
s.push(1);
s.push(2);
s.push(3);
- Pair
#include <utility>
std::pair<int, char> my_pair = std::make_pair(42, 'a');
- Vector (Dynamic Array)
#include <vector>
std::vector<int> numbers = {1, 2, 3, 4, 5};
numbers.push_back(6);
- LinkedList
#include <list>
std::list<int> linked_list;
linked_list.push_back(1);
linked_list.push_back(2);
linked_list.push_back(3);
- DoublyLinkedList
#include <forward_list>
std::forward_list<int> doubly_linked_list;
doubly_linked_list.push_front(3);
doubly_linked_list.push_front(2);
doubly_linked_list.push_front(1);
- BinaryTree
#include <iostream>
struct Node {
int data;
Node* left;
Node* right;
};
Node* createNode(int data) {
Node* newNode = new Node();
newNode->data = data;
newNode->left = newNode->right = nullptr;
return newNode;
}
int main() {
Node* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
return 0;
}
- HashTable
// HashTable using unordered_map
#include <unordered_map>
std::unordered_map<int, std::string> hash_table;
hash_table[1] = "One";
hash_table[2] = "Two";
hash_table[3] = "Three";
- Enum
enum class Direction {
Up,
Down,
Left,
Right,
};
Direction player_direction = Direction::Up;
- Struct
struct Person {
std::string name;
int age;
};
Person alice {"Alice", 30};
- Union
union MyUnion {
int i;
float f;
char str[20];
};
How To Create
- Class
class MyClass {
public:
int myMethod(int x, int y) {
return x + y;
}
private:
int myPrivateVariable;
};
- Interface
class Interface {
public:
virtual void myMethod() = 0; // Pure virtual function
};
- Function
int add(int x, int y) {
return x + y;
}
- Method
class MyClass {
public:
int myMethod(int x, int y) {
return x + y;
}
};
- Operator
class Complex {
public:
Complex operator+(const Complex& other) {
Complex result;
result.real = this->real + other.real;
result.imaginary = this->imaginary + other.imaginary;
return result;
}
private:
double real;
double imaginary;
};
- Exception
#include <stdexcept>
void myFunction() {
throw std::runtime_error("An error occurred");
}
- Namespace
namespace MyNamespace {
int myFunction(int x, int y) {
return x + y;
}
}
- Package/ Module
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass {
public:
int myMethod(int x, int y);
};
#endif
The Implementation:
#include "MyClass.h"
int MyClass::myMethod(int x, int y) {
return x + y;
}
- Library
Static:
# Compile MyClass.cpp into a static library
g++ -c MyClass.cpp -o MyClass.o
ar rcs libMyLibrary.a MyClass.o
Shared:
# Compile MyClass.cpp into a shared library
g++ -shared -fPIC MyClass.cpp -o libMyLibrary.so
Pros
- Efficiency: allow low-level memory management, provides a high degree of control over system resources.
- Object-Oriented: is an object-oriented programming language, providing code reusability and modularity.
- Standard Template Library (STL): the language includes a powerful STL that provides a wide range of data structures and algorithms.
- Verstility: it can be used in various domains, from game development to scientific computing.
- Community and Support: a large and active community with extensive documentation, books, and online resources.
Cons
- Complexity: C++ can be complex, especially for beginners. Its extensive feature set can lead to more intricate and error-prone code if not used carefully.
- Manual Memory Management: Like C, C++ requires manual memory management.
- Learning Curve: the learning curve can be steep, especially when dealing with advanced features, like templates and multiple inheritance.
Minimal program
#include <iostream>
int main(){
std::cout << "Hello, World!" << std::endl;
return 0;
}
Basic hello world
The basic hello world is the equal to the minimal program to write in CPP.
#include <iostream>
int main(){
std::cout << "Hello, World!" << std::endl;
return 0;
}