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.
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 | #include <iostream> // Base class for the pipeline class Pipeline { public: virtual Pipeline& operator>>(Pipeline& next) = 0; virtual ~Pipeline() {} }; // Const class for setting a constant value class Const : public Pipeline { private: int value; public: Const(int value) : value(value) {} Pipeline& operator>>(Pipeline& next) override { next.setValue(value); return next; } }; // Square class for squaring the value class Square : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { next.setValue(value * value); return next; } }; // Inc class for incrementing the value class Inc : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { next.setValue(value + 1); return next; } }; // Print class for printing the value class Print : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { std::cout << value << std::endl; return *this; } }; int main() { Const(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 | #include <iostream> // Base class for the pipeline class Pipeline { public: virtual Pipeline& operator>>(Pipeline& next) = 0; virtual ~Pipeline() {} }; // Const class for setting a constant value class Const : public Pipeline { private: int value; public: Const(int value) : value(value) {} Pipeline& operator>>(Pipeline& next) override { next.setValue(value); return next; } }; // Square class for squaring the value class Square : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { next.setValue(value * value); return next; } }; // Inc class for incrementing the value class Inc : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { next.setValue(value + 1); return next; } }; // Print class for printing the value class Print : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { std::cout << value << std::endl; return *this; } }; int main() { Const(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 | #include <iostream> // Base class for the pipeline class Pipeline { public: virtual Pipeline& operator>>(Pipeline& next) = 0; virtual ~Pipeline() {} }; // Const class for setting a constant value class Const : public Pipeline { private: int value; public: Const(int value) : value(value) {} Pipeline& operator>>(Pipeline& next) override { next.setValue(value); return next; } }; // Square class for squaring the value class Square : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { next.setValue(value * value); return next; } }; // Inc class for incrementing the value class Inc : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { next.setValue(value + 1); return next; } }; // Print class for printing the value class Print : public Pipeline { private: int value; public: void setValue(int value) { this->value = value; } Pipeline& operator>>(Pipeline& next) override { std::cout << value << std::endl; return *this; } }; int __delete_this__main() { Const(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In member function 'virtual Pipeline& Const::operator>>(Pipeline&)':
main.cpp:19:14: error: 'class Pipeline' has no member named 'setValue'
19 | next.setValue(value);
| ^~~~~~~~
main.cpp: In member function 'virtual Pipeline& Square::operator>>(Pipeline&)':
main.cpp:32:14: error: 'class Pipeline' has no member named 'setValue'
32 | next.setValue(value * value);
| ^~~~~~~~
main.cpp: In member function 'virtual Pipeline& Inc::operator>>(Pipeline&)':
main.cpp:45:14: error: 'class Pipeline' has no member named 'setValue'
45 | next.setValue(value + 1);
| ^~~~~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:64:17: error: cannot bind non-const lvalue reference of type 'Pipeline&' to an rvalue of type 'Pipeline'
64 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ^~~~~~~~
main.cpp:18:36: note: initializing argument 1 of 'virtual Pipeline& Const::operator>>(Pipeline&)'
18 | Pipeline& operator>>(Pipeline& next) override {
| ~~~~~~~~~~^~~~
main.cpp: In function 'int main()':
main.cpp:71:13: error: cannot bind non-const lvalue reference of type 'Pipeline&' to an rvalue of type 'Pipeline'
71 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~~~~
main.cpp:18:36: note: initializing argument 1 of 'virtual Pipeline& Const::operator>>(Pipeline&)'
18 | Pipeline& operator>>(Pipeline& next) override {
| ~~~~~~~~~~^~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False