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.
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 | class Const { public: Const(int value) : value_(value) {} Const operator>>(const Square&) const { return Square(value_); } private: int value_; }; class Square { public: explicit Square(int value) : value_(value) {} Square operator>>(const Inc&) const { return Inc(value_ * value_); } private: int value_; }; class Inc { public: explicit Inc(int value) : value_(value) {} Inc operator>>(const Inc&) const { return Inc(value_ + 1); } void operator>>(const Print&) const { std::cout << value_ << std::endl; } private: int value_; }; 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 | class Const { public: Const(int value) : value_(value) {} Const operator>>(const Square&) const { return Square(value_); } private: int value_; }; class Square { public: explicit Square(int value) : value_(value) {} Square operator>>(const Inc&) const { return Inc(value_ * value_); } private: int value_; }; class Inc { public: explicit Inc(int value) : value_(value) {} Inc operator>>(const Inc&) const { return Inc(value_ + 1); } void operator>>(const Print&) const { std::cout << value_ << std::endl; } private: int value_; }; 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 | class Const { public: Const(int value) : value_(value) {} Const operator>>(const Square&) const { return Square(value_); } private: int value_; }; class Square { public: explicit Square(int value) : value_(value) {} Square operator>>(const Inc&) const { return Inc(value_ * value_); } private: int value_; }; class Inc { public: explicit Inc(int value) : value_(value) {} Inc operator>>(const Inc&) const { return Inc(value_ + 1); } void operator>>(const Print&) const { std::cout << value_ << std::endl; } private: int value_; }; 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:6:26: error: 'Square' does not name a type
6 | Const operator>>(const Square&) const { return Square(value_); }
| ^~~~~~
main.cpp: In member function 'Const Const::operator>>(const int&) const':
main.cpp:6:50: error: 'Square' was not declared in this scope
6 | Const operator>>(const Square&) const { return Square(value_); }
| ^~~~~~
main.cpp: At global scope:
main.cpp:16:27: error: 'Inc' does not name a type
16 | Square operator>>(const Inc&) const { return Inc(value_ * value_); }
| ^~~
main.cpp: In member function 'Square Square::operator>>(const int&) const':
main.cpp:16:48: error: 'Inc' was not declared in this scope
16 | Square operator>>(const Inc&) const { return Inc(value_ * value_); }
| ^~~
main.cpp: At global scope:
main.cpp:28:25: error: 'Print' does not name a type; did you mean 'int'?
28 | void operator>>(const Print&) const { std::cout << value_ << std::endl; }
| ^~~~~
| int
main.cpp: In member function 'void Inc::operator>>(const int&) const':
main.cpp:28:46: error: 'cout' is not a member of 'std'
28 | void operator>>(const Print&) const { std::cout << value_ << std::endl; }
| ^~~~
main.cpp:1:1: note: 'std::cout' is defined in header '<iostream>'; did you forget to '#include <iostream>'?
+++ |+#include <iostream>
1 |
main.cpp:28:69: error: 'endl' is not a member of 'std'
28 | void operator>>(const Print&) const { std::cout << value_ << std::endl; }
| ^~~~
main.cpp:1:1: note: 'std::endl' is defined in header '<ostream>'; did you forget to '#include <ostream>'?
+++ |+#include <ostream>
1 |
main.cpp: In function 'int __delete_this__main()':
main.cpp:35:22: error: no matching function for call to 'Square::Square()'
35 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ^
main.cpp:14:12: note: candidate: 'Square::Square(int)'
14 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:14:12: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(const Square&)'
12 | class Square {
| ^~~~~~
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:35:31: error: no matching function for call to 'Inc::Inc()'
35 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ^
main.cpp:24:12: note: candidate: 'Inc::Inc(int)'
24 | explicit Inc(int value) : value_(value) {}
| ^~~
main.cpp:24:12: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
22 | class Inc {
| ^~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:35:40: error: no matching function for call to 'Inc::Inc()'
35 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ^
main.cpp:24:12: note: candidate: 'Inc::Inc(int)'
24 | explicit Inc(int value) : value_(value) {}
| ^~~
main.cpp:24:12: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
22 | class Inc {
| ^~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:35:45: error: 'Print' was not declared in this scope; did you mean 'int'?
35 | Const(5) >> Square() >> Inc() >> Inc() >> Print();
| ^~~~~
| int
main.cpp: In function 'int main()':
main.cpp:42:20: error: no matching function for call to 'Square::Square()'
42 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:14:12: note: candidate: 'Square::Square(int)'
14 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:14:12: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(const Square&)'
12 | class Square {
| ^~~~~~
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:42:29: error: no matching function for call to 'Inc::Inc()'
42 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:24:12: note: candidate: 'Inc::Inc(int)'
24 | explicit Inc(int value) : value_(value) {}
| ^~~
main.cpp:24:12: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
22 | class Inc {
| ^~~
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:22:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:22:7: note: candidate expects 1 argument, 0 provided
main.cpp:42:41: error: no matching function for call to 'Square::Square()'
42 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:14:12: note: candidate: 'Square::Square(int)'
14 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:14:12: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(const Square&)'
12 | class Square {
| ^~~~~~
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:42:53: error: no matching function for call to 'Square::Square()'
42 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:14:12: note: candidate: 'Square::Square(int)'
14 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:14:12: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(const Square&)'
12 | class Square {
| ^~~~~~
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:12:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:12:7: note: candidate expects 1 argument, 0 provided
main.cpp:42:58: error: 'Print' was not declared in this scope; did you mean 'int'?
42 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| int
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.
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 { public: Const(int value) : value_(value) {} Const operator>>(const Square&) const { return Square(value_); } private: int value_; }; class Square { public: explicit Square(int value) : value_(value) {} Square operator>>(const Inc&) const { return Inc(value_ * value_); } private: int value_; }; class Inc { public: explicit Inc(int value) : value_(value) {} Inc operator>>(const Print&) const { std::cout << value_ << std::endl; return *this; } private: int value_; }; int main() { Const(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 | 1250
|
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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} Const operator>>(const Square&) const { return Square(value_); } private: int value_; }; class Square { public: explicit Square(int value) : value_(value) {} Square operator>>(const Inc&) const { return Inc(value_ * value_); } private: int value_; }; class Inc { public: explicit Inc(int value) : value_(value) {} Inc operator>>(const Print&) const { std::cout << value_ << std::endl; return *this; } private: int 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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} Const operator>>(const Square&) const { return Square(value_); } private: int value_; }; class Square { public: explicit Square(int value) : value_(value) {} Square operator>>(const Inc&) const { return Inc(value_ * value_); } private: int value_; }; class Inc { public: explicit Inc(int value) : value_(value) {} Inc operator>>(const Print&) const { std::cout << value_ << std::endl; return *this; } private: int value_; }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:8:28: error: 'Square' does not name a type
8 | Const operator>>(const Square&) const {
| ^~~~~~
main.cpp: In member function 'Const Const::operator>>(const int&) const':
main.cpp:9:16: error: 'Square' was not declared in this scope
9 | return Square(value_);
| ^~~~~~
main.cpp: At global scope:
main.cpp:20:29: error: 'Inc' does not name a type
20 | Square operator>>(const Inc&) const {
| ^~~
main.cpp: In member function 'Square Square::operator>>(const int&) const':
main.cpp:21:16: error: 'Inc' was not declared in this scope
21 | return Inc(value_ * value_);
| ^~~
main.cpp: At global scope:
main.cpp:32:26: error: 'Print' does not name a type
32 | Inc operator>>(const Print&) const {
| ^~~~~
main.cpp: In function 'int main()':
main.cpp:45:20: error: no matching function for call to 'Square::Square()'
45 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:18:14: note: candidate: 'Square::Square(int)'
18 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:18:14: note: candidate expects 1 argument, 0 provided
main.cpp:16:7: note: candidate: 'constexpr Square::Square(const Square&)'
16 | class Square {
| ^~~~~~
main.cpp:16:7: note: candidate expects 1 argument, 0 provided
main.cpp:16:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:16:7: note: candidate expects 1 argument, 0 provided
main.cpp:45:29: error: no matching function for call to 'Inc::Inc()'
45 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:30:14: note: candidate: 'Inc::Inc(int)'
30 | explicit Inc(int value) : value_(value) {}
| ^~~
main.cpp:30:14: note: candidate expects 1 argument, 0 provided
main.cpp:28:7: note: candidate: 'constexpr Inc::Inc(const Inc&)'
28 | class Inc {
| ^~~
main.cpp:28:7: note: candidate expects 1 argument, 0 provided
main.cpp:28:7: note: candidate: 'constexpr Inc::Inc(Inc&&)'
main.cpp:28:7: note: candidate expects 1 argument, 0 provided
main.cpp:45:41: error: no matching function for call to 'Square::Square()'
45 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:18:14: note: candidate: 'Square::Square(int)'
18 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:18:14: note: candidate expects 1 argument, 0 provided
main.cpp:16:7: note: candidate: 'constexpr Square::Square(const Square&)'
16 | class Square {
| ^~~~~~
main.cpp:16:7: note: candidate expects 1 argument, 0 provided
main.cpp:16:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:16:7: note: candidate expects 1 argument, 0 provided
main.cpp:45:53: error: no matching function for call to 'Square::Square()'
45 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^
main.cpp:18:14: note: candidate: 'Square::Square(int)'
18 | explicit Square(int value) : value_(value) {}
| ^~~~~~
main.cpp:18:14: note: candidate expects 1 argument, 0 provided
main.cpp:16:7: note: candidate: 'constexpr Square::Square(const Square&)'
16 | class Square {
| ^~~~~~
main.cpp:16:7: note: candidate expects 1 argument, 0 provided
main.cpp:16:7: note: candidate: 'constexpr Square::Square(Square&&)'
main.cpp:16:7: note: candidate expects 1 argument, 0 provided
main.cpp:45:58: error: 'Print' was not declared in this scope; did you mean 'printf'?
45 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ^~~~~
| printf
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.
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 { public: Const(int value) : value_(value) {} friend Const operator>>(Const const& c, Square const&) { std::cout << c.value_ << std::endl; return c; } private: int value_; }; class Square { public: friend Const operator>>(Const const& c, Square const&) { return Const(c.value_ * c.value_); } }; class Inc { public: friend Const operator>>(Const const& c, Inc const&) { return Const(c.value_ + 1); } }; class Print { public: friend std::ostream& operator<<(std::ostream& os, Print const&) { os << std::endl; return os; } }; int main() { Const(5) >> Square() >> Inc() >> Inc() >> Print(); return 0; } |
1 2 3 4 | 5 25 26 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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} friend Const operator>>(Const const& c, Square const&) { std::cout << c.value_ << std::endl; return c; } private: int value_; }; class Square { public: friend Const operator>>(Const const& c, Square const&) { return Const(c.value_ * c.value_); } }; class Inc { public: friend Const operator>>(Const const& c, Inc const&) { return Const(c.value_ + 1); } }; class Print { public: friend std::ostream& operator<<(std::ostream& os, Print const&) { os << std::endl; return os; } }; |
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 | #include <iostream> class Const { public: Const(int value) : value_(value) {} friend Const operator>>(Const const& c, Square const&) { std::cout << c.value_ << std::endl; return c; } private: int value_; }; class Square { public: friend Const operator>>(Const const& c, Square const&) { return Const(c.value_ * c.value_); } }; class Inc { public: friend Const operator>>(Const const& c, Inc const&) { return Const(c.value_ + 1); } }; class Print { public: friend std::ostream& operator<<(std::ostream& os, Print const&) { os << std::endl; return os; } }; int main() { Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print(); return 0; } |
main.cpp:8:45: error: 'Square' has not been declared
8 | friend Const operator>>(Const const& c, Square const&) {
| ^~~~~~
main.cpp: In function 'Const operator>>(const Const&, const Square&)':
main.cpp:20:24: error: 'int Const::value_' is private within this context
20 | return Const(c.value_ * c.value_);
| ^~~~~~
main.cpp:14:9: note: declared private here
14 | int value_;
| ^~~~~~
main.cpp: In function 'Const operator>>(const Const&, const Inc&)':
main.cpp:27:24: error: 'int Const::value_' is private within this context
27 | return Const(c.value_ + 1);
| ^~~~~~
main.cpp:14:9: note: declared private here
14 | int value_;
| ^~~~~~
main.cpp: In function 'int main()':
main.cpp:43:55: error: no match for 'operator>>' (operand types are 'Const' and 'Print')
43 | Const(2) >> Square() >> Inc() >> Square() >> Square() >> Print();
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^~ ~~~~~~~
| | |
| Const Print
main.cpp:8:18: note: candidate: 'Const operator>>(const Const&, const int&)'
8 | friend Const operator>>(Const const& c, Square const&) {
| ^~~~~~~~
main.cpp:8:45: note: no known conversion for argument 2 from 'Print' to 'const int&'
8 | friend Const operator>>(Const const& c, Square const&) {
| ^~~~~~~~~~~~~
main.sh: line 2: ./a.out: No such file or directory
Testing if the previous output contains the string 625
: False