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++ class implementation that allows you to write `Cost(5) >> Square() >> Inc() >> Inc() >> Print()` and 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} Const& operator>>(Const& (*func)(Const&)) { return func(*this); } int getValue() const { return value; } void setValue(int val) { value = val; } }; Const& Square(Const& obj) { int val = obj.getValue(); obj.setValue(val * val); return obj; } Const& Inc(Const& obj) { int val = obj.getValue(); obj.setValue(val + 1); return obj; } Const& Print(Const& obj) { std::cout << obj.getValue() << std::endl; return obj; } 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} Const& operator>>(Const& (*func)(Const&)) { return func(*this); } int getValue() const { return value; } void setValue(int val) { value = val; } }; Const& Square(Const& obj) { int val = obj.getValue(); obj.setValue(val * val); return obj; } Const& Inc(Const& obj) { int val = obj.getValue(); obj.setValue(val + 1); return obj; } Const& Print(Const& obj) { std::cout << obj.getValue() << std::endl; return obj; } 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} Const& operator>>(Const& (*func)(Const&)) { return func(*this); } int getValue() const { return value; } void setValue(int val) { value = val; } }; Const& Square(Const& obj) { int val = obj.getValue(); obj.setValue(val * val); return obj; } Const& Inc(Const& obj) { int val = obj.getValue(); obj.setValue(val + 1); return obj; } Const& Print(Const& obj) { std::cout << obj.getValue() << std::endl; return obj; } 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 function 'int __delete_this__main()':
main.cpp:42:23: error: too few arguments to function 'Const& Square(Const&)'
42 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ~~~~~~^~
main.cpp:24:8: note: declared here
24 | Const& Square(Const& obj) {
| ^~~~~~
main.cpp:42:32: error: too few arguments to function 'Const& Inc(Const&)'
42 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ~~~^~
main.cpp:30:8: note: declared here
30 | Const& Inc(Const& obj) {
| ^~~
main.cpp:42:41: error: too few arguments to function 'Const& Inc(Const&)'
42 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ~~~^~
main.cpp:30:8: note: declared here
30 | Const& Inc(Const& obj) {
| ^~~
main.cpp:42:52: error: too few arguments to function 'Const& Print(Const&)'
42 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ~~~~~^~
main.cpp:36:8: note: declared here
36 | Const& Print(Const& obj) {
| ^~~~~
main.cpp: In function 'int main()':
main.cpp:49:19: error: too few arguments to function 'Const& Square(Const&)'
49 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~^~
main.cpp:24:8: note: declared here
24 | Const& Square(Const& obj) {
| ^~~~~~
main.cpp:49:28: error: too few arguments to function 'Const& Inc(Const&)'
49 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~^~
main.cpp:30:8: note: declared here
30 | Const& Inc(Const& obj) {
| ^~~
main.cpp:49:40: error: too few arguments to function 'Const& Square(Const&)'
49 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~^~
main.cpp:24:8: note: declared here
24 | Const& Square(Const& obj) {
| ^~~~~~
main.cpp:49:52: error: too few arguments to function 'Const& Square(Const&)'
49 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~^~
main.cpp:24:8: note: declared here
24 | Const& Square(Const& obj) {
| ^~~~~~
main.cpp:49:63: error: too few arguments to function 'Const& Print(Const&)'
49 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~^~
main.cpp:36:8: note: declared here
36 | Const& Print(Const& obj) {
| ^~~~~
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.
Sure, here's a C++ implementation of the `Const`, `Square`, and `Print` classes that allow you to perform the operations 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 Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>(Square& s) { s.setValue(value); return *this; } Const& operator>>(Print& p) { p.printValue(value); return *this; } }; class Square { private: int value; public: void setValue(int val) { value = val * val; } int getValue() const { return value; } Square& operator>>=(Const& c) { setValue(c.getValue()); return *this; } Square& operator>>(Inc& i) { i.setValue(value); return *this; } }; class Inc { private: int value; public: void setValue(int val) { value = val + 1; } int getValue() const { return value; } Inc& operator>>=(Square& s) { setValue(s.getValue()); return *this; } Inc& operator>>(Print& p) { p.printValue(value); return *this; } }; class Print { public: void printValue(int val) { std::cout << val << std::endl; } }; 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>(Square& s) { s.setValue(value); return *this; } Const& operator>>(Print& p) { p.printValue(value); return *this; } }; class Square { private: int value; public: void setValue(int val) { value = val * val; } int getValue() const { return value; } Square& operator>>=(Const& c) { setValue(c.getValue()); return *this; } Square& operator>>(Inc& i) { i.setValue(value); return *this; } }; class Inc { private: int value; public: void setValue(int val) { value = val + 1; } int getValue() const { return value; } Inc& operator>>=(Square& s) { setValue(s.getValue()); return *this; } Inc& operator>>(Print& p) { p.printValue(value); return *this; } }; class Print { public: void printValue(int val) { std::cout << val << std::endl; } }; 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>(Square& s) { s.setValue(value); return *this; } Const& operator>>(Print& p) { p.printValue(value); return *this; } }; class Square { private: int value; public: void setValue(int val) { value = val * val; } int getValue() const { return value; } Square& operator>>=(Const& c) { setValue(c.getValue()); return *this; } Square& operator>>(Inc& i) { i.setValue(value); return *this; } }; class Inc { private: int value; public: void setValue(int val) { value = val + 1; } int getValue() const { return value; } Inc& operator>>=(Square& s) { setValue(s.getValue()); return *this; } Inc& operator>>(Print& p) { p.printValue(value); return *this; } }; class Print { public: void printValue(int val) { std::cout << val << std::endl; } }; 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:15:23: error: 'Square' has not been declared
15 | Const& operator>>(Square& s) {
| ^~~~~~
main.cpp:20:23: error: 'Print' has not been declared
20 | Const& operator>>(Print& p) {
| ^~~~~
main.cpp:20:12: error: 'Const& Const::operator>>(int&)' cannot be overloaded with 'Const& Const::operator>>(int&)'
20 | Const& operator>>(Print& p) {
| ^~~~~~~~
main.cpp:15:12: note: previous declaration 'Const& Const::operator>>(int&)'
15 | Const& operator>>(Square& s) {
| ^~~~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&)':
main.cpp:16:11: error: request for member 'setValue' in 's', which is of non-class type 'int'
16 | s.setValue(value);
| ^~~~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&)':
main.cpp:21:11: error: request for member 'printValue' in 'p', which is of non-class type 'int'
21 | p.printValue(value);
| ^~~~~~~~~~
main.cpp: At global scope:
main.cpp:44:24: error: 'Inc' has not been declared
44 | Square& operator>>(Inc& i) {
| ^~~
main.cpp: In member function 'Square& Square::operator>>(int&)':
main.cpp:45:11: error: request for member 'setValue' in 'i', which is of non-class type 'int'
45 | i.setValue(value);
| ^~~~~~~~
main.cpp: At global scope:
main.cpp:68:21: error: 'Print' has not been declared
68 | Inc& operator>>(Print& p) {
| ^~~~~
main.cpp: In member function 'Inc& Inc::operator>>(int&)':
main.cpp:69:11: error: request for member 'printValue' in 'p', which is of non-class type 'int'
69 | p.printValue(value);
| ^~~~~~~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:82:14: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
82 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:15:12: note: candidate: 'Const& Const::operator>>(int&)'
15 | Const& operator>>(Square& s) {
| ^~~~~~~~
main.cpp:15:31: note: no known conversion for argument 1 from 'Square' to 'int&'
15 | Const& operator>>(Square& s) {
| ~~~~~~~~^
main.cpp: In function 'int main()':
main.cpp:89:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
89 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:15:12: note: candidate: 'Const& Const::operator>>(int&)'
15 | Const& operator>>(Square& s) {
| ^~~~~~~~
main.cpp:15:31: note: no known conversion for argument 1 from 'Square' to 'int&'
15 | Const& operator>>(Square& s) {
| ~~~~~~~~^
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.
Certainly! Here's a C++ class implementation that allows you to write the provided expression and prints `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 | #include <iostream> class Cost { private: int value; public: Cost(int val) : value(val) {} Cost& operator>>=(Cost& (*func)(Cost&)) { return func(*this); } }; Cost& Square(Cost& obj) { obj.value *= obj.value; return obj; } Cost& Inc(Cost& obj) { ++obj.value; return obj; } Cost& Print(Cost& obj) { std::cout << obj.value << std::endl; return obj; } int main() { Cost(5) >>= Square >>= Inc >>= Inc >>= Print; return 0; } |
1 | Cost(5) >>= Square >>= Inc >>= Inc >>= Print; |
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 | #include <iostream> class Cost { private: int value; public: Cost(int val) : value(val) {} Cost& operator>>=(Cost& (*func)(Cost&)) { return func(*this); } }; Cost& Square(Cost& obj) { obj.value *= obj.value; return obj; } Cost& Inc(Cost& obj) { ++obj.value; return obj; } Cost& Print(Cost& obj) { std::cout << obj.value << std::endl; return obj; } |
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 | #include <iostream> class Cost { private: int value; public: Cost(int val) : value(val) {} Cost& operator>>=(Cost& (*func)(Cost&)) { return func(*this); } }; Cost& Square(Cost& obj) { obj.value *= obj.value; return obj; } Cost& Inc(Cost& obj) { ++obj.value; return obj; } Cost& Print(Cost& obj) { std::cout << obj.value << std::endl; return obj; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'Cost& Square(Cost&)':
main.cpp:17:9: error: 'int Cost::value' is private within this context
17 | obj.value *= obj.value;
| ^~~~~
main.cpp:6:9: note: declared private here
6 | int value;
| ^~~~~
main.cpp:17:22: error: 'int Cost::value' is private within this context
17 | obj.value *= obj.value;
| ^~~~~
main.cpp:6:9: note: declared private here
6 | int value;
| ^~~~~
main.cpp: In function 'Cost& Inc(Cost&)':
main.cpp:22:11: error: 'int Cost::value' is private within this context
22 | ++obj.value;
| ^~~~~
main.cpp:6:9: note: declared private here
6 | int value;
| ^~~~~
main.cpp: In function 'Cost& Print(Cost&)':
main.cpp:27:22: error: 'int Cost::value' is private within this context
27 | std::cout << obj.value << std::endl;
| ^~~~~
main.cpp:6:9: note: declared private here
6 | int value;
| ^~~~~
main.cpp: In function 'int main()':
main.cpp:34:1: error: 'Const' was not declared in this scope; did you mean 'Cost'?
34 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| Cost
main.cpp:34:19: error: too few arguments to function 'Cost& Square(Cost&)'
34 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~^~
main.cpp:16:7: note: declared here
16 | Cost& Square(Cost& obj) {
| ^~~~~~
main.cpp:34:28: error: too few arguments to function 'Cost& Inc(Cost&)'
34 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~^~
main.cpp:21:7: note: declared here
21 | Cost& Inc(Cost& obj) {
| ^~~
main.cpp:34:40: error: too few arguments to function 'Cost& Square(Cost&)'
34 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~^~
main.cpp:16:7: note: declared here
16 | Cost& Square(Cost& obj) {
| ^~~~~~
main.cpp:34:52: error: too few arguments to function 'Cost& Square(Cost&)'
34 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~^~
main.cpp:16:7: note: declared here
16 | Cost& Square(Cost& obj) {
| ^~~~~~
main.cpp:34:63: error: too few arguments to function 'Cost& Print(Cost&)'
34 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~^~
main.cpp:26:7: note: declared here
26 | Cost& Print(Cost& obj) {
| ^~~~~
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`, and `Print` that allows you to chain the method calls as requested and print the result 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>(Square& sq) { sq.setValue(value); return *this; } Const& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Square { private: int value; public: Square() : value(0) {} void setValue(int val) { value = val * val; } Square& operator>>=(Inc& inc) { inc.setValue(value); return *this; } Square& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Inc { private: int value; public: Inc() : value(0) {} void setValue(int val) { value = val + 1; } Inc& operator>>=(Inc& inc) { inc.setValue(value); return inc; } Inc& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Print { private: int value; public: Print() : value(0) {} void setValue(int val) { value = val; } void operator()() const { std::cout << value << std::endl; } }; 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>(Square& sq) { sq.setValue(value); return *this; } Const& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Square { private: int value; public: Square() : value(0) {} void setValue(int val) { value = val * val; } Square& operator>>=(Inc& inc) { inc.setValue(value); return *this; } Square& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Inc { private: int value; public: Inc() : value(0) {} void setValue(int val) { value = val + 1; } Inc& operator>>=(Inc& inc) { inc.setValue(value); return inc; } Inc& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Print { private: int value; public: Print() : value(0) {} void setValue(int val) { value = val; } void operator()() const { std::cout << value << std::endl; } }; 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 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 Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>(Square& sq) { sq.setValue(value); return *this; } Const& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Square { private: int value; public: Square() : value(0) {} void setValue(int val) { value = val * val; } Square& operator>>=(Inc& inc) { inc.setValue(value); return *this; } Square& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Inc { private: int value; public: Inc() : value(0) {} void setValue(int val) { value = val + 1; } Inc& operator>>=(Inc& inc) { inc.setValue(value); return inc; } Inc& operator>>(Print& pr) { pr.setValue(value); return *this; } }; class Print { private: int value; public: Print() : value(0) {} void setValue(int val) { value = val; } void operator()() const { std::cout << value << std::endl; } }; 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:15:23: error: 'Square' has not been declared
15 | Const& operator>>(Square& sq) {
| ^~~~~~
main.cpp:20:23: error: 'Print' has not been declared
20 | Const& operator>>(Print& pr) {
| ^~~~~
main.cpp:20:12: error: 'Const& Const::operator>>(int&)' cannot be overloaded with 'Const& Const::operator>>(int&)'
20 | Const& operator>>(Print& pr) {
| ^~~~~~~~
main.cpp:15:12: note: previous declaration 'Const& Const::operator>>(int&)'
15 | Const& operator>>(Square& sq) {
| ^~~~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&)':
main.cpp:16:12: error: request for member 'setValue' in 'sq', which is of non-class type 'int'
16 | sq.setValue(value);
| ^~~~~~~~
main.cpp: In member function 'Const& Const::operator>>(int&)':
main.cpp:21:12: error: request for member 'setValue' in 'pr', which is of non-class type 'int'
21 | pr.setValue(value);
| ^~~~~~~~
main.cpp: At global scope:
main.cpp:37:25: error: 'Inc' has not been declared
37 | Square& operator>>=(Inc& inc) {
| ^~~
main.cpp:42:24: error: 'Print' has not been declared
42 | Square& operator>>(Print& pr) {
| ^~~~~
main.cpp: In member function 'Square& Square::operator>>=(int&)':
main.cpp:38:13: error: request for member 'setValue' in 'inc', which is of non-class type 'int'
38 | inc.setValue(value);
| ^~~~~~~~
main.cpp: In member function 'Square& Square::operator>>(int&)':
main.cpp:43:12: error: request for member 'setValue' in 'pr', which is of non-class type 'int'
43 | pr.setValue(value);
| ^~~~~~~~
main.cpp: At global scope:
main.cpp:64:21: error: 'Print' has not been declared
64 | Inc& operator>>(Print& pr) {
| ^~~~~
main.cpp: In member function 'Inc& Inc::operator>>(int&)':
main.cpp:65:12: error: request for member 'setValue' in 'pr', which is of non-class type 'int'
65 | pr.setValue(value);
| ^~~~~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:87:14: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
87 | Const(5) >> Square() >> Inc() >> Inc() >> Print()();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:15:12: note: candidate: 'Const& Const::operator>>(int&)'
15 | Const& operator>>(Square& sq) {
| ^~~~~~~~
main.cpp:15:31: note: no known conversion for argument 1 from 'Square' to 'int&'
15 | Const& operator>>(Square& sq) {
| ~~~~~~~~^~
main.cpp: In function 'int main()':
main.cpp:94:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
94 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:15:12: note: candidate: 'Const& Const::operator>>(int&)'
15 | Const& operator>>(Square& sq) {
| ^~~~~~~~
main.cpp:15:31: note: no known conversion for argument 1 from 'Square' to 'int&'
15 | Const& operator>>(Square& sq) {
| ~~~~~~~~^~
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 `Const`, `Square`, and `Print` classes that satisfy the given requirements:
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 Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>=(Square& sq) { sq.setValue(value); return *this; } Const& operator>>=(Print& print) { print.printValue(value); return *this; } }; class Square { private: int value; public: void setValue(int val) { value = val * val; } Square& operator>>=(Square& sq) { sq.setValue(value); return sq; } Square& operator>>=(Print& print) { print.printValue(value); return *this; } }; class Print { public: void printValue(int val) { std::cout << val << std::endl; } }; int main() { Const c(5); Square sq; Print print; c >>= sq >>= sq >>= print; return 0; } |
1 | c >>= sq >>= sq >>= print; |
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 Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>=(Square& sq) { sq.setValue(value); return *this; } Const& operator>>=(Print& print) { print.printValue(value); return *this; } }; class Square { private: int value; public: void setValue(int val) { value = val * val; } Square& operator>>=(Square& sq) { sq.setValue(value); return sq; } Square& operator>>=(Print& print) { print.printValue(value); return *this; } }; class Print { public: void printValue(int val) { std::cout << val << 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } Const& operator>>=(Square& sq) { sq.setValue(value); return *this; } Const& operator>>=(Print& print) { print.printValue(value); return *this; } }; class Square { private: int value; public: void setValue(int val) { value = val * val; } Square& operator>>=(Square& sq) { sq.setValue(value); return sq; } Square& operator>>=(Print& print) { print.printValue(value); return *this; } }; class Print { public: void printValue(int val) { std::cout << val << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:14:24: error: 'Square' has not been declared
14 | Const& operator>>=(Square& sq) {
| ^~~~~~
main.cpp:19:24: error: 'Print' has not been declared
19 | Const& operator>>=(Print& print) {
| ^~~~~
main.cpp:19:12: error: 'Const& Const::operator>>=(int&)' cannot be overloaded with 'Const& Const::operator>>=(int&)'
19 | Const& operator>>=(Print& print) {
| ^~~~~~~~
main.cpp:14:12: note: previous declaration 'Const& Const::operator>>=(int&)'
14 | Const& operator>>=(Square& sq) {
| ^~~~~~~~
main.cpp: In member function 'Const& Const::operator>>=(int&)':
main.cpp:15:12: error: request for member 'setValue' in 'sq', which is of non-class type 'int'
15 | sq.setValue(value);
| ^~~~~~~~
main.cpp: In member function 'Const& Const::operator>>=(int&)':
main.cpp:20:15: error: request for member 'printValue' in 'print', which is of non-class type 'int'
20 | print.printValue(value);
| ^~~~~~~~~~
main.cpp: At global scope:
main.cpp:39:25: error: 'Print' has not been declared
39 | Square& operator>>=(Print& print) {
| ^~~~~
main.cpp: In member function 'Square& Square::operator>>=(int&)':
main.cpp:40:15: error: request for member 'printValue' in 'print', which is of non-class type 'int'
40 | print.printValue(value);
| ^~~~~~~~~~
main.cpp: In function 'int main()':
main.cpp:54:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
54 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:54:25: error: 'Inc' was not declared in this scope
54 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False