directory

  • 1. Specify the methods and member functions of the class to be implemented
  • 2. Assuming you have written Vec2D, write the test code as required
  • 3. Write and test the plane vector class Vec2D
  • 4. Complete code
  • 5. Final results

1. Specify the methods and member functions of the class to be implemented

For efficiency reasons, we usually set the parameter of the function to a reference type. An important purpose of setting function parameter types to reference types is to avoid object copying overhead.

2. Assuming you have written Vec2D, write the test code as required

Follow the principles of TDD:

This section shows the following: Assume that the plane vector class Vec2D has been written to test the various functions in Vec2D in the main function.

Write the test code in the source file:

include <iostream>

using std::cout;
using std::endl;

int main(a)
{
	// Create a vector object
	Vec2D v1{ 3.5 }, v2{ 4.6 };

	// Vector to 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);

	cout << "v2 = " << v3.toString() << endl;
	cout << "v3 = " << v4.toString() << endl;

	// Subtraction, dot product, vector times
	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);

	cout << "v2 - v1 = " << v5.toString() << endl;
	cout << "v2 * v1 = " << v6 << endl;
	cout << "V3 * 2.1 =" << v7.toString() << endl;
	// Take the negative of the vector
	Vec2D v8 = v2.negative(a); cout <<"-v2 = " << v8.toString() << endl;
	// The vector increases/decreases
	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 magnitude of the vector and the 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 the code

Since the Vec2D class has not yet been written, the test program will obviously fail.

3. Write and test the plane vector class Vec2D

1. Add classes

To add a class, use the Class Wizard in VS2019: View -> Class View -> Select project, right-click -> Class Wizard -> Add Class



Add a class member variable:

Return to the Class wizard -> Member Variables -> Add Customization

Add x_,y_, type double, private.



3, add method, note here the same name overload function is not allowed to add, need to manually add later



Here’s an example:

4. Supplement functions that need to be overloaded and functions that are missing

We also subtract an overloaded function from add.

//cpp
// Add to the value
Vec2D Vec2D::add(double num)
{
	// TODO:Add the implementation code here.
	return Vec2D(a); }//h
	// Add to the value
	Vec2D add(double num);
Copy the code

Add missing header files

#include <iostream>
#include <string>
#include <cmath>
#include <exception>
Copy the code

5. Refine the details of each member function and modify the format slightly

4. Complete code

Vec2D.h

#pragma once
#include <iostream>
#include <string>
#include <cmath>
#include <exception>
class Vec2D
{
private:
	double x_;
	double y_;
public:
	Vec2D(a);Vec2D(double , double );
	~Vec2D(a);// Convert the vector to a string representation
	std::string toString(a);
	// Vector addition
	Vec2D add(Vec2D secondVec2D);
	// Add to the value
	Vec2D add(double num);
	// Read or modify vector elements
	double& at(const int index);
	// Vector subtraction
	Vec2D subtract(Vec2D secondVec2D);
	// Subtract from the value
	Vec2D subtract(double num);
	// vector dot product
	double dot(Vec2D secondVec2D);
	// Vector times
	Vec2D multiply(double multiplier);
	// Take the negative of the vector
	Vec2D negative(a);
	// The vector increases by 1
	Vec2D& increase(a);
	// The vector subtracts by 1
	Vec2D& decrease(a);
	// Find the vector norm (length)
	double magnitude(a);
	Vec2D: Vec2D: Vec2D: Vec2D: Vec2D: Vec2D: Vec2D: Vec2D
	double direction(a);
	// Compare the lengths of two vectors. FirstVec2D is -1 if it is less than secondVec2D, 1 if it is greater, and 0 if it is equal
	int compareTo(Vec2D secondVec2D);
};
Copy the code

Vec2D.cpp

#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(a)
{
	// TODO:Add the implementation code here.
	return std::string("(" + std::to_string(x_) + ","+ std::to_string(y_) + ")");
}


// Vector addition
Vec2D Vec2D::add(Vec2D secondVec2D)
{
	// TODO:Add the 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);
}

// Vector subtraction
Vec2D Vec2D::subtract(Vec2D secondVec2D)
{
	// TODO:Add the implementation code here.
	return Vec2D(x_ - secondVec2D.x_ , y_ - secondVec2D.y_);
}
// Vector subtraction
Vec2D Vec2D::subtract(double num)
{
	// TODO:Add the implementation code here.
	return Vec2D(this->x_ - num , this->y_ - num);
}


// vector dot product
double Vec2D::dot(Vec2D secondVec2D)
{
	// TODO:Add the implementation code here.
	return (x_ * secondVec2D.x_ + y_ * secondVec2D.y_);
}


// Vector times
Vec2D Vec2D::multiply(double multiplier)
{
	// TODO:Add the implementation code here.
	return Vec2D(x_ * multiplier ,y_ * multiplier);
}


// Take the negative of the vector
Vec2D Vec2D::negative(a)
{
	// TODO:Add the implementation code here.
	return Vec2D( -x_ , -y_);
}


// The vector increases by 1
Vec2D& Vec2D::increase(a)
{
	x_++;
	y_++;
	// TODO:Add the implementation code here.
	return (*this);
}


// The vector subtracts by 1
Vec2D& Vec2D::decrease(a)
{
	x_--;
	y_--;
	// TODO:Add the implementation code here.
	return (*this);
}


// Find the vector norm (length)
double Vec2D::magnitude(a)
{
	// TODO:Add the implementation code here.
	return sqrt(x_ * x_ + y_ * y_);
}


Vec2D: Vec2D: Vec2D: Vec2D: Vec2D: Vec2D: Vec2D: Vec2D
double Vec2D::direction(a)
{
	// TODO:Add the implementation code here.
	return atan(y_ / x_);
}


// Compare the lengths of two vectors. FirstVec2D is -1 if it is less than secondVec2D, 1 if it is greater, and 0 if it is equal
int Vec2D::compareTo(Vec2D secondVec2D)
{
	// TODO:Add the implementation code here.
	double m1 = this->magnitude(a);double m2 = secondVec2D.magnitude(a);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 to throw an exception and print the carrying information
	else
		throw std::out_of_range("at() only accept 1 or 2 as parameter");
	// TODO:Add the implementation code here.
	// TODO:Insert the return statement here
}
Copy the code

main.cpp

#include <iostream>
#include "Vec2D.h"
using std::cout;
using std::endl;

int main(a)
{
	// Create a vector object
	Vec2D v1{ 3.5 }, v2{ 4.6 };

	// Vector to 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);

	cout << "v2 = " << v3.toString() << endl;
	cout << "v3 = " << v4.toString() << endl;

	// Subtraction, dot product, vector times
	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);

	cout << "v2 - v1 = " << v5.toString() << endl;
	cout << "v2 * v1 = " << v6 << endl;
	cout << "V3 * 2.1 =" << v7.toString() << endl;
	// Take the negative of the vector
	Vec2D v8 = v2.negative(a); cout <<"-v2 = " << v8.toString() << endl;
	// The vector increases/decreases
	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 magnitude of the vector and the 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 the code

5. Final results

V1 = (3.000000, 5.000000) v2 = (4.000000, 6.000000) v2 = (7.000000, 11.000000) V3 = (17.000000, V2-v1 = (1.000000, 1.000000) v2 * v1 = 42 V3 * 2.1 = (14.700000, 23.100000) -v2 = (-4.000000, 6.000000) + + 8 = (3.000000, 5.000000) – v2 = (3.000000, V1. X_ = 1 v1. Y_ = 1 v1. Magnitude = 1 v1. Direction = 1 v1