搜尋此網誌

2025年9月28日星期日

Constructors and Destructors

A constructor is a special member function of a class that is automatically called when an object is created. Its purpose is to initialize the object.

A destructor is a special member function that is automatically called when an object goes out of scope or is deleted. Its purpose is to clean up resources.

In programming, overloaded refers to a feature where multiple functions or operators share the same name but differ in their parameters or behavior.

Function Overloading
This allows you to define multiple versions of a function with the same name but different:
- Number of parameters
- Types of parameters
- Order of parameters

Operator Overloading
This lets you redefine how operators (like +, -, ==) behave for user-defined types (classes).

An implicit constructor in C++ refers to a constructor that the compiler automatically provides or uses without you explicitly defining it.

explicit: (of a statement or piece of writing) clear and easy to understand, so that you have no doubt what is meant

Heap memory is a region of memory used for dynamic allocation—you manually request and release memory during runtime using pointers.

void greet(string name) {
    cout << "Hello, " << name << "!" << endl;
}

Here, name is a parameter of the greet function.

void greet(string name) {  // 'name' is a parameter
    cout << "Hello, " << name << "!" << endl;
}

int main() {
    greet("Alice");        // "Alice" is the argument
}

Object instantiation in C++ refers to the process of creating an instance of a class—in other words, making an actual object that you can use in your program.

A default constructor in C++ is a constructor that takes no parameters (or all parameters have default values). It's automatically called when an object is created without any arguments.

In C++, new and delete are operators used for dynamic memory management—they allow you to allocate and deallocate memory on the heap manually.

In C++, this is a keyword that refers to the pointer to the current object—the instance of the class where the member function is being executed.

class Person {
private:
    string name;

public:
    void setName(string name) {
        this->name = name;  // Resolves ambiguity between parameter and member
    }

    void greet() {
        cout << "Hello, " << this->name << "!" << endl;
    }
};

Parameter is a variable passed into a function or constructor to provide input.
Member is a variable declared inside a class to represent the state of an object.

In C++, a member initializer list is a special syntax used in constructors to directly initialize class members before the constructor body runs. It's more efficient and often necessary for certain types of members.

In C++, an overloaded constructor means defining multiple constructors within the same class, each with different parameter lists. This allows objects to be created in various ways depending on the arguments provided.

// overloaded constructor 
Inventory(int capacity_i) : capacity(capacity_i) {
    items = new std::vector<std::string>();
}

Inventory(int capacity_i) is a constructor for the Inventory class.

: capacity(capacity_i) is the member initializer list, which directly initializes the member capacity with the value of capacity_i.

Inside the constructor body, items is dynamically allocated as a new std::vector on the heap.

In C++, the ~ symbol is used to define a destructor, which is a special member function that is automatically called when an object is destroyed.

Microsoft Copilot

沒有留言:

發佈留言