#include #include #include #include #include #include #include struct Variable_Error : public std::runtime_error { using std::runtime_error::runtime_error; }; // Implement Variable here int main() { Variable v1 { 3.0 }; Variable v2 { 5.0 }; { std::ostringstream oss { }; (v1 + v2).print(oss); assert( oss.str() == "8" ); } // ensure that v1.size() throws a Variable_Error exception try { v1.size(); assert(false); } catch (Variable_Error& error) { } catch (...) { assert(false); } { std::ostringstream oss { }; (v2 - v1).print(oss); assert( oss.str() == "2" ); } v2 = "my text"; assert( v2.size() == 7 ); // ensure that variables of different types throws a Variable_Error exception try { (v1 + v2); assert(false); } catch (Variable_Error& error) { } catch (...) { assert(false); } // operator- should not work for anyting but numbers try { (v2 - v2); assert(false); } catch (Variable_Error& error) { } catch (...) { assert(false); } Variable v3 { { v1, v2 } }; assert( v3.size() == 2 ); { std::ostringstream oss { }; v3.print(oss); assert( oss.str() == "[ 3 \"my text\" ]" ); } v1 = { v1, v2, v3 }; assert( v1.size() == 3 ); { std::ostringstream oss { }; v1.print(oss); assert( oss.str() == "[ 3 \"my text\" [ 3 \"my text\" ] ]" ); } }