#include // The 'Vector_3d.hpp' file contains all the class and // function declarations, as well as the class definition. // // The 'Vector_3d.cpp' file contains all the function // definitions. You can treat all of these as if they // were in this file. // // This example extends the previous Vector_3d project. // The primary focus here is operator overloading. #include "Vector_3d.hpp" // Function declaration (for this file only) int main(); // Function definition (for this file only) int main() { Vector_3d u{ 3.2, -4.5, 2.1 }; Vector_3d v{ 2.5, 7.2, -9.1 }; // The overloaded << operator allows Vector_3d objects // to be inserted directly into an output stream. std::cout << "u = " << u << std::endl; std::cout << "v = " << v << std::endl; // Demonstrate the binary + and - operators. std::cout << std::endl << "Binary + and - operators:" << std::endl; std::cout << "u + v = " << (u + v) << std::endl; std::cout << "u - v = " << (u - v) << std::endl; // Demonstrate both overloads of scalar multiplication // and scalar division. std::cout << std::endl << "Scalar multiplication and division:" << std::endl; std::cout << "3.0*u = " << (3.0*u) << std::endl; std::cout << "u*3.0 = " << (u*3.0) << std::endl; std::cout << "u/2.0 = " << (u/2.0) << std::endl; // Demonstrate the unary + and - operators. // Unary + leaves the vector unchanged, while unary - // returns its additive inverse. std::cout << std::endl << "Unary + and - operators:" << std::endl; std::cout << "+u = " << (+u) << std::endl; std::cout << "-u = " << (-u) << std::endl; // Demonstrate the == and != operators. // std::boolalpha causes Boolean values to be displayed // as 'true' and 'false' rather than 1 and 0. std::cout << std::endl << "Equality and inequality:" << std::endl; std::cout << std::boolalpha; std::cout << "u == v: " << (u == v) << std::endl; std::cout << "u != v: " << (u != v) << std::endl; std::cout << "u == u: " << (u == u) << std::endl; std::cout << "u != u: " << (u != u) << std::endl; // These functions were introduced in the previous // Vector_3d project and are included here for comparison. std::cout << std::endl << "Other vector operations:" << std::endl; std::cout << "inner( u, v ) = " << inner( u, v ) << std::endl; std::cout << "norm( u ) = " << norm( u ) << std::endl; std::cout << "proj( u, v ) = " << proj( u, v ) << std::endl; std::cout << "cross( u, v ) = " << cross( u, v ) << std::endl; std::cout << "to_string( u ) = " << to_string( u ) << std::endl; // Demonstrate normalize(...). Use a copy so that the // original value of u remains unchanged for the examples // that follow. Vector_3d w{ u }; std::cout << std::endl << "Normalizing a copy of u:" << std::endl; std::cout << "w = " << w << std::endl; normalize( w ); std::cout << "w = " << w << std::endl; std::cout << "norm( w ) = " << norm( w ) << std::endl; // Demonstrate the compound assignment operators. // Each of these modifies its left operand. w = u; std::cout << std::endl << "Compound assignment operators:" << std::endl; std::cout << "Initially, w = " << w << std::endl; w += v; std::cout << "After w += v, w = " << w << std::endl; w -= v; std::cout << "After w -= v, w = " << w << std::endl; w *= 2.0; std::cout << "After w *= 2.0, w = " << w << std::endl; w /= 2.0; std::cout << "After w /= 2.0, w = " << w << std::endl; return 0; }