Files
CSCI-1200/lectures/05_classes_II/README.md
2023-09-14 18:15:16 -04:00

3.7 KiB
Raw Blame History

Lecture 5 --- Classes II: Sort, Non-member Operators

  • Classes in C++;
  • Non-member operators

5.1 C++ Classes

  • Nuances to remember

    • Within class scope (within the code of a member function) member variables and member functions of that class may be accessed without providing the name of the class object.
    • Within a member function, when an object of the same class type has been passed as an argument, direct access to the private member variables of that object is allowed (using the . notation).

5.2 Operator Overloading

  • When sorting objects of a custom class, we can provide a third argument to the sort function, and this third argument is a comparison function.
  • What if we do not want to provide this third argument? The answer is: define a function that creates a < operator for objects of that class! At first, this seems a bit weird, but it is extremely useful.
  • Lets start with syntax. The expressions a < b and x + y are really function calls! Syntactically, they are equivalent to operator< (a, b) and operator+ (x, y) respectively.
  • When we want to write our own operators, we write them as functions with these weird names.
  • For example, if we write:
bool operator< (const Date& a, const Date& b) {
return (a.getYear() < b.getYear() ||
(a.getYear() == b.getYear() && a.getMonth() < b.getMonth()) ||
(a.getYear() == b.getYear() && a.getMonth() == b.getMonth() && a.getDay() < b.getDay()));
}

then the statement

sort(dates.begin(), dates.end());

will sort Date objects into chronological order.

  • Really, the only weird thing about operators is their syntax.
  • We will have many opportunities to write operators throughout this course. Sometimes these will be made class member functions, but more on this in a later lecture.

5.3 Questions

  • 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, plus make the definition of this member function outside of the class definition?

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 to a function.
  • Still use the Date class as an example, if you have defined your own copy constructor whose prototype is like:
Date(const Date &other);

and when you have the following lines of code:

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