Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- class Vector{
- private:
- // 資料(變數部分)
- double _x; // 加底線 避免名稱上衝突
- double _y;
- public:
- // 方法;功能(函式部分)
- friend void printVector(Vector v);
- Vector(){
- _x = 0;
- _y = 0;
- }
- Vector(double x, double y){
- _x = x;
- _y = y;
- }
- double length(){
- return pow( _x*_x + _y*_y, 0.5 );
- }
- Vector& setX(double x){
- _x = x;
- return *this;
- }
- Vector& setY(double y){
- _y = y;
- return *this;
- }
- double getX(){
- return _x;
- }
- double getY(){
- return _y;
- }
- void print(){
- cout << "(" << _x << "," << _y << ")" << endl;
- }
- Vector operator+(const Vector& v2){
- double newX = _x + v2._x;
- double newY = _y + v2._y;
- Vector v3(newX, newY);
- return v3;
- }
- };
- int main(){
- Vector v1(3, 4);
- Vector v2(5, 4);
- v1.setX(5).setY(6);
- Vector v3 = v1 + v2; //v1.operator+(v2);
- v1.print();
- v2.print();
- v3.print();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement