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 as requested:
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 Expression { public: virtual int evaluate() const = 0; virtual ~Expression() = default; }; class Const : public Expression { private: int value; public: Const(int v) : value(v) {} int evaluate() const override { return value; } template <typename T> auto operator>>(const T& next) const { return next(value); } }; class Square { public: auto operator()(int value) const { return Const(value * value); } }; class Inc { public: auto operator()(int value) const { return Const(value + 1); } }; class Print { public: auto operator()(int value) const { std::cout << value << std::endl; return Const(value); } }; 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 Expression { public: virtual int evaluate() const = 0; virtual ~Expression() = default; }; class Const : public Expression { private: int value; public: Const(int v) : value(v) {} int evaluate() const override { return value; } template <typename T> auto operator>>(const T& next) const { return next(value); } }; class Square { public: auto operator()(int value) const { return Const(value * value); } }; class Inc { public: auto operator()(int value) const { return Const(value + 1); } }; class Print { public: auto operator()(int value) const { std::cout << value << std::endl; return Const(value); } }; |
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 | #include <iostream> class Expression { public: virtual int evaluate() const = 0; virtual ~Expression() = default; }; class Const : public Expression { private: int value; public: Const(int v) : value(v) {} int evaluate() const override { return value; } template <typename T> auto operator>>(const T& next) const { return next(value); } }; class Square { public: auto operator()(int value) const { return Const(value * value); } }; class Inc { public: auto operator()(int value) const { return Const(value + 1); } }; class Print { public: auto operator()(int value) const { std::cout << value << std::endl; return Const(value); } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
625
Testing if the previous output contains the string 625
: True
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 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 77 78 79 80 81 82 83 | #include <iostream> class Operation { protected: int value; public: Operation(int val) : value(val) {} virtual int execute() = 0; }; class Const : public Operation { public: Const(int val) : Operation(val) {} int execute() override { return value; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } }; class Square : public Operation { public: Square() : Operation(0) {} int execute() override { return value * value; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } Square operator()(int val) { this->value = val; return *this; } }; class Inc : public Operation { public: Inc() : Operation(0) {} int execute() override { return value + 1; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } Inc operator()(int val) { this->value = val; return *this; } }; class Print : public Operation { public: Print() : Operation(0) {} int execute() override { std::cout << value << std::endl; return value; } Print operator()(int val) { this->value = val; this->execute(); return *this; } }; 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 77 78 | #include <iostream> class Operation { protected: int value; public: Operation(int val) : value(val) {} virtual int execute() = 0; }; class Const : public Operation { public: Const(int val) : Operation(val) {} int execute() override { return value; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } }; class Square : public Operation { public: Square() : Operation(0) {} int execute() override { return value * value; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } Square operator()(int val) { this->value = val; return *this; } }; class Inc : public Operation { public: Inc() : Operation(0) {} int execute() override { return value + 1; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } Inc operator()(int val) { this->value = val; return *this; } }; class Print : public Operation { public: Print() : Operation(0) {} int execute() override { std::cout << value << std::endl; return value; } Print operator()(int val) { this->value = val; this->execute(); return *this; } }; |
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 | #include <iostream> class Operation { protected: int value; public: Operation(int val) : value(val) {} virtual int execute() = 0; }; class Const : public Operation { public: Const(int val) : Operation(val) {} int execute() override { return value; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } }; class Square : public Operation { public: Square() : Operation(0) {} int execute() override { return value * value; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } Square operator()(int val) { this->value = val; return *this; } }; class Inc : public Operation { public: Inc() : Operation(0) {} int execute() override { return value + 1; } template <typename NextOp> auto operator>>(NextOp next) { return next(this->execute()); } Inc operator()(int val) { this->value = val; return *this; } }; class Print : public Operation { public: Print() : Operation(0) {} int execute() override { std::cout << value << std::endl; return value; } Print operator()(int val) { this->value = val; this->execute(); return *this; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
625
Testing if the previous output contains the string 625
: True
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 | #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: int 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)); } Value Const::operator>>(const Inc& inc) const { return inc(Value(value)); } Value Const::operator>>(const Print& print) const { return print(Value(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: int 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)); } Value Const::operator>>(const Inc& inc) const { return inc(Value(value)); } Value Const::operator>>(const Print& print) const { return print(Value(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: int 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)); } Value Const::operator>>(const Inc& inc) const { return inc(Value(value)); } Value Const::operator>>(const Print& print) const { return print(Value(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
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 of operations as requested:
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 | #include <iostream> class Printable { public: virtual int getValue() const = 0; }; class Const : public Printable { private: int value; public: Const(int val) : value(val) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; class Square { public: auto operate(const Printable& prev) const { class SquareOp : public Printable { private: int value; public: SquareOp(int val) : value(val * val) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; return SquareOp(prev.getValue()); } }; class Inc { public: auto operate(const Printable& prev) const { class IncOp : public Printable { private: int value; public: IncOp(int val) : value(val + 1) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; return IncOp(prev.getValue()); } }; class Print { public: void operate(const Printable& prev) const { std::cout << prev.getValue() << std::endl; } }; 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 | #include <iostream> class Printable { public: virtual int getValue() const = 0; }; class Const : public Printable { private: int value; public: Const(int val) : value(val) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; class Square { public: auto operate(const Printable& prev) const { class SquareOp : public Printable { private: int value; public: SquareOp(int val) : value(val * val) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; return SquareOp(prev.getValue()); } }; class Inc { public: auto operate(const Printable& prev) const { class IncOp : public Printable { private: int value; public: IncOp(int val) : value(val + 1) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; return IncOp(prev.getValue()); } }; class Print { public: void operate(const Printable& prev) const { std::cout << prev.getValue() << std::endl; } }; |
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 | #include <iostream> class Printable { public: virtual int getValue() const = 0; }; class Const : public Printable { private: int value; public: Const(int val) : value(val) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; class Square { public: auto operate(const Printable& prev) const { class SquareOp : public Printable { private: int value; public: SquareOp(int val) : value(val * val) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; return SquareOp(prev.getValue()); } }; class Inc { public: auto operate(const Printable& prev) const { class IncOp : public Printable { private: int value; public: IncOp(int val) : value(val + 1) {} int getValue() const override { return value; } template<typename T> auto operator>>(const T& next) const { return next.operate(*this); } }; return IncOp(prev.getValue()); } }; class Print { public: void operate(const Printable& prev) const { std::cout << prev.getValue() << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In member function 'auto Square::operate(const Printable&) const':
main.cpp:35:13: error: invalid declaration of member template in local class
35 | template<typename T>
| ^~~~~~~~
main.cpp: In member function 'auto Inc::operate(const Printable&) const':
main.cpp:53:13: error: invalid declaration of member template in local class
53 | template<typename T>
| ^~~~~~~~
main.cpp: In function 'int main()':
main.cpp:72:22: error: no match for 'operator>>' (operand types are 'Square::operate(const Printable&) const::SquareOp' and 'Inc')
72 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~
| | |
| | Inc
| Square::operate(const Printable&) const::SquareOp
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 | #include <iostream> class Operation { protected: int value; public: Operation(int v) : value(v) {} virtual int getValue() const { return value; } }; class Cost : public Operation { public: Cost(int v) : Operation(v) {} }; class Square : public Operation { public: Square() : Operation(0) {} Square operator>>(const Operation& op) { value = op.getValue() * op.getValue(); return *this; } }; class Inc : public Operation { public: Inc() : Operation(0) {} Inc operator>>(const Operation& op) { value = op.getValue() + 1; return *this; } }; class Print : public Operation { public: Print() : Operation(0) {} void operator>>(const Operation& op) { std::cout << op.getValue() << std::endl; } }; // Operator overloads for chaining template<typename T> T operator>>(const Operation& lhs, T rhs) { return rhs >> lhs; } |
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 Operation { protected: int value; public: Operation(int v) : value(v) {} virtual int getValue() const { return value; } }; class Cost : public Operation { public: Cost(int v) : Operation(v) {} }; class Square : public Operation { public: Square() : Operation(0) {} Square operator>>(const Operation& op) { value = op.getValue() * op.getValue(); return *this; } }; class Inc : public Operation { public: Inc() : Operation(0) {} Inc operator>>(const Operation& op) { value = op.getValue() + 1; return *this; } }; class Print : public Operation { public: Print() : Operation(0) {} void operator>>(const Operation& op) { std::cout << op.getValue() << std::endl; } }; template<typename T> T operator>>(const Operation& lhs, T rhs) { return rhs >> lhs; } |
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 Operation { protected: int value; public: Operation(int v) : value(v) {} virtual int getValue() const { return value; } }; class Cost : public Operation { public: Cost(int v) : Operation(v) {} }; class Square : public Operation { public: Square() : Operation(0) {} Square operator>>(const Operation& op) { value = op.getValue() * op.getValue(); return *this; } }; class Inc : public Operation { public: Inc() : Operation(0) {} Inc operator>>(const Operation& op) { value = op.getValue() + 1; return *this; } }; class Print : public Operation { public: Print() : Operation(0) {} void operator>>(const Operation& op) { std::cout << op.getValue() << std::endl; } }; template<typename T> T operator>>(const Operation& lhs, T rhs) { return rhs >> lhs; } 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