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 is the implementation of the Const, Square, Print classes in C++:
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) {} friend class Square; }; class Square { private: int value; public: void operator>>(Square& s) { s.value = value * value; } friend class Print; }; class Inc { public: void operator>>(Square& s) { s.value += 1; } }; class Print { public: void operator>>(Square& s) { std::cout << s.value << std::endl; } }; int main() { Square square; 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int value; public: void operator>>(Square& s) { s.value = value * value; } friend class Print; }; class Inc { public: void operator>>(Square& s) { s.value += 1; } }; class Print { public: void operator>>(Square& s) { std::cout << s.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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int value; public: void operator>>(Square& s) { s.value = value * value; } friend class Print; }; class Inc { public: void operator>>(Square& s) { s.value += 1; } }; class Print { public: void operator>>(Square& s) { std::cout << s.value << std::endl; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In member function 'void Inc::operator>>(Square&)':
main.cpp:27:11: error: 'int Square::value' is private within this context
27 | s.value += 1;
| ^~~~~
main.cpp:15:9: note: declared private here
15 | int value;
| ^~~~~
main.cpp: In function 'int main()':
main.cpp:41:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
41 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
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.
To achieve this, you can define three classes in C++ - Const, Square, and Print. Here's an example implementation:
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 Const { private: int value; public: Const(int val) : value(val) {} int getValue() { return value; } }; class Square { public: void operator>>(Const& c) { c = Const(c.getValue() * c.getValue()); } }; class Inc { public: void operator>>(Const& c) { c = Const(c.getValue() + 1); } }; class Print { public: void operator>>(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square sq; Inc inc; Print pr; c >> sq >> inc >> inc >> pr; 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() { return value; } }; class Square { public: void operator>>(Const& c) { c = Const(c.getValue() * c.getValue()); } }; class Inc { public: void operator>>(Const& c) { c = Const(c.getValue() + 1); } }; class Print { public: void operator>>(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square sq; Inc inc; Print pr; c >> sq >> inc >> inc >> pr; 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() { return value; } }; class Square { public: void operator>>(Const& c) { c = Const(c.getValue() * c.getValue()); } }; class Inc { public: void operator>>(Const& c) { c = Const(c.getValue() + 1); } }; class Print { public: void operator>>(Const& c) { std::cout << c.getValue() << std::endl; } }; int __delete_this__main() { Const c(5); Square sq; Inc inc; Print pr; c >> sq >> inc >> inc >> pr; return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int __delete_this__main()':
main.cpp:42:7: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
42 | c >> sq >> inc >> inc >> pr;
| ~ ^~ ~~
| | |
| | Square
| Const
main.cpp: In function 'int main()':
main.cpp:50:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
50 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
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 is the C++ code for the Const, Square, Print classes:
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) {} friend class Square; }; class Square { private: int value; public: Square() {} Square& operator>>(Square& square) { square.value = value * value; return square; } Square& operator>>(Const& constant) { value = constant.value; return *this; } Square& operator>>(Print& printer) { printer.print(value); return *this; } Square& operator>>(int num) { value += num; return *this; } }; class Print { public: void print(int value) { std::cout << value << std::endl; } }; int main() { Const(5) >> Square() >> Square() >> Square() >> 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 Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int value; public: Square() {} Square& operator>>(Square& square) { square.value = value * value; return square; } Square& operator>>(Const& constant) { value = constant.value; return *this; } Square& operator>>(Print& printer) { printer.print(value); return *this; } Square& operator>>(int num) { value += num; return *this; } }; class Print { public: void print(int value) { std::cout << value << std::endl; } }; int main() { Const(5) >> Square() >> Square() >> Square() >> 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 Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int value; public: Square() {} Square& operator>>(Square& square) { square.value = value * value; return square; } Square& operator>>(Const& constant) { value = constant.value; return *this; } Square& operator>>(Print& printer) { printer.print(value); return *this; } Square& operator>>(int num) { value += num; return *this; } }; class Print { public: void print(int value) { std::cout << value << std::endl; } }; int __delete_this__main() { Const(5) >> Square() >> Square() >> Square() >> Print(); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:29:24: error: 'Print' has not been declared
29 | Square& operator>>(Print& printer) {
| ^~~~~
main.cpp: In member function 'Square& Square::operator>>(int&)':
main.cpp:30:17: error: request for member 'print' in 'printer', which is of non-class type 'int'
30 | printer.print(value);
| ^~~~~
main.cpp: In function 'int __delete_this__main()':
main.cpp:48:14: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
48 | Const(5) >> Square() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp: In function 'int main()':
main.cpp:56:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
56 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:56:25: error: 'Inc' was not declared in this scope
56 | 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
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 is a C++ implementation of the requested classes:
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 { public: Const(int value) : value(value) {} int getValue() const { return value; } private: int value; }; class Square { public: void operator()(Const& c) { int val = c.getValue(); c = Const(val * val); } }; class Inc { public: void operator()(Const& c) { int val = c.getValue(); c = Const(val + 1); } }; class Print { public: void operator()(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square square; Inc inc; Print print; square(c); inc(c); inc(c); print(c); 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 Const { public: Const(int value) : value(value) {} int getValue() const { return value; } private: int value; }; class Square { public: void operator()(Const& c) { int val = c.getValue(); c = Const(val * val); } }; class Inc { public: void operator()(Const& c) { int val = c.getValue(); c = Const(val + 1); } }; class Print { public: void operator()(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square square; Inc inc; Print print; square(c); inc(c); inc(c); print(c); 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 Const { public: Const(int value) : value(value) {} int getValue() const { return value; } private: int value; }; class Square { public: void operator()(Const& c) { int val = c.getValue(); c = Const(val * val); } }; class Inc { public: void operator()(Const& c) { int val = c.getValue(); c = Const(val + 1); } }; class Print { public: void operator()(Const& c) { std::cout << c.getValue() << std::endl; } }; int __delete_this__main() { Const c(5); Square square; Inc inc; Print print; square(c); inc(c); inc(c); print(c); return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:56:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
56 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
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 is the C++ code for the classes Const, Square, Print:
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 Const { private: int value; public: Const(int val) : value(val) {} int getValue() { return value; } }; class Square { public: Square() {} void operator>>(Const& obj) { int val = obj.getValue(); obj = Const(val * val); } }; class Inc { public: Inc() {} void operator>>(Const& obj) { int val = obj.getValue(); obj = Const(val + 1); } }; class Print { public: Print() {} void operator>>(Const& obj) { std::cout << obj.getValue() << std::endl; } }; int main() { Const c(5); Square s; Inc i1; Inc i2; Print p; c >> s >> i1 >> i2 >> p; 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() { return value; } }; class Square { public: Square() {} void operator>>(Const& obj) { int val = obj.getValue(); obj = Const(val * val); } }; class Inc { public: Inc() {} void operator>>(Const& obj) { int val = obj.getValue(); obj = Const(val + 1); } }; class Print { public: Print() {} void operator>>(Const& obj) { std::cout << obj.getValue() << std::endl; } }; int main() { Const c(5); Square s; Inc i1; Inc i2; Print p; c >> s >> i1 >> i2 >> p; 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() { return value; } }; class Square { public: Square() {} void operator>>(Const& obj) { int val = obj.getValue(); obj = Const(val * val); } }; class Inc { public: Inc() {} void operator>>(Const& obj) { int val = obj.getValue(); obj = Const(val + 1); } }; class Print { public: Print() {} void operator>>(Const& obj) { std::cout << obj.getValue() << std::endl; } }; int __delete_this__main() { Const c(5); Square s; Inc i1; Inc i2; Print p; c >> s >> i1 >> i2 >> p; return 0; } int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp: In function 'int __delete_this__main()':
main.cpp:51:7: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
51 | c >> s >> i1 >> i2 >> p;
| ~ ^~ ~
| | |
| | Square
| Const
main.cpp: In function 'int main()':
main.cpp:59:10: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
59 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False