[C++ Advanced] Use overloaded binary operators to improve the plane vector class Vec2D

[C++ Advanced] Use overloaded binary operators to improve the plane vector class Vec2D

Previous review

In [C++ Advanced] Following the TDD principle and implementing the plane vector class (Vec2D) , we have initially implemented the Vec2D content, and now we have made certain improvements:

Realize half of Vec2D binary arithmetic operator overload
1, +-(two Vec2D object operations and one Vec2D object and a double number operation)
2, * (dot multiplication and number multiplication)
At the same time, use the function that involves copying the object to pass The way of reference is modified.
Note that if there is an object copy, you need to pass by reference, if it's just a simple value copy, you don't need to pass by reference.

Knowledge points

Let's take a look at how we generally call binary operators:
note that the last one is a "wrong" wording, because this is not a member function wording, and z is not an object of the Vec2D class.
But common sense tells us that this way of writing and d should be the same.

Vec2D a{ 1 , 2 }, b{ 3 , 6 }; double z { 1.3 }; Vec2D c = a + b; //a.operator+(b); Vec2D Vec2D::operator+(Vec2D); Vec2D d = a + z; //a.operator+(z); Vec2D Vec2D::operator+(double); Vec2D e = z + b; //z.operator+(b); Vec2D double::operator+(Vec2D); Wrong! Copy code

Here are the prototypes of these overloaded functions. Note that the last one is a friend function. If you don t remember the friend function, please review: blog.csdn.net/qq_42604176

