add member init list
This commit is contained in:
committed by
JamesFlare1212
parent
944a2129a6
commit
f40714a01e
@@ -73,8 +73,44 @@ will sort Date objects into chronological order.
|
||||
- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a member function?
|
||||
- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a member function, plus make the definition of this member function outside of the class definition?
|
||||
-->
|
||||
## 4.1 Member Initializer List in C++
|
||||
|
||||
## 4.1 Copy Constructor
|
||||
A **member initializer list** is a special syntax in C++ that allows class members to be initialized directly before the constructor body executes. It is used in the constructor definition, right after the parameter list and before the body of the constructor.
|
||||
|
||||
### Syntax
|
||||
```cpp
|
||||
ClassName(parameter_list) : member1(value1), member2(value2), ... {
|
||||
// Constructor body (optional, can be empty)
|
||||
}
|
||||
```
|
||||
|
||||
### Example
|
||||
|
||||
```cpp
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class MyClass {
|
||||
private:
|
||||
std::string name;
|
||||
|
||||
public:
|
||||
// Constructor with member initializer list
|
||||
MyClass(const std::string& initName) : name(initName) {}
|
||||
|
||||
void display() const {
|
||||
std::cout << "Name: " << name << std::endl;
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
MyClass obj("Alice");
|
||||
obj.display(); // Output: Name: Alice
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
## 4.2 Copy Constructor
|
||||
|
||||
- A copy constructor is a constructor which is used to create a new object as a copy of an existing object of the same class.
|
||||
<!-- - Copy constructors are automatically generated by the compiler if you do not provide one explicitly. However, if your class uses dynamic memory (which will be covered in next lecture), and you want a copy constructor, then you must write your own copy constructor.-->
|
||||
@@ -119,7 +155,7 @@ int main() {
|
||||
}
|
||||
```
|
||||
|
||||
## 4.2 Assignment Operator
|
||||
## 4.3 Assignment Operator
|
||||
|
||||
- The assignment operator (=) is used to copy the values from one object to another after both objects have been created.
|
||||
|
||||
@@ -195,7 +231,7 @@ A = B;
|
||||
|
||||
These two lines will: the first line creates the object A, and the second line invokes the assignment operator.
|
||||
|
||||
## 4.3 Destructor (the “constructor with a tilde/twiddle”)
|
||||
## 4.4 Destructor (the “constructor with a tilde/twiddle”)
|
||||
|
||||
A **destructor** is a special member function automatically called when an object goes out of scope or is explicitly deleted.
|
||||
<!-- - The destructor is responsible for deleting the dynamic memory “owned” by the class.
|
||||
@@ -228,7 +264,7 @@ int main() {
|
||||
|
||||
**Question**: in the above example, obj1's destructor or obj2's destructor, which one gets called first?
|
||||
|
||||
## 4.4 Overloading `operator<<` in C++
|
||||
## 4.5 Overloading `operator<<` in C++
|
||||
|
||||
### Purpose
|
||||
The `operator<<` is commonly overloaded to enable custom objects to be output using streams such as `std::cout`.
|
||||
@@ -267,7 +303,7 @@ int main() {
|
||||
|
||||
**Question:** Can we overload this operator as a member function?
|
||||
|
||||
## 4.5 Exercises
|
||||
## 4.6 Exercises
|
||||
|
||||
- [Leetcode problem 56: Merge Intervals](https://leetcode.com/problems/merge-intervals/). Solution: [p56_mergeintervals.cpp](../../leetcode/p56_mergeintervals.cpp)
|
||||
- [Leetcode problem 905: Sort Array By Parity](https://leetcode.com/problems/sort-array-by-parity/). Solution: [p905_sortarraybyparity.cpp](../../leetcode/p905_sortarraybyparity.cpp)
|
||||
|
||||
Reference in New Issue
Block a user