From 1aace1130fbf7bbc44e14e7eb77fbfe248f18e26 Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Fri, 17 Jan 2025 11:57:34 -0500 Subject: [PATCH] revising notes --- lectures/04_classes_II/README.md | 95 ++++++++++++++++++++++++++------ 1 file changed, 77 insertions(+), 18 deletions(-) diff --git a/lectures/04_classes_II/README.md b/lectures/04_classes_II/README.md index 0606a1a..5bc1077 100644 --- a/lectures/04_classes_II/README.md +++ b/lectures/04_classes_II/README.md @@ -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. – 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++; + -## 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. -- 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. -- Still use the *Date* class as an example, if you have defined your own copy constructor whose prototype is like: + +### Copy Constructor Example ```cpp -Date(const Date &other); +#include + +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: - -```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 +## 4.2 Assignment Operator - 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. -## 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.