1. Constructors :

Constructors are special member functions that are automatically called when an object is created. They are used to initialize the object's member variables.

class Person {
public:
    // Parameterized constructor
    Person(const std::string& n, int a) : name(n), age(a) {}

    void introduce() {
        std::cout << "Hello, my name is " << name << " and I am " << age << " years old." << std::endl;
    }

private:
    std::string name;
    int age;
};

int main() {
    // Creating objects using the constructor
    Person person1("David", 28);
    Person person2("Eve", 22);

    person1.introduce();
    person2.introduce();

    return 0;
}

when an object is constructed the function is called

Person(const std::string& n, int a) : name(n), age(a) {}

name variable takes the matter of n

age variable takes the matter of a

  1. Destructors :

Destructors are special member functions that are automatically called when an object is destroyed. They are used to clean up resources and perform any necessary cleanup operations.

class MyClass {
public:
    // Constructor
    MyClass() {
        std::cout << "Constructor called." << std::endl;
    }

    // Destructor
    ~MyClass() {
        std::cout << "Destructor called." << std::endl;
    }
};

int main() {
    MyClass obj; // Constructor called
    // obj goes out of scope here, Destructor called

    return 0;
}

This is the method of Declaring a constructor and a Destructor

// Constructor MyClass() { std::cout << "Constructor called." << std::endl; }

// Destructor ~MyClass() { std::cout << "Destructor called." << std::endl; }

3.Access Specifiers :

class MyClass {
public:
    int publicVar;

private:
    int privateVar;

protected:
    int protectedVar;
};

Inheritance

Inheritance is a powerful mechanism that allows you to create a new class (subclass or derived class) based on an existing class (superclass or base class). The derived class inherits the properties (data members) and behaviors (member functions) of the base class. This promotes code reuse, as you can define common attributes and methods in the base class and extend or specialize them in the derived classes.

There are child classes and parent classes

child class can get the method and Attribute

Abstract Classs

Abstract classes provide a powerful mechanism for achieving polymorphism, encapsulation, and code organization by defining a common interface that multiple classes can adhere to while allowing for diverse implementations.

see this is helps in making a polymorphism

class Shape {
public:
    virtual double area() const {
        return 0.0;
    }
};

class Circle : public Shape {
private:
    double radius;

public:
    Circle(double r) : radius(r) {}

    double area() const override {
        return 3.14159 * radius * radius;
    }
};

class Rectangle : public Shape {
private:
    double width, height;

public:
    Rectangle(double w, double h) : width(w), height(h) {}

    double area() const override {
        return width * height;
    }
};

Polymorphism is a key concept in object-oriented programming (OOP) that allows objects of different classes to be treated as objects of a common superclass. It enables you to write code that can work with objects of various types in a uniform and flexible way. Polymorphism is achieved through the use of inheritance, virtual functions, and class hierarchies.

There are two main types of polymorphism in C++:

  1. Compile-Time Polymorphism (Static Polymorphism) : This is achieved through function overloading and operator overloading. The appropriate function or operator is determined at compile time based on the number and types of arguments. This type of polymorphism is resolved during compilation.
  2. Run-Time Polymorphism (Dynamic Polymorphism) : This is achieved using virtual functions and is more closely associated with inheritance and class hierarchies. The specific function to be called is determined at runtime based on the actual object type. This type of polymorphism is resolved during program execution.

Let's focus on run-time polymorphism using virtual functions:

Virtual Functions :

A virtual function is a member function declared in a base class with the virtual keyword. Derived classes can provide their own implementations of the virtual function, and the appropriate implementation is called based on the actual object type at runtime.

Key points about virtual functions:

  • Virtual functions are declared in the base class with thevirtual keyword.
  • They are meant to be overridden (redefined) in derived classes.
  • When a virtual function is called using a base class pointer or reference, the implementation of the derived class is invoked.

Example:

cppCopy code
class Shape { public: virtual void draw() const { // Default implementation } }; class Circle : public Shape { public: void draw() const override { std::cout << "Drawing a circle." << std::endl; } }; class Rectangle : public Shape { public: void draw() const override { std::cout << "Drawing a rectangle." << std::endl; } }; int main() { Shape* shapes[2]; shapes[0] = new Circle(); shapes[1] = new Rectangle(); for (int i = 0; i < 2; ++i) { shapes[i]->draw(); // Calls the appropriate implementation delete shapes[i]; } return 0; }

In this example, the Shape class defines a virtual function draw(), and both Circle and Rectangle classes override this function. When the draw() function is called on objects of the derived classes through a base class pointer, the appropriate overridden implementation is invoked based on the actual object type.

Polymorphism allows you to write more generic and flexible code by working with objects at a higher level of abstraction. It enables you to build software systems that can adapt and extend to new types of objects without significant code changes, making your code more adaptable and maintainable.

Abstract class

class Shape {
public:
    virtual double area() const = 0;  // Pure virtual function

    virtual void draw() const {
        // Default implementation
    }
};

References and pointers are both important concepts in C++ that allow you to work with memory and data in different ways. They serve similar purposes but have distinct characteristics and use cases. Let's break down each concept and explain their meanings.

Pointers :

A pointer is a variable that holds the memory address of another variable. Pointers allow you to indirectly access and manipulate data by referring to its memory location. Here's a brief overview:

  • Declaration : You declare a pointer by specifying the data type it points to, followed by an asterisk (*), and then the pointer's name.
    cppCopy code
    int *ptr; // Declares a pointer to an integer
  • Initialization : A pointer needs to be initialized with the memory address it points to. You use the address-of operator (&) to get the address of a variable.
    cppCopy code
    int num = 42; int *ptr = &num; // Pointer ptr now points to the memory address of num
  • Dereferencing : You can use the pointer dereference operator (*) to access the value stored at the memory address pointed to by the pointer.
    cppCopy code
    int value = *ptr; // Retrieves the value stored at the address pointed to by ptr
  • Dynamic Memory Allocation : Pointers are commonly used to allocate memory dynamically at runtime using new and deallocate it using delete.
    cppCopy code
    int *dynPtr = new int; // Allocates memory for an integer on the heap *dynPtr = 42; // Store value 42 in the memory pointed to by dynPtr delete dynPtr; // Deallocate the memory

References :

A reference is an alias or an alternative name for an existing variable. References allow you to access and manipulate the original data directly. Here's a brief overview:

  • Declaration : You declare a reference by specifying the data type it refers to, followed by an ampersand (&), and then the reference's name.
    cppCopy code
    int num = 42; int &ref = num; // Declares a reference to an integer that refers to num
  • Initialization : A reference must be initialized when declared, and once initialized, it cannot be made to refer to another variable.
  • No Dereferencing : Unlike pointers, you don't need to use an operator to access the value referred to by a reference. You directly use the reference variable itself.
    cppCopy code
    int value = ref; // Retrieves the value referred to by ref (same as num)
  • Cannot Be NULL : References must always refer to valid objects and cannot be uninitialized or NULL.