diff --git a/lectures/13_operators/README.md b/lectures/13_operators/README.md index c8c5682..a3ab3df 100644 --- a/lectures/13_operators/README.md +++ b/lectures/13_operators/README.md @@ -156,6 +156,12 @@ function of the Complex class. This is why stream operators are never member fun - Stream operators are either ordinary non-member functions (if the operators can do their work through the public class interface) or friend functions (if they need non public access). +You can compile and run these three examples, in which the output stream operators are overloaded as a non-member function, a friend function, and a member function. + +- [Example 1](overloading_non_member.cpp) +- [Example 2](overloading_friend.cpp) +- [Example 3](overloading_member.cpp) - pay attention to the main function, does it surprise you? + ## 13.10 Summary of Operator Overloading in C++ - Unary operators that can be overloaded: + - * & ~ ! ++ -- -> ->* diff --git a/lectures/13_operators/overloading_friend.cpp b/lectures/13_operators/overloading_friend.cpp new file mode 100644 index 0000000..4a37e27 --- /dev/null +++ b/lectures/13_operators/overloading_friend.cpp @@ -0,0 +1,32 @@ +// Overloading the output stream operator as a friend function. + +#include + +class MyClass { +private: + int value; + +public: + MyClass(int val) : value(val) {} + friend std::ostream& operator<<(std::ostream& os, const MyClass& obj); + + // getter function to access private member + int getValue() const { + return value; + } +}; + +// overload the output stream operator as a non-member function +std::ostream& operator<<(std::ostream& os, const MyClass& obj) { + os << "Value: " << obj.value; + return os; +} + +int main() { + MyClass obj(42); + + // output the object using the overloaded operator + std::cout << obj << std::endl; + + return 0; +} diff --git a/lectures/13_operators/overloading_member.cpp b/lectures/13_operators/overloading_member.cpp new file mode 100644 index 0000000..d98abef --- /dev/null +++ b/lectures/13_operators/overloading_member.cpp @@ -0,0 +1,32 @@ +// Overloading the output stream operator as a member function. + +#include + +class MyClass { +private: + int value; + +public: + MyClass(int val) : value(val) {} + + // getter function to access private member + int getValue() const { + return value; + } + + // overload the output stream operator as a non-member function + std::ostream& operator<<(std::ostream& os) { + os << "Value: " << value; + return os; + } + +}; + +int main() { + MyClass obj(42); + + // output the object using the overloaded operator + obj << std::cout << std::endl; + + return 0; +} diff --git a/lectures/13_operators/overloading_non_member.cpp b/lectures/13_operators/overloading_non_member.cpp new file mode 100644 index 0000000..97b7485 --- /dev/null +++ b/lectures/13_operators/overloading_non_member.cpp @@ -0,0 +1,31 @@ +// Overloading the output stream operator as a non-member function. + +#include + +class MyClass { +private: + int value; + +public: + MyClass(int val) : value(val) {} + + // getter function to access private member + int getValue() const { + return value; + } +}; + +// overload the output stream operator as a non-member function +std::ostream& operator<<(std::ostream& os, const MyClass& obj) { + os << "Value: " << obj.getValue(); + return os; +} + +int main() { + MyClass obj(42); + + // output the object using the overloaded operator + std::cout << obj << std::endl; + + return 0; +}