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 you to chain operations in the way 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include <iostream> class Operation { public: virtual int apply(int value) = 0; virtual ~Operation() = default; }; class Cost : public Operation { private: int value; public: Cost(int val) : value(val) {} int apply(int) override { return value; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Square : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } return input * input; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Inc : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } return input + 1; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Print : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } std::cout << input << std::endl; return input; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation*) { apply(0); return nullptr; } }; int main() { Cost(5) >> new Square() >> new Inc() >> new Inc() >> new Print(); return 0; } |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include <iostream> class Operation { public: virtual int apply(int value) = 0; virtual ~Operation() = default; }; class Cost : public Operation { private: int value; public: Cost(int val) : value(val) {} int apply(int) override { return value; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Square : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } return input * input; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Inc : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } return input + 1; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Print : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } std::cout << input << std::endl; return input; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation*) { apply(0); return nullptr; } }; int main() { Cost(5) >> new Square() >> new Inc() >> new Inc() >> new Print(); return 0; } |
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | #include <iostream> class Operation { public: virtual int apply(int value) = 0; virtual ~Operation() = default; }; class Cost : public Operation { private: int value; public: Cost(int val) : value(val) {} int apply(int) override { return value; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Square : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } return input * input; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Inc : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } return input + 1; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation* next) { int result = apply(0); return next->setInput(result); } }; class Print : public Operation { private: int input; bool inputSet = false; public: int apply(int val) override { if (!inputSet) { input = val; inputSet = true; } std::cout << input << std::endl; return input; } Operation* setInput(int val) { input = val; inputSet = true; return this; } Operation* operator>>(Operation*) { apply(0); return nullptr; } }; int __delete_this__main() { Cost(5) >> new Square() >> new Inc() >> new Inc() >> new Print(); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In member function 'Operation* Cost::operator>>(Operation*)':
main.cpp:23:22: error: 'class Operation' has no member named 'setInput'
23 | return next->setInput(result);
| ^~~~~~~~
main.cpp: In member function 'Operation* Square::operator>>(Operation*)':
main.cpp:49:22: error: 'class Operation' has no member named 'setInput'
49 | return next->setInput(result);
| ^~~~~~~~
main.cpp: In member function 'Operation* Inc::operator>>(Operation*)':
main.cpp:75:22: error: 'class Operation' has no member named 'setInput'
75 | return next->setInput(result);
| ^~~~~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:107:29: error: invalid operands of types 'Operation*' and 'Inc*' to binary 'operator>>'
107 | Cost(5) >> new Square() >> new Inc() >> new Inc() >> new Print();
| ~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~~~~~
| | |
| Operation* Inc*
main.cpp: In function 'int main()':
main.cpp:114:1: error: 'Const' was not declared in this scope; did you mean 'const'?
114 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| const
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 method chaining and achieves the desired behavior:
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 84 85 86 87 88 89 90 91 92 93 94 95 | #include <iostream> class Base { public: virtual Base& operator>>(Base& next) { return next; } virtual int getValue() = 0; virtual ~Base() {} }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} Base& operator>>(Base& next) override { next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Square : public Base { private: int value; public: Base& operator>>(Base& next) override { value *= value; next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Inc : public Base { private: int value; public: Base& operator>>(Base& next) override { value++; next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Print : public Base { private: int value; public: Base& operator>>(Base& next) override { std::cout << value << std::endl; return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <iostream> class Base { public: virtual Base& operator>>(Base& next) { return next; } virtual int getValue() = 0; virtual ~Base() {} }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} Base& operator>>(Base& next) override { next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Square : public Base { private: int value; public: Base& operator>>(Base& next) override { value *= value; next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Inc : public Base { private: int value; public: Base& operator>>(Base& next) override { value++; next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Print : public Base { private: int value; public: Base& operator>>(Base& next) override { std::cout << value << std::endl; return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
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 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | #include <iostream> class Base { public: virtual Base& operator>>(Base& next) { return next; } virtual int getValue() = 0; virtual ~Base() {} }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} Base& operator>>(Base& next) override { next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Square : public Base { private: int value; public: Base& operator>>(Base& next) override { value *= value; next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Inc : public Base { private: int value; public: Base& operator>>(Base& next) override { value++; next.setValue(value); return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; class Print : public Base { private: int value; public: Base& operator>>(Base& next) override { std::cout << value << std::endl; return next; } void setValue(int val) override { value = val; } int getValue() override { return value; } }; int __delete_this__main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:25:10: error: 'void Cost::setValue(int)' marked 'override', but does not override
25 | void setValue(int val) override {
| ^~~~~~~~
main.cpp: In member function 'virtual Base& Cost::operator>>(Base&)':
main.cpp:21:14: error: 'class Base' has no member named 'setValue'; did you mean 'getValue'?
21 | next.setValue(value);
| ^~~~~~~~
| getValue
main.cpp: At global scope:
main.cpp:45:10: error: 'void Square::setValue(int)' marked 'override', but does not override
45 | void setValue(int val) override {
| ^~~~~~~~
main.cpp: In member function 'virtual Base& Square::operator>>(Base&)':
main.cpp:41:14: error: 'class Base' has no member named 'setValue'; did you mean 'getValue'?
41 | next.setValue(value);
| ^~~~~~~~
| getValue
main.cpp: At global scope:
main.cpp:65:10: error: 'void Inc::setValue(int)' marked 'override', but does not override
65 | void setValue(int val) override {
| ^~~~~~~~
main.cpp: In member function 'virtual Base& Inc::operator>>(Base&)':
main.cpp:61:14: error: 'class Base' has no member named 'setValue'; did you mean 'getValue'?
61 | next.setValue(value);
| ^~~~~~~~
| getValue
main.cpp: At global scope:
main.cpp:84:10: error: 'void Print::setValue(int)' marked 'override', but does not override
84 | void setValue(int val) override {
| ^~~~~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:94:16: error: cannot bind non-const lvalue reference of type 'Base&' to an rvalue of type 'Base'
94 | Cost(5) >> Square() >> Inc() >> Inc() >> Print();
| ^~~~~~~~
main.cpp:20:28: note: initializing argument 1 of 'virtual Base& Cost::operator>>(Base&)'
20 | Base& operator>>(Base& next) override {
| ~~~~~~^~~~
main.cpp: In function 'int main()':
main.cpp:101:1: error: 'Const' was not declared in this scope; did you mean 'const'?
101 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| const
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 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 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 Base { public: virtual int getValue() = 0; virtual Base* operator>>(Base* next) = 0; virtual ~Base() {} }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value); return next; } virtual void setValue(int val) { value = val; } }; class Square : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value * value); return next; } void setValue(int val) { value = val; } }; class Inc : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value + 1); return next; } void setValue(int val) { value = val; } }; class Print : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { std::cout << value << std::endl; return next; } void setValue(int val) { value = val; } }; int main() { Base* result = Cost(5) >> new Square() >> new Inc() >> new Inc() >> new Print(); delete result; return 0; } |
1 2 3 | g++ -std=c++11 program.cpp -o program ./program 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 Base { public: virtual int getValue() = 0; virtual Base* operator>>(Base* next) = 0; virtual ~Base() {} }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value); return next; } virtual void setValue(int val) { value = val; } }; class Square : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value * value); return next; } void setValue(int val) { value = val; } }; class Inc : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value + 1); return next; } void setValue(int val) { value = val; } }; class Print : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { std::cout << value << std::endl; return next; } void setValue(int val) { value = val; } }; |
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 Base { public: virtual int getValue() = 0; virtual Base* operator>>(Base* next) = 0; virtual ~Base() {} }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value); return next; } virtual void setValue(int val) { value = val; } }; class Square : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value * value); return next; } void setValue(int val) { value = val; } }; class Inc : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { next->setValue(value + 1); return next; } void setValue(int val) { value = val; } }; class Print : public Base { private: int value; public: int getValue() override { return value; } Base* operator>>(Base* next) override { std::cout << value << std::endl; return next; } void setValue(int val) { value = val; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In member function 'virtual Base* Cost::operator>>(Base*)':
main.cpp:21:15: error: 'class Base' has no member named 'setValue'; did you mean 'getValue'?
21 | next->setValue(value);
| ^~~~~~~~
| getValue
main.cpp: In member function 'virtual Base* Square::operator>>(Base*)':
main.cpp:36:15: error: 'class Base' has no member named 'setValue'; did you mean 'getValue'?
36 | next->setValue(value * value);
| ^~~~~~~~
| getValue
main.cpp: In member function 'virtual Base* Inc::operator>>(Base*)':
main.cpp:51:15: error: 'class Base' has no member named 'setValue'; did you mean 'getValue'?
51 | next->setValue(value + 1);
| ^~~~~~~~
| getValue
main.cpp: In function 'int main()':
main.cpp:76:1: error: 'Const' was not declared in this scope; did you mean 'const'?
76 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| const
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 method chaining and achieves the desired behavior:
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 Base { public: virtual Base& operator>>=(Base& next) { return next; } }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} Base& operator>>=(Base& next) override { next.value = value; return next; } int getValue() const { return value; } }; class Square : public Base { public: int value; Base& operator>>=(Base& next) override { value *= value; next.value = value; return next; } }; class Inc : public Base { public: int value; Base& operator>>=(Base& next) override { value++; next.value = value; return next; } }; class Print : public Base { public: int value; Base& operator>>=(Base& next) override { std::cout << value << std::endl; return next; } }; int main() { Cost(5) >>= Square() >>= Inc() >>= Inc() >>= Print(); return 0; } |
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 | #include <iostream> class Base { public: virtual Base& operator>>=(Base& next) { return next; } }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} Base& operator>>=(Base& next) override { next.value = value; return next; } int getValue() const { return value; } }; class Square : public Base { public: int value; Base& operator>>=(Base& next) override { value *= value; next.value = value; return next; } }; class Inc : public Base { public: int value; Base& operator>>=(Base& next) override { value++; next.value = value; return next; } }; class Print : public Base { public: int value; Base& operator>>=(Base& next) override { std::cout << value << std::endl; return next; } }; int main() { Cost(5) >>= Square() >>= Inc() >>= Inc() >>= Print(); return 0; } |
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 | #include <iostream> class Base { public: virtual Base& operator>>=(Base& next) { return next; } }; class Cost : public Base { private: int value; public: Cost(int val) : value(val) {} Base& operator>>=(Base& next) override { next.value = value; return next; } int getValue() const { return value; } }; class Square : public Base { public: int value; Base& operator>>=(Base& next) override { value *= value; next.value = value; return next; } }; class Inc : public Base { public: int value; Base& operator>>=(Base& next) override { value++; next.value = value; return next; } }; class Print : public Base { public: int value; Base& operator>>=(Base& next) override { std::cout << value << std::endl; return next; } }; int __delete_this__main() { Cost(5) >>= Square() >>= Inc() >>= Inc() >>= Print(); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In member function 'virtual Base& Cost::operator>>=(Base&)':
main.cpp:19:14: error: 'class Base' has no member named 'value'
19 | next.value = value;
| ^~~~~
main.cpp: In member function 'virtual Base& Square::operator>>=(Base&)':
main.cpp:32:14: error: 'class Base' has no member named 'value'
32 | next.value = value;
| ^~~~~
main.cpp: In member function 'virtual Base& Inc::operator>>=(Base&)':
main.cpp:43:14: error: 'class Base' has no member named 'value'
43 | next.value = value;
| ^~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:59:50: error: cannot bind non-const lvalue reference of type 'Base&' to an rvalue of type 'Base'
59 | Cost(5) >>= Square() >>= Inc() >>= Inc() >>= Print();
| ^~~~~~~
main.cpp:41:29: note: initializing argument 1 of 'virtual Base& Inc::operator>>=(Base&)'
41 | Base& operator>>=(Base& next) override {
| ~~~~~~^~~~
main.cpp: In function 'int main()':
main.cpp:66:1: error: 'Const' was not declared in this scope; did you mean 'const'?
66 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| const
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 method chaining and achieves the behavior 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} template<typename NextOperation> auto operator>>(NextOperation op) { return op(*this); } int& getValue() { return value; } }; class Square { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { int val = prev.getValue(); prev.getValue() = val * val; return prev; } }; class Inc { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { prev.getValue()++; return prev; } }; class Print { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { std::cout << prev.getValue() << std::endl; return prev; } }; 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} template<typename NextOperation> auto operator>>(NextOperation op) { return op(*this); } int& getValue() { return value; } }; class Square { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { int val = prev.getValue(); prev.getValue() = val * val; return prev; } }; class Inc { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { prev.getValue()++; return prev; } }; class Print { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { std::cout << prev.getValue() << std::endl; return prev; } }; |
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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} template<typename NextOperation> auto operator>>(NextOperation op) { return op(*this); } int& getValue() { return value; } }; class Square { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { int val = prev.getValue(); prev.getValue() = val * val; return prev; } }; class Inc { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { prev.getValue()++; return prev; } }; class Print { public: template<typename PrevOperation> Const operator()(PrevOperation& prev) { std::cout << prev.getValue() << std::endl; return prev; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
625
Testing if the previous output contains the string 625
: True