Learn C++ Programming Part 2 to Part 10

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 2: Core C++ Concepts

4. Functions

Functions are a fundamental part of C++. They allow you to modularize your code by breaking it down into smaller, reusable sections. Here are the key concepts:

  • Function Declaration and Definition
  • Parameter Passing (by value, by reference)
  • Inline Functions
  • Function Overloading
  • Recursion
// Function Declaration
int add(int a, int b);

// Function Definition
int add(int a, int b) {
    return a + b;
}

int main() {
    int sum = add(5, 3);
    std::cout << "Sum: " << sum << std::endl;
    return 0;
}

        

5. Arrays and Strings

Arrays and strings are essential for handling collections of data and text in C++. You'll learn about:

  • Arrays
  • Multidimensional Arrays
  • Strings and C-String Functions
// Single-dimensional array
int numbers[5] = {1, 2, 3, 4, 5};

// Multidimensional array
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

// String
#include <cstring>
char greeting[6] = "Hello";

int main() {
    std::cout << greeting << std::endl;
    return 0;
}

        

6. Pointers

Pointers provide a powerful way to manage memory and interact with arrays and functions:

  • Pointer Basics
  • Pointer Arithmetic
  • Pointers and Arrays
  • Pointers and Functions
  • Dynamic Memory Allocation
#include <iostream>
using namespace std;

int main() {
    int var = 20;
    int *ptr;
    ptr = &var;

    cout << "Value at ptr = " << *ptr << endl;
    return 0;
}

        

7. References

References are an alternative to pointers that provide a safer way to pass and manipulate data:

  • Reference Variables
  • Passing by Reference
#include <iostream>
using namespace std;

void swap(int &x, int &y) {
    int temp = x;
    x = y;
    y = temp;
}

int main() {
    int a = 10, b = 20;
    swap(a, b);
    cout << "a = " << a << ", b = " << b << endl;
    return 0;
}

        

Part 3: Object-Oriented Programming (OOP)

8. Classes and Objects

Classes and objects are the foundation of OOP in C++. You'll explore:

  • Class Definition
  • Access Specifiers
  • Member Functions
  • Constructors and Destructors
  • this Pointer
#include <iostream>
using namespace std;

class Car {
public:
    string brand;
    string model;
    int year;

    Car(string x, string y, int z) {
        brand = x;
        model = y;
        year = z;
    }

    void display() {
        cout << brand << " " << model << " " << year << endl;
    }
};

int main() {
    Car carObj("Toyota", "Corolla", 2020);
    carObj.display();
    return 0;
}

        

9. Inheritance

Inheritance allows you to create a new class based on an existing class. Types of inheritance include:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance
  • Access Control and Inheritance
#include <iostream>
using namespace std;

class Animal {
public:
    void eat() {
        cout << "I can eat!" << endl;
    }
};

class Dog : public Animal {
public:
    void bark() {
        cout << "I can bark!" << endl;
    }
};

int main() {
    Dog dog1;
    dog1.eat();
    dog1.bark();
    return 0;
}

        

10. Polymorphism

Polymorphism enables objects to be treated as instances of their parent class rather than their actual class. Key concepts are:

  • Function Overriding
  • Virtual Functions
  • Abstract Classes
  • Pure Virtual Functions
#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() {
        cout << "Animal sound" << endl;
    }
};

class Dog : public Animal {
public:
    void sound() override {
        cout << "Bark" << endl;
    }
};

int main() {
    Animal *a;
    Dog d;
    a = &d;
    a->sound();
    return 0;
}

        

Part 4: Advanced Topics

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;
}

        

Post a Comment

Post a Comment (0)

Previous Post Next Post