Classes and Objects in C++

Classes and objects in C++ are core concepts of Object-Oriented Programming (OOP), providing features such as encapsulation, inheritance, and polymorphism. Below is a detailed introduction to constructors, destructors, and operator overloading:

1. Basics of Classes and Objects

A class is a user-defined data type that encapsulates data (member variables) and operations (member functions). An object is an instance of a class.

Example:

class Rectangle {private:    int width;  // Member variable    int height;public:    // Member function    int getArea() { return width * height; }    void setDimensions(int w, int h) { width = w; height = h; }};

2. Constructor

A constructor is used to initialize an object, has the same name as the class, and has no return type.

1. Default Constructor

  • No parameters or all parameters have default values.

  • If no constructor is defined, the compiler will automatically generate a default constructor.

Example:

class Rectangle {private:    int width, height;public:    // Default constructor    Rectangle() {        width = 0;        height = 0;    }    // Constructor with default parameters (can also serve as a default constructor)    Rectangle(int w = 0, int h = 0) {        width = w;        height = h;    }};

2. Parameterized Constructor

  • Accepts parameters to initialize the object.

Example:

class Rectangle {public:    // Parameterized constructor    Rectangle(int w, int h) {        width = w;        height = h;    }};// Create object using parameterized constructorRectangle rect(5, 10);  // width=5, height=10

3. Copy Constructor

  • Creates a new object by copying the values of an existing object.

  • Format: ClassName(const ClassName& other).

Example:

class Rectangle {public:    // Copy constructor    Rectangle(const Rectangle& other) {        width = other.width;        height = other.height;    }};Rectangle rect1(5, 10);Rectangle rect2(rect1);  // Use copy constructor to copy rect1

Shallow Copy vs Deep Copy:

  • Shallow Copy: Directly copies the values of member variables (default behavior).

  • Deep Copy: Allocates new memory on the heap and copies the contents (used for pointer members).

3. Destructor

  • Automatically called when an object is destroyed, releasing resources (such as dynamic memory, file handles, etc.).

  • Name is ~ClassName(), has no parameters, and no return type.

Example:

class ArrayWrapper {private:    int* data;public:    ArrayWrapper(int size) {        data = new int[size];  // Dynamically allocate memory    }    ~ArrayWrapper() {        delete[] data;  // Release memory    }};

4. Operator Overloading

By defining special member functions, the behavior of operators can be redefined.

1. Member Function Overloading

  • The left operand must be an object of the current class.

Example:

Overloading the + operator

class Complex {private:    double real, imag;public:    // Overload + operator    Complex operator+(const Complex& other) const {        return Complex(real + other.real, imag + other.imag);    }};

2. Non-Member Function Overloading

  • Used in scenarios where the operands do not include the current class object (e.g., ostream& operator<< ).

Example:

Overloading the << operator

class Complex {    // Declare friend function to access private members    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Complex&amp; c);};// Non-member function overloadstd::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, const Complex&amp; c) {    os &lt;&lt; c.real &lt;&lt; 

Leave a Comment