struct Vec2D { Vec2D operator +(Vec2D); //Member function Vec2D operator +( double ); //Member function friend Vec2D operator +( double , Vec2D); //Non-member (friend) function }; Vec2D operator +( double , Vec2D) { //Do something } Copy code

Modified part

1. First improve the h file

# pragma once # include <iostream> # include <string> # include <cmath> # include <exception> class Vec2D { private : double x_; double y_; public : Vec2D (); Vec2D ( double , double ); ~ Vec2D (); //Convert a vector to a string representation std::string toString () ; //Vector addition Vec2D add ( const Vec2D& secondVec2D) ; // Add to a numeric value Vec2D add ( double num) ; //Overloaded addition function Vec2D operator +( const Vec2D& secondVec2D); Vec2D operator +( const double num); //read or modify vector elements double & at ( const int index) ; //vector subtraction Vec2D subtract ( const Vec2D& secondVec2D) ; //Subtract from the value Vec2D subtract ( const double num) ; //Overloaded Vec2D operator of subtraction function -( const Vec2D& secondVec2D); Vec2D operator -( const double num); //vector dot product double dot ( const Vec2D& secondVec2D) ; //vector number multiplication Vec2D multiply ( const double multiplier) ; //Overload of vector multiplication double operator *( const Vec2D& secondVec2D); Vec2D operator *( const double multiplier); //Add a multiplication friend function friend Vec2D operator *( const double multiplier,Vec2D vec2d); //vector negative value Vec2D negative () ; //vector self-increase 1 Vec2D& increase () ; //vector self-decrement 1 Vec2D& decrease () ; //find the norm (length) of the vector double magnitude () ; //Find the angle between Vec2D and x+axis double direction () ; //Compare the length of the two vectors. If firstVec2D is less than secondVec2D, return -1, if greater than, return 1, if equal, return 0 int compareTo (Vec2D secondVec2D) ; }; Copy code

2. Add the specific details of the function in the cpp file

# include "Vec2D.h" Vec2D:: Vec2D () { x_ = 0.0 ; y_ = 0.0 ; } Vec2D:: Vec2D ( double x, double y) { x_ = x; y_ = y; } Vec2D::~ Vec2D () { } //Convert the vector to a string representation std::string Vec2D::toString () { //TODO: add implementation code here. return std:: string ( "(" + std:: to_string (x_) + ", " + std:: to_string (y_) + ")" ); } //Vector addition Vec2D Vec2D::add ( const Vec2D& secondVec2D) { //TODO: add implementation code here. return Vec2D (x_ + secondVec2D.x_, y_ + secondVec2D.y_); } // Add to the value Vec2D Vec2D::add ( double num) { //TODO: add the implementation code here. return Vec2D ( this ->x_ + num, this ->y_ + num); } //Addition overload function Vec2D Vec2D:: operator +( const Vec2D& secondVec2D) { return this -> add (secondVec2D); } Vec2D Vec2D:: operator +( const double num) { return this -> add (num); } //Vector subtraction Vec2D Vec2D::subtract ( const Vec2D& secondVec2D) { //TODO: add implementation code here. return Vec2D (x_-secondVec2D.x_, y_-secondVec2D.y_); } //Vector subtraction Vec2D Vec2D::subtract ( const double num) { //TODO: add implementation code here. return Vec2D ( this ->x_-num, this ->y_-num); } //Subtraction overload function Vec2D Vec2D:: operator -( const Vec2D& secondVec2D) { return this -> subtract (secondVec2D); } Vec2D Vec2D:: operator -( const double num) { return this -> subtract (num); } //Vector dot product double Vec2D::dot ( const Vec2D& secondVec2D) { //TODO: add implementation code here. return (x_ * secondVec2D.x_ + y_ * secondVec2D.y_); } //Vector number multiplication Vec2D Vec2D::multiply ( const double multiplier) { //TODO: add implementation code here. return Vec2D (x_ * multiplier ,y_ * multiplier); } //Multiplication overload function Vec2D Vec2D:: operator *( const double num) { return this -> multiply (num); } double Vec2D:: operator *( const Vec2D& secondVec2D) { return this -> dot (secondVec2D); } //Multiplication friend function, not a member function of Vec2D Vec2D operator *( const double multiplier,Vec2D vec2d) { return vec2d. multiply (multiplier); } //Negative vector value Vec2D Vec2D::negative () { //TODO: add implementation code here. return Vec2D (-x_, -y_); } //Vector increment 1 Vec2D& Vec2D::increase () { x_++; y_++; //TODO: add implementation code here. return (* this ); } //vector decrement 1 Vec2D& Vec2D::decrease () { x_--; y_--; //TODO: add implementation code here. return (* this ); } //Find the norm (length) of the vector double Vec2D::magnitude () { //TODO: add implementation code here. return sqrt (x_ * x_ + y_ * y_); } //Find the angle between Vec2D and x+ axis double Vec2D::direction () { //TODO: add implementation code here. return atan (y_/x_); } //Compare the length of two vectors. If firstVec2D is less than secondVec2D, return -1, if greater than, return 1, and if equal, return 0 int Vec2D::compareTo (Vec2D secondVec2D) { //TODO: add implementation code here. double m1 = this -> magnitude () ; double m2 = secondVec2D. magnitude (); if ( abs (m1-m2) < 1e-10 ) return 0 ; else return (m1> m2? 1 : -1 ); } //read or modify vector elements double & Vec2D::at ( const int index) { if ( 0 == index) return x_; else if ( 1 == index) return y_; //use exception handling method, throw An exception occurs , and the carrying information is printed else throw std:: out_of_range ( "at() only accept 1 or 2 as parameter" ); //TODO: add implementation code here. //TODO: insert return statement here } Copy code

3. Run the test function

# include <iostream> # include "Vec2D.h" using std::cout; using std::endl; int main () { //Create vector object Vec2D v1{ 3 , 5 }, v2{ 4 , 6 }; //The vector is converted to a string cout << "v1 = " << v1. toString () << endl; cout << "v2 = " << v2. toString () << endl; //Vector addition: vector + vector, vector + number //Vec2D v3 = v1.add(v2); //Vec2D v4 = v3.add(10.0); //Use overloaded operator function Vec2D v3 = v1 + v2; Vec2D v4 = v3 + 10.0 ; cout << "v3 = " << v3. toString () << endl; cout << "v4 = " << v4. toString () << endl; //Vector subtraction, vector dot product, vector number multiplication //Vec2D v5 = v2.subtract(v1); //double v6 = v2.dot(v1);//The dot product of two vectors is a number //Vec2D v7 = v3.multiply(2.1); //Use overloaded operator function Vec2D v5 = v2-v1; double v6 = v2 * v1; //The dot product of two vectors is a number Vec2D v7 = v3 * 2.1 ; cout << "v2-v1 = " << v5. toString () << endl; cout << "v2 * v1 = " << v6 << endl; cout << "v3 * 2.1 = " << v7. toString () << endl; cout << "2.1 * v3 = " << ( 2.1 * v3). toString () << endl; //Friend function is used here //vector negation Vec2D v8 = v2. negative (); cout << "-v2 = " << v8. toString () << endl; //vector increment/decrement cout << " ++v8 = " << v8. increase (). toString () << endl ; cout << "--v2 =" << v2. decrease (). toString () << endl; //Read or modify vector elements cout << "v1.x_ = " << v1. at ( 0 ) < <endl; cout << "v1.y_ = " << v1. at ( 1 ) << endl; //The length of the vector magnitude and angle direction cout << "v1.magnitude =" << v1. magnitude () << endl; cout << "v1.direction = " << v1. direction () << endl; //Compare two vectors cout << "v1 compare v2 :" << v1. compareTo (v2) << endl; return 0 ; } Copy code

4. Get the test result