Object-Oriented Programming in C++ (1)

Constructor

  1. 1. The compiler provides a default constructor only when no constructors are defined. If a constructor is defined and you want to use the default constructor, you must explicitly define the default constructor.
  2. 2. There can only be one default constructor, but multiple constructors can exist.
  3. 3. The default constructor can have no parameters; if it does, all parameters must have default values.

Initialization Methods for Constructors

  1. 1. Initialization list
  2. 2. Assignment within the constructor body
  3. 3. Using default parameters
  4. 4. Delegating constructors
class Person
{
public:
    const int id_;  // const member variables can only be initialized using the initialization list
    string name_;
    short age_;
    double height_;

    Person(int id, const string& name, short age)
      : id_(id), name_(name) {  // 1. Initialization list
      age_ = age;
      height_ = 0.0;  // 2. Assignment within the constructor body
    }
    Person(int id, const string& name, double height, short age=0)//3. Using default parameters
      : Person(id, name, age) {  // 4. Delegating constructors
      height_ = height;
    }
};

Destructor

  1. 1. If not defined, the compiler provides a default destructor.
  2. 2. Generally, defining a virtual destructor is a good practice.

Copy Constructor

The copy constructor is used to copy an object into a newly created object. Its prototype is as follows:

ClassName(const ClassName &);  // Declaration

ClassName::ClassName(const ClassName & cls) {}  // Definition

If not defined, the compiler provides a default copy constructor, which performs a member-wise copy of non-static member variables (this is also known as shallow copy), copying the values of the members.

When is the copy constructor called?

  1. 1. When a new object is created and initialized with an existing object of the same type.
    // Assuming Person class, existing instance object p1
    Person p2(p1);
    Person p2 = p1;
    Person p2 = Person(p1);
    Person* ptr = new Person(p1);
  2. 2. When generating a copy of an object, such as when passing an object by value to a function or returning an object from a function.

Assignment Operator

The assignment operator is overloaded to assign class objects, and its prototype is as follows:

ClassName& operator=(const ClassName &); // Declaration

ClassName& ClassName::operator=(const ClassName & cls)  // Definition
{
  if (this == &cls)
  {
    return *this;
  }
  ...
  return *this;
}

When is the assignment operator called? When assigning an existing object to another existing object.

If not defined, the compiler provides a default assignment operator, which behaves like the copy constructor.

Be particularly careful when using new in constructors:

  1. 1. If a pointer member is initialized using new in the constructor, then delete should be used in the destructor.
  2. 2. If there are multiple constructors, new must be used in the same way, either with brackets or without. Because there is only one destructor, <span>new</span> and <span>delete</span>, <span>new[]</span> and <span>delete[]</span> must correspond.
  3. 3. A copy constructor should be defined to initialize one object as another object through deep copy.
  4. 4. The assignment operator should be overloaded to copy one object to another object through deep copy.

Leave a Comment