Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a C++ implementation of the classes Cost, Square, Inc, and Print that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; }; class Cost : public Expression { private: int value; public: Cost(int val) : value(val) {} int evaluate() const override { return value; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Square : public Expression { private: const Expression& prev; public: Square() : prev(Cost(0)) {} Square(const Expression& p) : prev(p) {} int evaluate() const override { int val = prev.evaluate(); return val * val; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Inc : public Expression { private: const Expression& prev; public: Inc() : prev(Cost(0)) {} Inc(const Expression& p) : prev(p) {} int evaluate() const override { return prev.evaluate() + 1; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Print : public Expression { private: const Expression& prev; public: Print() : prev(Cost(0)) {} Print(const Expression& p) : prev(p) {} int evaluate() const override { int result = prev.evaluate(); std::cout << result << std::endl; return result; } }; int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | Cost(5) >> Square() >> Inc() >> Inc() >> Print(); |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; }; class Cost : public Expression { private: int value; public: Cost(int val) : value(val) {} int evaluate() const override { return value; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Square : public Expression { private: const Expression& prev; public: Square() : prev(Cost(0)) {} Square(const Expression& p) : prev(p) {} int evaluate() const override { int val = prev.evaluate(); return val * val; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Inc : public Expression { private: const Expression& prev; public: Inc() : prev(Cost(0)) {} Inc(const Expression& p) : prev(p) {} int evaluate() const override { return prev.evaluate() + 1; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Print : public Expression { private: const Expression& prev; public: Print() : prev(Cost(0)) {} Print(const Expression& p) : prev(p) {} int evaluate() const override { int result = prev.evaluate(); std::cout << result << std::endl; return result; } }; |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; }; class Cost : public Expression { private: int value; public: Cost(int val) : value(val) {} int evaluate() const override { return value; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Square : public Expression { private: const Expression& prev; public: Square() : prev(Cost(0)) {} Square(const Expression& p) : prev(p) {} int evaluate() const override { int val = prev.evaluate(); return val * val; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Inc : public Expression { private: const Expression& prev; public: Inc() : prev(Cost(0)) {} Inc(const Expression& p) : prev(p) {} int evaluate() const override { return prev.evaluate() + 1; } template<typename T> T operator>>(const T& next) const { return T(*this, next); } }; class Print : public Expression { private: const Expression& prev; public: Print() : prev(Cost(0)) {} Print(const Expression& p) : prev(p) {} int evaluate() const override { int result = prev.evaluate(); std::cout << result << std::endl; return result; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:81:1: error: 'Const' was not declared in this scope; did you mean 'Cost'?
81 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| Cost
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a C++ implementation of the classes `Const`, `Square`, `Inc`, and `Print` that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; virtual ~Expression() = default; }; class Const : public Expression { private: int value; public: Const(int val) : value(val) {} int evaluate() const override { return value; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Square : public Expression { private: const Expression* prev; public: Square() : prev(nullptr) {} Square(const Expression* p) : prev(p) {} int evaluate() const override { int val = prev->evaluate(); return val * val; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Inc : public Expression { private: const Expression* prev; public: Inc() : prev(nullptr) {} Inc(const Expression* p) : prev(p) {} int evaluate() const override { return prev->evaluate() + 1; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Print : public Expression { private: const Expression* prev; public: Print() : prev(nullptr) {} Print(const Expression* p) : prev(p) {} int evaluate() const override { int result = prev->evaluate(); std::cout << result << std::endl; return result; } }; int main() { Const(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | Const(5) >> Square() >> Inc() >> Inc() >> Print(); |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; virtual ~Expression() = default; }; class Const : public Expression { private: int value; public: Const(int val) : value(val) {} int evaluate() const override { return value; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Square : public Expression { private: const Expression* prev; public: Square() : prev(nullptr) {} Square(const Expression* p) : prev(p) {} int evaluate() const override { int val = prev->evaluate(); return val * val; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Inc : public Expression { private: const Expression* prev; public: Inc() : prev(nullptr) {} Inc(const Expression* p) : prev(p) {} int evaluate() const override { return prev->evaluate() + 1; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Print : public Expression { private: const Expression* prev; public: Print() : prev(nullptr) {} Print(const Expression* p) : prev(p) {} int evaluate() const override { int result = prev->evaluate(); std::cout << result << std::endl; return result; } }; |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; virtual ~Expression() = default; }; class Const : public Expression { private: int value; public: Const(int val) : value(val) {} int evaluate() const override { return value; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Square : public Expression { private: const Expression* prev; public: Square() : prev(nullptr) {} Square(const Expression* p) : prev(p) {} int evaluate() const override { int val = prev->evaluate(); return val * val; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Inc : public Expression { private: const Expression* prev; public: Inc() : prev(nullptr) {} Inc(const Expression* p) : prev(p) {} int evaluate() const override { return prev->evaluate() + 1; } template<typename T> T operator>>(const T& next) const { return T(this->evaluate()); } }; class Print : public Expression { private: const Expression* prev; public: Print() : prev(nullptr) {} Print(const Expression* p) : prev(p) {} int evaluate() const override { int result = prev->evaluate(); std::cout << result << std::endl; return result; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In instantiation of 'T Const::operator>>(const T&) const [with T = Square]':
main.cpp:82:20: required from here
main.cpp:23:32: error: invalid conversion from 'int' to 'const Expression*' [-fpermissive]
23 | return T(this->evaluate());
| ~~~~~~~~~~~~~~^~
| |
| int
main.cpp:33:30: note: initializing argument 1 of 'Square::Square(const Expression*)'
33 | Square(const Expression* p) : prev(p) {}
| ~~~~~~~~~~~~~~~~~~^
main.cpp: In instantiation of 'T Square::operator>>(const T&) const [with T = Inc]':
main.cpp:82:29: required from here
main.cpp:42:32: error: invalid conversion from 'int' to 'const Expression*' [-fpermissive]
42 | return T(this->evaluate());
| ~~~~~~~~~~~~~~^~
| |
| int
main.cpp:52:27: note: initializing argument 1 of 'Inc::Inc(const Expression*)'
52 | Inc(const Expression* p) : prev(p) {}
| ~~~~~~~~~~~~~~~~~~^
main.cpp: In instantiation of 'T Inc::operator>>(const T&) const [with T = Square]':
main.cpp:82:41: required from here
main.cpp:60:32: error: invalid conversion from 'int' to 'const Expression*' [-fpermissive]
60 | return T(this->evaluate());
| ~~~~~~~~~~~~~~^~
| |
| int
main.cpp:33:30: note: initializing argument 1 of 'Square::Square(const Expression*)'
33 | Square(const Expression* p) : prev(p) {}
| ~~~~~~~~~~~~~~~~~~^
main.cpp: In instantiation of 'T Square::operator>>(const T&) const [with T = Square]':
main.cpp:82:53: required from here
main.cpp:42:32: error: invalid conversion from 'int' to 'const Expression*' [-fpermissive]
42 | return T(this->evaluate());
| ~~~~~~~~~~~~~~^~
| |
| int
main.cpp:33:30: note: initializing argument 1 of 'Square::Square(const Expression*)'
33 | Square(const Expression* p) : prev(p) {}
| ~~~~~~~~~~~~~~~~~~^
main.cpp: In instantiation of 'T Square::operator>>(const T&) const [with T = Print]':
main.cpp:82:64: required from here
main.cpp:42:32: error: invalid conversion from 'int' to 'const Expression*' [-fpermissive]
42 | return T(this->evaluate());
| ~~~~~~~~~~~~~~^~
| |
| int
main.cpp:70:29: note: initializing argument 1 of 'Print::Print(const Expression*)'
70 | Print(const Expression* p) : prev(p) {}
| ~~~~~~~~~~~~~~~~~~^
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a C++ implementation of the classes Cost, Square, Inc, and Print that allows for the chaining of operations as you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include <iostream> class BaseOperation { protected: int value; public: BaseOperation(int val) : value(val) {} virtual int getValue() const { return value; } }; class Cost : public BaseOperation { public: Cost(int val) : BaseOperation(val) {} }; class Square : public BaseOperation { public: Square() : BaseOperation(0) {} Square& operator>>(const BaseOperation& prev) { value = prev.getValue() * prev.getValue(); return *this; } }; class Inc : public BaseOperation { public: Inc() : BaseOperation(0) {} Inc& operator>>(const BaseOperation& prev) { value = prev.getValue() + 1; return *this; } }; class Print : public BaseOperation { public: Print() : BaseOperation(0) {} void operator>>(const BaseOperation& prev) { std::cout << prev.getValue() << std::endl; } }; // Overloading the >> operator for Cost to start the chain template<typename T> T& operator>>(const Cost& cost, T& next) { return next >> cost; } |
1 2 3 4 | int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <iostream> class BaseOperation { protected: int value; public: BaseOperation(int val) : value(val) {} virtual int getValue() const { return value; } }; class Cost : public BaseOperation { public: Cost(int val) : BaseOperation(val) {} }; class Square : public BaseOperation { public: Square() : BaseOperation(0) {} Square& operator>>(const BaseOperation& prev) { value = prev.getValue() * prev.getValue(); return *this; } }; class Inc : public BaseOperation { public: Inc() : BaseOperation(0) {} Inc& operator>>(const BaseOperation& prev) { value = prev.getValue() + 1; return *this; } }; class Print : public BaseOperation { public: Print() : BaseOperation(0) {} void operator>>(const BaseOperation& prev) { std::cout << prev.getValue() << std::endl; } }; template<typename T> T& operator>>(const Cost& cost, T& next) { return next >> cost; } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | #include <iostream> class BaseOperation { protected: int value; public: BaseOperation(int val) : value(val) {} virtual int getValue() const { return value; } }; class Cost : public BaseOperation { public: Cost(int val) : BaseOperation(val) {} }; class Square : public BaseOperation { public: Square() : BaseOperation(0) {} Square& operator>>(const BaseOperation& prev) { value = prev.getValue() * prev.getValue(); return *this; } }; class Inc : public BaseOperation { public: Inc() : BaseOperation(0) {} Inc& operator>>(const BaseOperation& prev) { value = prev.getValue() + 1; return *this; } }; class Print : public BaseOperation { public: Print() : BaseOperation(0) {} void operator>>(const BaseOperation& prev) { std::cout << prev.getValue() << std::endl; } }; template<typename T> T& operator>>(const Cost& cost, T& next) { return next >> cost; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:55:1: error: 'Const' was not declared in this scope; did you mean 'Cost'?
55 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| Cost
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a C++ implementation of the classes Cost, Square, Inc, and Print that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include <iostream> class Value { public: virtual int get() const = 0; virtual ~Value() = default; }; class Cost : public Value { private: int value; public: Cost(int v) : value(v) {} int get() const override { return value; } }; class Square : public Value { private: const Value* prev; public: Square() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; } int get() const override { return prev ? prev->get() * prev->get() : 0; } }; class Inc : public Value { private: const Value* prev; public: Inc() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; } int get() const override { return prev ? prev->get() + 1 : 0; } }; class Print : public Value { private: const Value* prev; public: Print() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; std::cout << prev->get() << std::endl; } int get() const override { return prev ? prev->get() : 0; } }; template<typename T, typename U> T&& operator>>(T&& t, U&& u) { std::forward<U>(u) >> std::forward<T>(t); return std::forward<T>(t); } int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | Cost(5) >> Square() >> Inc() >> Inc() >> Print(); |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | #include <iostream> class Value { public: virtual int get() const = 0; virtual ~Value() = default; }; class Cost : public Value { private: int value; public: Cost(int v) : value(v) {} int get() const override { return value; } }; class Square : public Value { private: const Value* prev; public: Square() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; } int get() const override { return prev ? prev->get() * prev->get() : 0; } }; class Inc : public Value { private: const Value* prev; public: Inc() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; } int get() const override { return prev ? prev->get() + 1 : 0; } }; class Print : public Value { private: const Value* prev; public: Print() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; std::cout << prev->get() << std::endl; } int get() const override { return prev ? prev->get() : 0; } }; template<typename T, typename U> T&& operator>>(T&& t, U&& u) { std::forward<U>(u) >> std::forward<T>(t); return std::forward<T>(t); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include <iostream> class Value { public: virtual int get() const = 0; virtual ~Value() = default; }; class Cost : public Value { private: int value; public: Cost(int v) : value(v) {} int get() const override { return value; } }; class Square : public Value { private: const Value* prev; public: Square() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; } int get() const override { return prev ? prev->get() * prev->get() : 0; } }; class Inc : public Value { private: const Value* prev; public: Inc() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; } int get() const override { return prev ? prev->get() + 1 : 0; } }; class Print : public Value { private: const Value* prev; public: Print() : prev(nullptr) {} void operator>>(const Value& v) { prev = &v; std::cout << prev->get() << std::endl; } int get() const override { return prev ? prev->get() : 0; } }; template<typename T, typename U> T&& operator>>(T&& t, U&& u) { std::forward<U>(u) >> std::forward<T>(t); return std::forward<T>(t); } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:61:1: error: 'Const' was not declared in this scope; did you mean 'Cost'?
61 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| Cost
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Inc() >> Print() and the resulting python program will print 27 to the screen.
Here's a C++ implementation that allows for the chaining syntax you described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #include <iostream> class Value { private: int value; public: Value(int v) : value(v) {} int get() const { return value; } void set(int v) { value = v; } }; class Const { private: Value value; public: Const(int v) : value(v) {} Value operator>>(const class Square&) const; Value operator>>(const class Inc&) const; Value operator>>(const class Print&) const; }; class Square { public: Value operator()(Value v) const { return Value(v.get() * v.get()); } }; class Inc { public: Value operator()(Value v) const { return Value(v.get() + 1); } }; class Print { public: Value operator()(Value v) const { std::cout << v.get() << std::endl; return v; } }; Value Const::operator>>(const Square& square) const { return square(value); } Value Const::operator>>(const Inc& inc) const { return inc(value); } Value Const::operator>>(const Print& print) const { return print(value); } Value operator>>(Value v, const Square& square) { return square(v); } Value operator>>(Value v, const Inc& inc) { return inc(v); } Value operator>>(Value v, const Print& print) { return print(v); } int main() { Const(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | 27
|
I extracted the following code from that output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | #include <iostream> class Value { private: int value; public: Value(int v) : value(v) {} int get() const { return value; } void set(int v) { value = v; } }; class Const { private: Value value; public: Const(int v) : value(v) {} Value operator>>(const class Square&) const; Value operator>>(const class Inc&) const; Value operator>>(const class Print&) const; }; class Square { public: Value operator()(Value v) const { return Value(v.get() * v.get()); } }; class Inc { public: Value operator()(Value v) const { return Value(v.get() + 1); } }; class Print { public: Value operator()(Value v) const { std::cout << v.get() << std::endl; return v; } }; Value Const::operator>>(const Square& square) const { return square(value); } Value Const::operator>>(const Inc& inc) const { return inc(value); } Value Const::operator>>(const Print& print) const { return print(value); } Value operator>>(Value v, const Square& square) { return square(v); } Value operator>>(Value v, const Inc& inc) { return inc(v); } Value operator>>(Value v, const Print& print) { return print(v); } |
Running the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include <iostream> class Value { private: int value; public: Value(int v) : value(v) {} int get() const { return value; } void set(int v) { value = v; } }; class Const { private: Value value; public: Const(int v) : value(v) {} Value operator>>(const class Square&) const; Value operator>>(const class Inc&) const; Value operator>>(const class Print&) const; }; class Square { public: Value operator()(Value v) const { return Value(v.get() * v.get()); } }; class Inc { public: Value operator()(Value v) const { return Value(v.get() + 1); } }; class Print { public: Value operator()(Value v) const { std::cout << v.get() << std::endl; return v; } }; Value Const::operator>>(const Square& square) const { return square(value); } Value Const::operator>>(const Inc& inc) const { return inc(value); } Value Const::operator>>(const Print& print) const { return print(value); } Value operator>>(Value v, const Square& square) { return square(v); } Value operator>>(Value v, const Inc& inc) { return inc(v); } Value operator>>(Value v, const Print& print) { return print(v); } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
625
Testing if the previous output contains the string 625: True