adding the overloading example
This commit is contained in:
@@ -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: + - * & ~ ! ++ -- -> ->*
|
||||
|
||||
32
lectures/13_operators/overloading_friend.cpp
Normal file
32
lectures/13_operators/overloading_friend.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Overloading the output stream operator as a friend function.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
32
lectures/13_operators/overloading_member.cpp
Normal file
32
lectures/13_operators/overloading_member.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// Overloading the output stream operator as a member function.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
31
lectures/13_operators/overloading_non_member.cpp
Normal file
31
lectures/13_operators/overloading_non_member.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
// Overloading the output stream operator as a non-member function.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
}
|
||||
Reference in New Issue
Block a user