Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is a possible 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 | #include <iostream> class Const { public: int value; Const(int val) : value(val) {} friend Const operator>>(Const c, Square s) { return Const(s.square(c.value)); } friend Const operator>>(Const c, Print p) { p.print(c.value); return c; } }; class Square { public: int square(int val) { return val * val; } }; class Print { public: void print(int val) { std::cout << val << std::endl; } }; int main() { Const(5) >> 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 | #include <iostream> class Const { public: int value; Const(int val) : value(val) {} friend Const operator>>(Const c, Square s) { return Const(s.square(c.value)); } friend Const operator>>(Const c, Print p) { p.print(c.value); return c; } }; class Square { public: int square(int val) { return val * val; } }; class Print { public: void print(int val) { std::cout << val << std::endl; } }; int main() { Const(5) >> 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 | #include <iostream> class Const { public: int value; Const(int val) : value(val) {} friend Const operator>>(Const c, Square s) { return Const(s.square(c.value)); } friend Const operator>>(Const c, Print p) { p.print(c.value); return c; } }; class Square { public: int square(int val) { return val * val; } }; class Print { public: void print(int val) { std::cout << val << std::endl; } }; int main() { Const(5) >> Square() >> Print(); return 0; } |
main.cpp:10:38: error: 'Square' has not been declared
10 | friend Const operator>>(Const c, Square s) {
| ^~~~~~
main.cpp:14:38: error: 'Print' has not been declared
14 | friend Const operator>>(Const c, Print p) {
| ^~~~~
main.cpp:14:18: error: redefinition of 'Const operator>>(Const, int)'
14 | friend Const operator>>(Const c, Print p) {
| ^~~~~~~~
main.cpp:10:18: note: 'Const operator>>(Const, int)' previously defined here
10 | friend Const operator>>(Const c, Square s) {
| ^~~~~~~~
main.cpp: In function 'Const operator>>(Const, int)':
main.cpp:11:24: error: request for member 'square' in 's', which is of non-class type 'int'
11 | return Const(s.square(c.value));
| ^~~~~~
main.cpp: In function 'int main()':
main.cpp:35:14: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
35 | Const(5) >> Square() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.cpp:10:18: note: candidate: 'Const operator>>(Const, int)'
10 | friend Const operator>>(Const c, Square s) {
| ^~~~~~~~
main.cpp:10:45: note: no known conversion for argument 2 from 'Square' to 'int'
10 | friend Const operator>>(Const c, Square s) {
| ~~~~~~~^
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is a possible implementation of the classes Const, Square, Print 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 44 45 46 47 48 49 50 51 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } }; class Square { public: Square() = default; void operator>>(Const& c) { c = Const(c.getValue() * c.getValue()); } }; class Inc { public: Inc() = default; void operator>>(Const& c) { c = Const(c.getValue() + 1); } }; class Print { public: Print() = default; void operator>>(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square sq; Inc inc; Print pr; c >> sq >> 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 45 46 47 48 49 50 51 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { return value; } }; class Square { public: Square() = default; void operator>>(Const& c) { c = Const(c.getValue() * c.getValue()); } }; class Inc { public: Inc() = default; void operator>>(Const& c) { c = Const(c.getValue() + 1); } }; class Print { public: Print() = default; void operator>>(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square sq; Inc inc; Print pr; c >> sq >> 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() const { return value; } }; class Square { public: Square() = default; void operator>>(Const& c) { c = Const(c.getValue() * c.getValue()); } }; class Inc { public: Inc() = default; void operator>>(Const& c) { c = Const(c.getValue() + 1); } }; class Print { public: Print() = default; void operator>>(Const& c) { std::cout << c.getValue() << std::endl; } }; int main() { Const c(5); Square sq; Inc inc; Print pr; c >> sq >> inc >> pr; return 0; } |
main.cpp: In function 'int main()':
main.cpp:49:7: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
49 | c >> sq >> inc >> pr;
| ~ ^~ ~~
| | |
| | Square
| Const
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is a C++ program that defines 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 | #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 >> 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 | #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 >> 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 | #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 >> pr; return 0; } |
main.cpp: In function 'int main()':
main.cpp:39:7: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
39 | c >> sq >> inc >> pr;
| ~ ^~ ~~
| | |
| | Square
| Const
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Here is a possible implementation of the `Const`, `Square`, `Inc`, and `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 44 45 46 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int result; public: Square() {} Square& operator>>(const Const& obj) { result = obj.value * obj.value; return *this; } Square& operator>>(const class Inc&) { result += 1; return *this; } friend class Print; }; class Inc {}; class Print { public: Print() {} void operator>>(const Square& obj) { std::cout << obj.result << std::endl; } }; int main() { Const(5) >> Square() >> 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int result; public: Square() {} Square& operator>>(const Const& obj) { result = obj.value * obj.value; return *this; } Square& operator>>(const class Inc&) { result += 1; return *this; } friend class Print; }; class Inc {}; class Print { public: Print() {} void operator>>(const Square& obj) { std::cout << obj.result << std::endl; } }; int main() { Const(5) >> Square() >> 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} friend class Square; }; class Square { private: int result; public: Square() {} Square& operator>>(const Const& obj) { result = obj.value * obj.value; return *this; } Square& operator>>(const class Inc&) { result += 1; return *this; } friend class Print; }; class Inc {}; class Print { public: Print() {} void operator>>(const Square& obj) { std::cout << obj.result << std::endl; } }; int main() { Const(5) >> Square() >> Inc() >> Print(); return 0; } |
main.cpp: In function 'int main()':
main.cpp:44:14: error: no match for 'operator>>' (operand types are 'Const' and 'Square')
44 | Const(5) >> Square() >> Inc() >> Print();
| ~~~~~~~~ ^~ ~~~~~~~~
| | |
| Const Square
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 to the screen.
Write a C++ class Const, Square, Print so that I can write, for example, Cost(5) >> Square() >> Inc() >> Print() and the resulting python program will print 26 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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { 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 >> pr; return 0; } |
1 | 26
|
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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { 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; } }; |
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 | #include <iostream> class Const { private: int value; public: Const(int val) : value(val) {} int getValue() const { 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; } }; |
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/11/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x1b): undefined reference to `main'
collect2: error: ld returned 1 exit status
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string All tests passed
: False