diff options
author | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-09-06 12:24:03 +0200 |
---|---|---|
committer | Hugo Hörnquist <hugo@lysator.liu.se> | 2022-09-06 12:24:03 +0200 |
commit | 95bfddc4329119535cf2fb47bef3c1b87f61e411 (patch) | |
tree | 69c5b24bfe8fb7028450aaed7f0abf38f40329da /C++.wiki | |
parent | ons 31 aug 2022 17:37:49 CEST (diff) | |
download | wiki-public-95bfddc4329119535cf2fb47bef3c1b87f61e411.tar.gz wiki-public-95bfddc4329119535cf2fb47bef3c1b87f61e411.tar.xz |
tis 6 sep 2022 12:24:03 CEST
Diffstat (limited to 'C++.wiki')
-rw-r--r-- | C++.wiki | 83 |
1 files changed, 3 insertions, 80 deletions
@@ -1,90 +1,13 @@ = Contents = - [[#C++ Examples|C++ Examples]] - - [[#C++ Examples#Friend operator|Friend operator]] - [[#C++ Examples#Time Header|Time Header]] = C++ Examples = -== Friend operator == +- [[C++/Friend Operator]] +- [[C++/Time Header]] +- [[C++/Threeway Comparison]] -{{{c++ -#include <iostream> - -class T { - int x; - -public: - T (int x); - - friend std::ostream& operator<<(std::ostream&, const T&); -}; - -T::T (int x) { - this->x = x; -} - - -std::ostream& operator<<(std::ostream& out, const T& t) { - out << "T {" << t.x << "}"; - return out; -} - -int main () { - T t(10); - - std::cout << t << std::endl; -} -}}} == Time Header == -{{{c++ -#pragma once - -#include <ostream> - -enum timetype { - _day, _hour, _minute, _second -}; - -class Time { -private: - - int _days = 0; - int _hours = 0; - int _minutes = 0; - int _seconds = 0; - -public: - Time() { }; - Time(int n, timetype type); - - int to_seconds() const; - - friend std::ostream& operator<<(std::ostream&, const Time&); - friend Time operator * (int, Time); - - Time operator + (const Time&); - Time operator , (const Time& t) { return *this + t; } - Time operator && (const Time& t) { return *this + t; } -}; - -#define days * Time(1, _day) -#define day days -#define hours * Time(1, _hour) -#define hour hours -#define minutes * Time(1, _minute) -#define minute minutes -#define seconds * Time(1, _second) -#define second seconds - -int main() { - std::cout - << 5 days << std::endl - << 7 minutes << std::endl - << (5 days and 3 seconds) << std::endl - << (7 days, 7 minutes and 3 seconds) << std::endl - << (9 hours and 1 second) << std::endl - ; -} -}}} |