生活随笔
收集整理的這篇文章主要介紹了
面向对象程序设计实践(C++)——二维向量
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
概述
設(shè)計(jì)一個類,實(shí)現(xiàn)對二維向量的存儲及實(shí)現(xiàn)。其類中存儲了向量的坐標(biāo),以及一些常見的操作。
實(shí)現(xiàn)
#pragma once
#include <bits/stdc++.h>using namespace std
;class Vec2D
{double x_
;double y_
;
public:Vec2D();Vec2D(double, double);~Vec2D();std
::string
toString();Vec2D
add(const Vec2D
& vec
);Vec2D
add(double num
);Vec2D
sub(const Vec2D
& vec
);Vec2D
sub(double num
);double dot(const Vec2D
& vec
);Vec2D
mul(double num
);Vec2D
neg();Vec2D
& inc();Vec2D
& dec();double len();double getX();double getY();int comp(Vec2D
& vec
);bool operator<(Vec2D
& vec
);Vec2D
operator-();Vec2D
& operator++();Vec2D
& operator--();friend ostream
& operator<<(ostream
& os
, Vec2D vec
);friend istream
& operator>>(istream
& is
, Vec2D
& vec
);operator double();
};
#include "Vec2D.h"Vec2D
::Vec2D()
{x_
= 0.0;y_
= 0.0;
}Vec2D
::Vec2D(double x
, double y
)
{x_
= x
;y_
= y
;
}Vec2D
::~Vec2D()
{}std
::string Vec2D
::toString()
{return std
::string('(' + std
::to_string(x_
) + ',' + std
::to_string(y_
) + ')');
}Vec2D Vec2D
::add(const Vec2D
& vec
)
{return Vec2D(x_
+ vec
.x_
, y_
+ vec
.y_
);
}Vec2D Vec2D
::add(double num
)
{return Vec2D(x_
+ num
, y_
+ num
);
}Vec2D Vec2D
::sub(const Vec2D
& vec
)
{return Vec2D(x_
- vec
.x_
, y_
- vec
.y_
);
}Vec2D Vec2D
::sub(double num
)
{return Vec2D(x_
- num
, y_
- num
);
}double Vec2D
::dot(const Vec2D
& vec
)
{return x_
* vec
.x_
+ y_
* vec
.y_
;
}Vec2D Vec2D
::mul(double num
)
{return Vec2D(x_
* num
, y_
* num
);
}Vec2D Vec2D
::neg()
{return Vec2D(-x_
, -y_
);
}Vec2D
& Vec2D
::inc()
{x_
++;y_
++;return *this;
}Vec2D
& Vec2D
::dec()
{x_
--;y_
--;return *this;
}double Vec2D
::len()
{return sqrt(x_
* x_
+ y_
* y_
);
}double Vec2D
::getX()
{return x_
;
}double Vec2D
::getY()
{return y_
;
}int Vec2D
::comp(Vec2D
& vec
)
{double l1
= this->len();double l2
= vec
.len();if (abs(l1
- l2
) < 1e-10)return 0;elsereturn l1
> l2
? 1 : -1;return 0;
}bool Vec2D
::operator<(Vec2D
& vec
)
{return this->comp(vec
) < 0;
}Vec2D Vec2D
::operator-()
{return Vec2D(-x_
, -y_
);
}Vec2D
& Vec2D
::operator++()
{x_
+= 1;y_
+= 1;return *this;
}Vec2D
& Vec2D
::operator--()
{x_
-= 1;y_
-= 1;return *this;
}Vec2D
::operator double()
{return len();
}ostream
& operator<<(ostream
& os
, Vec2D vec
)
{os
<< vec
.toString();return os
;
}istream
& operator>>(istream
& is
, Vec2D
& vec
)
{is
>> vec
.x_
>> vec
.y_
;return is
;
}
測試
#include <bits/stdc++.h>
#include "Vec2D.h"using namespace std
;int main()
{Vec2D v1
{ 3,4 }, v2
{ 6,8 }, v3
{};cout
<< "v1=" << v1
.toString() << endl
;cout
<< "v2=" << v2
<< endl
;cout
<< "v1.len=" << v1
.len() << endl
;cout
<< "v2.len=" << v2
.len() << endl
;cout
<< "v1+v2=" << v1
.add(v2
).toString() << endl
;cout
<< "v1-v2=" << v1
.sub(v2
).toString() << endl
;cout
<< "v1·v2=" << v1
.dot(v2
) << endl
;cout
<< "v1+1=" << v1
.add(1).toString() << endl
;cout
<< "v2-2=" << v2
.sub(2).toString() << endl
;cout
<< "v1 vs v2=" << v1
.comp(v2
) << endl
;cout
<< "v1.neg=" << v1
.neg().toString() << endl
;cout
<< "-v1=" << (-v1
).toString() << endl
;cout
<< "v1*3=" << v1
.mul(3).toString() << endl
;cout
<< "v1.inc=" << v1
.inc().toString() << endl
;cout
<< "++v1=" << (++v1
).toString() << endl
;cout
<< "v2.dec=" << v2
.dec().toString() << endl
;cout
<< "--v2=" << (--v2
).toString() << endl
;if (v1
< v2
)cout
<< "v1<v2" << endl
;elsecout
<< "v1>=v2" << endl
;cout
<< "Input (x,y): ";cin
>> v3
;cout
<< v3
<< endl
;double lv1
=v1
;cout
<< "double v1=" << lv1
;return 0;
}
v1
=(3.000000,4.000000
)
v2
=(6.000000,8.000000
)
v1.len
=5
v2.len
=10
v1+v2
=(9.000000,12.000000
)
v1-v2
=(-3.000000,-4.000000
)
v1·v2
=50
v1+1
=(4.000000,5.000000
)
v2-2
=(4.000000,6.000000
)
v1 vs v2
=-1
v1.neg
=(-3.000000,-4.000000
)
-v1
=(-3.000000,-4.000000
)
v1*3
=(9.000000,12.000000
)
v1.inc
=(4.000000,5.000000
)
++v1
=(5.000000,6.000000
)
v2.dec
=(5.000000,7.000000
)
--v2
=(4.000000,6.000000
)
v1
>v2
Input
(x,y
): 1 2
(1.000000,2.000000
)
double v1
=7.81025
總結(jié)
以上是生活随笔為你收集整理的面向对象程序设计实践(C++)——二维向量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。