adding example of copy constructors
This commit is contained in:
@@ -39,11 +39,33 @@ will sort Date objects into chronological order.
|
||||
|
||||
## 5.3 Questions
|
||||
|
||||
- Can you solve leetcode problem 905 with a overloaded operator <?
|
||||
- Can you solve leetcode problem 905 with a overloaded operator <, and make this overloaded operator < a member function?
|
||||
- Can you solve leetcode problem 905 with a overloaded operator <, and make this overloaded operator < a member function, plus make the definition of this member function outside of the class definition?
|
||||
- Can you solve leetcode problem 905 with an overloaded operator <?
|
||||
- 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?
|
||||
|
||||
## 5.4 Exercises
|
||||
## 5.4 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.
|
||||
- Copy constructors get called when you create a new object by copying an existing object using the assignment operator (=), or when you pass an object by value (NOT by a reference) to a function.
|
||||
- Still use the *Date* class as an example, if you have defined your own copy constructor whose prototype is like:
|
||||
|
||||
```cpp
|
||||
Date(const Date &other);
|
||||
```
|
||||
|
||||
and when you have the following lines of code:
|
||||
|
||||
```cpp
|
||||
Date a;
|
||||
Date b = a;
|
||||
```
|
||||
|
||||
The first statement will call the default constructor, while the second statement will call the copy constructor.
|
||||
|
||||
## 5.5 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)
|
||||
- [Leetcode problem 1929: Concatenation of Array
|
||||
](https://leetcode.com/problems/concatenation-of-array/). Solution: [p1929_concatenationofarray.cpp](../../leetcode/p1929_concatenationofarray.cpp)
|
||||
|
||||
Reference in New Issue
Block a user