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++ solution that implements a chain of operations using the >> operator:
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 Value { private: int value; public: Value(int v) : value(v) {} int getValue() const { return value; } void setValue(int v) { value = v; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} Value operator>>(const class Square& square) const { return Value(value); } }; class Square { public: friend Value operator>>(const Value& val, const Square&) { return Value(val.getValue() * val.getValue()); } }; class Inc { public: friend Value operator>>(const Value& val, const Inc&) { return Value(val.getValue() + 1); } }; class Print { public: friend void operator>>(const Value& val, const Print&) { std::cout << val.getValue() << std::endl; } }; int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | 27
|
1 2 | Cost(3) >> Square() >> Inc() >> Print(); // Outputs: 10 Cost(2) >> Inc() >> Square() >> Print(); // Outputs: 9 |
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 | #include <iostream> class Value { private: int value; public: Value(int v) : value(v) {} int getValue() const { return value; } void setValue(int v) { value = v; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} Value operator>>(const class Square& square) const { return Value(value); } }; class Square { public: friend Value operator>>(const Value& val, const Square&) { return Value(val.getValue() * val.getValue()); } }; class Inc { public: friend Value operator>>(const Value& val, const Inc&) { return Value(val.getValue() + 1); } }; class Print { public: friend void operator>>(const Value& val, const Print&) { std::cout << val.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 | #include <iostream> class Value { private: int value; public: Value(int v) : value(v) {} int getValue() const { return value; } void setValue(int v) { value = v; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} Value operator>>(const class Square& square) const { return Value(value); } }; class Square { public: friend Value operator>>(const Value& val, const Square&) { return Value(val.getValue() * val.getValue()); } }; class Inc { public: friend Value operator>>(const Value& val, const Inc&) { return Value(val.getValue() + 1); } }; class Print { public: friend void operator>>(const Value& val, const Print&) { std::cout << val.getValue() << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:50:1: error: 'Const' was not declared in this scope; did you mean 'const'?
50 | 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 creates a chain of operations using operator overloading:
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 | #include <iostream> // Forward declaration class Square; class Inc; class Print; // Base class to hold the value class Expression { protected: int value; public: Expression(int v) : value(v) {} virtual int getValue() const { return value; } }; // Initial constant value class Cost : public Expression { public: Cost(int v) : Expression(v) {} // Operator overloading for chaining operations template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Square operation class Square : public Expression { public: Square() : Expression(0) {} Square(const Expression& expr) : Expression(expr.getValue() * expr.getValue()) {} template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Increment operation class Inc : public Expression { public: Inc() : Expression(0) {} Inc(const Expression& expr) : Expression(expr.getValue() + 1) {} template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Print operation class Print : public Expression { public: Print() : Expression(0) {} Print(const Expression& expr) : Expression(expr.getValue()) { std::cout << value << std::endl; } }; int main() { Cost(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | Cost(3) >> Square() >> Inc() >> Print(); // Prints 10 |
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 | #include <iostream> // Forward declaration class Square; class Inc; class Print; // Base class to hold the value class Expression { protected: int value; public: Expression(int v) : value(v) {} virtual int getValue() const { return value; } }; // Initial constant value class Cost : public Expression { public: Cost(int v) : Expression(v) {} // Operator overloading for chaining operations template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Square operation class Square : public Expression { public: Square() : Expression(0) {} Square(const Expression& expr) : Expression(expr.getValue() * expr.getValue()) {} template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Increment operation class Inc : public Expression { public: Inc() : Expression(0) {} Inc(const Expression& expr) : Expression(expr.getValue() + 1) {} template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Print operation class Print : public Expression { public: Print() : Expression(0) {} Print(const Expression& expr) : Expression(expr.getValue()) { std::cout << value << 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 | #include <iostream> // Forward declaration class Square; class Inc; class Print; // Base class to hold the value class Expression { protected: int value; public: Expression(int v) : value(v) {} virtual int getValue() const { return value; } }; // Initial constant value class Cost : public Expression { public: Cost(int v) : Expression(v) {} // Operator overloading for chaining operations template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Square operation class Square : public Expression { public: Square() : Expression(0) {} Square(const Expression& expr) : Expression(expr.getValue() * expr.getValue()) {} template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Increment operation class Inc : public Expression { public: Inc() : Expression(0) {} Inc(const Expression& expr) : Expression(expr.getValue() + 1) {} template<typename T> T operator>>(const T& op) const { return T(*this); } }; // Print operation class Print : public Expression { public: Print() : Expression(0) {} Print(const Expression& expr) : Expression(expr.getValue()) { std::cout << value << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:69:1: error: 'Const' was not declared in this scope; did you mean 'const'?
69 | 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 such function chaining:
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 Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} template<typename T> auto operator>>(const T& operation) const { return operation.apply(value); } }; class Square { public: Number apply(int value) const { return Number(value * value); } }; class Inc { public: Number apply(int value) const { return Number(value + 1); } }; class Print { public: Number apply(int value) const { std::cout << value << std::endl; return Number(value); } }; // Operator overloading for Number to enable chaining template<typename T> auto operator>>(const Number& num, const T& operation) { return operation.apply(num.getValue()); } 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 | #include <iostream> class Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} template<typename T> auto operator>>(const T& operation) const { return operation.apply(value); } }; class Square { public: Number apply(int value) const { return Number(value * value); } }; class Inc { public: Number apply(int value) const { return Number(value + 1); } }; class Print { public: Number apply(int value) const { std::cout << value << std::endl; return Number(value); } }; template<typename T> auto operator>>(const Number& num, const T& operation) { return operation.apply(num.getValue()); } |
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 | #include <iostream> class Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} template<typename T> auto operator>>(const T& operation) const { return operation.apply(value); } }; class Square { public: Number apply(int value) const { return Number(value * value); } }; class Inc { public: Number apply(int value) const { return Number(value + 1); } }; class Print { public: Number apply(int value) const { std::cout << value << std::endl; return Number(value); } }; template<typename T> auto operator>>(const Number& num, const T& operation) { return operation.apply(num.getValue()); } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:56:1: error: 'Const' was not declared in this scope; did you mean 'const'?
56 | 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 that chain of operations:
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 Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} template<typename Operation> auto operator>>(Operation op) const { return op(Number(value)); } }; class Square { public: Number operator()(Number n) const { return Number(n.getValue() * n.getValue()); } }; class Inc { public: Number operator()(Number n) const { return Number(n.getValue() + 1); } }; class Print { public: Number operator()(Number n) const { std::cout << n.getValue() << std::endl; return n; } }; 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 | #include <iostream> class Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} template<typename Operation> auto operator>>(Operation op) const { return op(Number(value)); } }; class Square { public: Number operator()(Number n) const { return Number(n.getValue() * n.getValue()); } }; class Inc { public: Number operator()(Number n) const { return Number(n.getValue() + 1); } }; class Print { public: Number operator()(Number n) const { std::cout << n.getValue() << std::endl; return n; } }; 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 | #include <iostream> class Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} template<typename Operation> auto operator>>(Operation op) const { return op(Number(value)); } }; class Square { public: Number operator()(Number n) const { return Number(n.getValue() * n.getValue()); } }; class Inc { public: Number operator()(Number n) const { return Number(n.getValue() + 1); } }; class Print { public: Number operator()(Number n) const { std::cout << n.getValue() << std::endl; return n; } }; 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 function 'int __delete_this__main()':
main.cpp:49:25: error: no match for 'operator>>' (operand types are 'Number' and 'Inc')
49 | Cost(5) >> Square() >> Inc() >> Inc() >> Print();
| ~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~
| | |
| Number Inc
main.cpp: In function 'int main()':
main.cpp:56:1: error: 'Const' was not declared in this scope; did you mean 'const'?
56 | 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++ solution that implements a chain of operations using the ">>" operator:
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 Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } void setValue(int v) { value = v; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} Number operator>>(const class Square& square) const { return Number(value); } }; class Square { public: friend Number operator>>(const Number& num, const Square& square) { return Number(num.getValue() * num.getValue()); } }; class Inc { public: friend Number operator>>(const Number& num, const Inc& inc) { return Number(num.getValue() + 1); } }; class Print { public: friend void operator>>(const Number& num, const Print& print) { std::cout << num.getValue() << std::endl; } }; 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 | #include <iostream> class Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } void setValue(int v) { value = v; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} Number operator>>(const class Square& square) const { return Number(value); } }; class Square { public: friend Number operator>>(const Number& num, const Square& square) { return Number(num.getValue() * num.getValue()); } }; class Inc { public: friend Number operator>>(const Number& num, const Inc& inc) { return Number(num.getValue() + 1); } }; class Print { public: friend void operator>>(const Number& num, const Print& print) { std::cout << num.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 | #include <iostream> class Number { private: int value; public: Number(int v) : value(v) {} int getValue() const { return value; } void setValue(int v) { value = v; } }; class Cost { private: int value; public: Cost(int v) : value(v) {} Number operator>>(const class Square& square) const { return Number(value); } }; class Square { public: friend Number operator>>(const Number& num, const Square& square) { return Number(num.getValue() * num.getValue()); } }; class Inc { public: friend Number operator>>(const Number& num, const Inc& inc) { return Number(num.getValue() + 1); } }; class Print { public: friend void operator>>(const Number& num, const Print& print) { std::cout << num.getValue() << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:50:1: error: 'Const' was not declared in this scope; did you mean 'const'?
50 | 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