Shallow copying
Because C++ does not know much about your class, the default copy constructor and default assignment operators it provides use a copying method known as a memberwise copy [also known as a shallow copy]. This means that C++ copies each member of the class individually [using the assignment operator for overloaded operator=, and direct initialization for the copy constructor]. When classes are simple [e.g. do not contain any dynamically allocated memory], this works very well.
For example, let’s take a look at our Fraction class:
#include
#include
class Fraction
{
private:
int m_numerator { 0 };
int m_denominator { 1 };
public:
// Default constructor
Fraction[int numerator = 0, int denominator = 1]
: m_numerator{ numerator }
, m_denominator{ denominator }
{
assert[denominator != 0];
}
friend std::ostream& operator