revising notes
This commit is contained in:
committed by
JamesFlare1212
parent
9ba953f648
commit
1aace1130f
@@ -28,9 +28,9 @@ word processor’s -> save as/export to PDF, or Google Docs -> Download -> PDF s
|
|||||||
– Computers, cell-phones, smart watches, calculators, music players, etc. are not permitted. Please do not bring your laptop, books, backpack, etc. to the exam room – leave everything in your dorm room. Unless you are coming directly from another class or sports/club meeting.
|
– Computers, cell-phones, smart watches, calculators, music players, etc. are not permitted. Please do not bring your laptop, books, backpack, etc. to the exam room – leave everything in your dorm room. Unless you are coming directly from another class or sports/club meeting.
|
||||||
– Do not bring your own scratch paper. We will provide scratch paper. -->
|
– Do not bring your own scratch paper. We will provide scratch paper. -->
|
||||||
|
|
||||||
# Lecture 4 --- Classes II: Sort, Non-member Operators
|
# Lecture 4 --- Classes II: Copy Constructor, Assignment Operator, Destructor, and Non-member Operators
|
||||||
|
|
||||||
- Classes in C++;
|
<!--- Classes in C++;
|
||||||
- Non-member operators
|
- Non-member operators
|
||||||
|
|
||||||
## 4.1 C++ Classes
|
## 4.1 C++ Classes
|
||||||
@@ -72,28 +72,48 @@ will sort Date objects into chronological order.
|
|||||||
- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a non-member function?
|
- Can you solve leetcode problem 905 with an overloaded operator <, and make this overloaded operator < a non-member function?
|
||||||
- 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?
|
||||||
- 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?
|
- 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.4 Copy Constructor
|
## 4.1 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.
|
- 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 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 to a function.
|
- 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 to a function.
|
||||||
- Still use the *Date* class as an example, if you have defined your own copy constructor whose prototype is like:
|
|
||||||
|
### Copy Constructor Example
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
Date(const Date &other);
|
#include <iostream>
|
||||||
|
|
||||||
|
class MyClass {
|
||||||
|
private:
|
||||||
|
int value;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
MyClass(int val) : value(val) {}
|
||||||
|
|
||||||
|
// Copy Constructor
|
||||||
|
MyClass(const MyClass& other) {
|
||||||
|
value = other.value;
|
||||||
|
std::cout << "Copy Constructor Called!" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void display() const {
|
||||||
|
std::cout << "Value: " << value << std::endl;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
MyClass obj1(10);
|
||||||
|
MyClass obj2 = obj1; // Copy constructor is called here
|
||||||
|
|
||||||
|
obj2.display(); // Output: Value: 10
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
and when you have the following lines of code:
|
## 4.2 Assignment Operator
|
||||||
|
|
||||||
```cpp
|
|
||||||
Date a;
|
|
||||||
Date b = a;
|
|
||||||
```
|
|
||||||
|
|
||||||
The first statement will call the default constructor, while the second statement will call the copy constructor.
|
|
||||||
|
|
||||||
## 4.5 Assignment Operator
|
|
||||||
|
|
||||||
- The assignment operator (=) is used to copy the values from one object to another after both objects have been created.
|
- The assignment operator (=) is used to copy the values from one object to another after both objects have been created.
|
||||||
|
|
||||||
@@ -169,7 +189,7 @@ A = B;
|
|||||||
|
|
||||||
These two lines will: the first line creates the object A, and the second line invokes the assignment operator.
|
These two lines will: the first line creates the object A, and the second line invokes the assignment operator.
|
||||||
|
|
||||||
## 4.6 Destructor (the “constructor with a tilde/twiddle”)
|
## 4.3 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.
|
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.
|
<!-- - The destructor is responsible for deleting the dynamic memory “owned” by the class.
|
||||||
@@ -200,7 +220,46 @@ int main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
## 4.7 Exercises
|
## 4.4 Overloading `operator<<` in C++
|
||||||
|
|
||||||
|
### Purpose
|
||||||
|
The `operator<<` is commonly overloaded to enable custom objects to be output using streams such as `std::cout`.
|
||||||
|
|
||||||
|
### Example
|
||||||
|
```cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
private:
|
||||||
|
std::string name;
|
||||||
|
int age;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// Constructor
|
||||||
|
Person(const std::string& name, int age) : name(name), age(age) {}
|
||||||
|
|
||||||
|
// Public getters
|
||||||
|
std::string getName() const { return name; }
|
||||||
|
int getAge() const { return age; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Non-member function
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Person& person) {
|
||||||
|
os << "Name: " << person.getName() << ", Age: " << person.getAge();
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Person p("Alice", 30);
|
||||||
|
std::cout << p << std::endl; // Output: Name: Alice, Age: 30
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
**Question:** Can we overload this operator as a member function?
|
||||||
|
|
||||||
|
## 4.5 Exercises
|
||||||
|
|
||||||
- [Leetcode problem 56: Merge Intervals](https://leetcode.com/problems/merge-intervals/). Solution: [p56_mergeintervals.cpp](../../leetcode/p56_mergeintervals.cpp)
|
- [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 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