This commit is contained in:
Jidong Xiao
2025-02-20 23:00:55 -05:00
committed by JamesFlare
parent f95296f4d1
commit 3da5d38338
6 changed files with 353 additions and 0 deletions

View 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;
}