Explicitly Creating the Copy Constructor and Assignment Operator - C++ Class

C++ examples for Class:Constructor

Description

Explicitly Creating the Copy Constructor and Assignment Operator

#include <cinttypes>
#include <iostream>
#include <string>

using namespace std;

class Car
{
private:
    string m_Name;
    int m_NumberOfWheels{};

public:
    Car() = default;

    Car(string name, int numberOfWheels)
        : m_Name{ name }
        , m_NumberOfWheels{ numberOfWheels }
    {

    }

    ~Car()
    {
        cout << m_Name << " at " << this << " is being destroyed!" << endl;
    }

    Car(const Car& other) = default;
    Car& operator=(const Car& other) = default;

    int GetNumberOfWheels()
    {
        return m_NumberOfWheels;
    }
};

Related Tutorials