Learn C++ Programing Part 11 to Part 17

Learn C++ from Beginner to Advanced

Learn C++ from Beginner to Advanced

Welcome to our comprehensive guide on learning C++! We'll take you through the basics to advanced topics, ensuring you have a strong foundation in this powerful programming language.

Part 4: Advanced Topics (Continued)

11. Templates

Templates allow you to write generic and reusable code. They include:

  • Function Templates
  • Class Templates
#include <iostream>
using namespace std;

template <typename T>
T add(T a, T b) {
    return a + b;
}

int main() {
    cout << add<int>(2, 3) << endl;
    cout << add<double>(2.2, 3.3) << endl;
    return 0;
}

        

12. Exception Handling

Exception handling provides a way to manage errors and unexpected situations in your program:

  • try, catch, and throw Statements
  • Standard Exceptions
#include <iostream>
using namespace std;

int main() {
    try {
        int age = 15;
        if (age < 18) {
            throw "Age is less than 18";
        }
    } catch (const char* msg) {
        cout << "Error: " << msg << endl;
    }
    return 0;
}

        

13. Standard Template Library (STL)

The STL provides a collection of classes and functions for common data structures and algorithms:

  • Containers (Vector, List, Stack, Queue, etc.)
  • Iterators
  • Algorithms
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> numbers = {1, 2, 3, 4, 5};
    for (int n : numbers) {
        cout << n << " ";
    }
    return 0;
}

        

Part 5: File Handling

14. File Input and Output

File handling is essential for reading from and writing to files:

  • Reading and Writing Files
  • File Pointers
  • File Modes
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ofstream myfile("example.txt");
    if (myfile.is_open()) {
        myfile << "This is a line.\n";
        myfile << "This is another line.\n";
        myfile.close();
    }

    string line;
    ifstream myfile_read("example.txt");
    if (myfile_read.is_open()) {
        while (getline(myfile_read, line)) {
            cout << line << '\n';
        }
        myfile_read.close();
    }
    return 0;
}

        

15. Advanced File Handling

Explore more advanced file operations, such as:

  • Binary File Operations
  • Random Access to Files
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // Binary write
    ofstream outfile("binary.dat", ios::binary);
    int num = 12345;
    outfile.write(reinterpret_cast<char*>(&num), sizeof(num));
    outfile.close();

    // Binary read
    ifstream infile("binary.dat", ios::binary);
    infile.read(reinterpret_cast<char*>(&num), sizeof(num));
    cout << "Number: " << num << endl;
    infile.close();
    return 0;
}

        

Part 6: Data Structures

16. Linked Lists

Linked lists are a fundamental data structure in C++. Key topics include:

  • Singly Linked Lists
  • Doubly Linked Lists
  • Circular Linked Lists
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* next;
};

void printList(Node* n) {
    while (n != nullptr) {
        cout << n->data << " ";
        n = n->next;
    }
}

int main() {
    Node* head = nullptr;
    Node* second = nullptr;
    Node* third = nullptr;

    head = new Node();
    second = new Node();
    third = new Node();

    head->data = 1;
    head->next = second;

    second->data = 2;
    second->next = third;

    third->data = 3;
    third->next = nullptr;

    printList(head);
    return 0;
}

        

17. Trees

Trees are hierarchical data structures that are used in various applications, including databases and file systems:

  • Binary Trees
  • Binary Search Trees
  • Tree Traversal Methods
#include <iostream>
using namespace std;

struct Node {
    int data;
    Node* left;
    Node* right;
};

Node* newNode(int data) {
    Node* node = new Node();
    node->data = data;
    node->left = nullptr;
    node->right = nullptr;
    return node;
}

void inorderTraversal(Node* node) {
    if (node == nullptr) {
        return;
    }
    inorderTraversal(node->left);
    cout << node->data << " ";
    inorderTraversal(node->right);
}

int main() {
    Node* root = newNode(1);
    root->left = newNode(2);
    root->right = newNode(3);
    root->left->left = newNode(4);
    root->left->right = newNode(5);

    cout << "Inorder traversal: ";
    inorderTraversal(root);

    return 0;
}

        

Post a Comment

Post a Comment (0)

Previous Post Next Post