Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> C Language

Understanding C++ Constructors: Default, Parameterized, and Copy Explained

Understanding C++ Constructors

This tutorial delves into C++ constructors—what they are, why they matter, and how to implement default, parameterized, and copy constructors with clear examples.

A constructor is a special member function that runs automatically when an object is instantiated. In C++, the constructor shares its name with the class and does not specify a return type.

class Wall {
public:
    // Constructor
    Wall() {
        // initialization code
    }
};

The constructor must:


Default Constructor

A constructor without parameters is known as a default constructor. The Wall() example above is one.


Example 1: Default Constructor

// Demonstrating a default constructor
#include <iostream>
using namespace std;

class Wall {
private:
    double length;

public:
    // Default constructor
    Wall() {
        length = 5.5;
        cout << "Creating a wall." << endl;
        cout << "Length = " << length << endl;
    }
};

int main() {
    Wall wall1;
    return 0;
}

Output

Creating a Wall
Length = 5.5

When wall1 is instantiated, the default constructor sets length to 5.5 and prints the messages.

Note: If a class lacks an explicit constructor, the compiler automatically generates a default constructor that does nothing.


Parameterized Constructor

Parameterized constructors receive arguments to initialize member data at object creation. They are the preferred way to set up an object’s state.


Example 2: Parameterized Constructor

// Calculating the area of a wall using a parameterized constructor
#include <iostream>
using namespace std;

class Wall {
private:
    double length;
    double height;

public:
    // Parameterized constructor
    Wall(double len, double hgt) {
        length = len;
        height = hgt;
    }

    double calculateArea() {
        return length * height;
    }
};

int main() {
    Wall wall1(10.5, 8.6);
    Wall wall2(8.5, 6.3);

    cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
    cout << "Area of Wall 2: " << wall2.calculateArea();

    return 0;
}

Output

Area of Wall 1: 90.3
Area of Wall 2: 53.55

The constructor assigns the passed values to length and height, enabling calculateArea() to compute the wall’s area.


Copy Constructor

A copy constructor creates a new object as an exact copy of an existing one. It is invoked when an object is initialized from another of the same type.


Example 3: Copy Constructor

#include <iostream>
using namespace std;

class Wall {
private:
    double length;
    double height;

public:
    // Parameterized constructor
    Wall(double len, double hgt) {
        length = len;
        height = hgt;
    }

    // Copy constructor
    Wall(Wall &obj) {
        length = obj.length;
        height = obj.height;
    }

    double calculateArea() {
        return length * height;
    }
};

int main() {
    Wall wall1(10.5, 8.6);
    Wall wall2 = wall1; // Invokes copy constructor

    cout << "Area of Wall 1: " << wall1.calculateArea() << endl;
    cout << "Area of Wall 2: " << wall2.calculateArea();

    return 0;
}

Output

Area of Wall 1: 90.3
Area of Wall 2: 90.3

The copy constructor copies length and height from wall1 to wall2, ensuring both objects have identical state.

Note: Constructors primarily initialize objects, but they also enable custom logic to run during object creation.


C Language

  1. C# Constructors: Types, Examples, and Advanced Patterns
  2. Mastering Constructor Overloading in C#
  3. Understanding C++ Type Conversion: Implicit, Explicit, and Casting Techniques
  4. Mastering C++ Operators: A Complete Guide with Practical Examples
  5. C++ Comments: Best Practices for Readable, Maintainable Code
  6. Mastering the C++ break Statement
  7. Mastering C++ Functions: From Basics to Advanced Usage
  8. Mastering C++ Recursion: Concepts, Examples, and Best Practices
  9. Master Java Constructors: Types, Usage, and Practical Examples
  10. Understanding Java Constructors: Initialization and Best Practices