Docsity

Vector Class Implementation with Overloaded Operators and Functions, Exercises of Object Oriented Programming

Toggle sidebar
bg1
using namespace std;
class vector
{
private:
float magnitude;
float angle;
public:
vector operator+(vector& a);//it is a prototype for overloadin
function of +
vector operator-(vector& a);//it is a prototype for overloadin
function of -
float operator*(vector& a);//it is a prototype for overloadin
function of *
vector operator%(vector& a);//it is a prototype for overloadin
function of %
void operator=(vector& y);//it is a prototype for overloadin
function of =
vector(float mag,float point)//constructor with arguments
{
magnitude=mag;
angle=point;
}
vector():magnitude(0.0),angle(0.0) {};//constructor without
arguments
void set_angle(float g)//function for setting value of angle
{
angle=g;
}
void set_magnitude(float g)//function for setting value of
magnitude
{
magnitude=g;
}
float get_magnitude()//function for getting value of angle
{
return magnitude;
}
float get_angle()//function for getting value of magnitude
{
return angle;
}
};
vector vector::operator+(vector& a)//+ is overloaded in this function
{
float b,c,d,e;
b=pow( magnitude*cos(angle*(3.14159F/180)) +
(a.magnitude)*cos(a.angle*(3.14159F/180)) ,2) + pow(
magnitude*sin(angle*(3.14159F/180)) +
(a.magnitude)*sin(a.angle*(3.14159F/180)) ,2);
b=sqrt(b);
d=( magnitude*sin(angle*(3.14159F/180)) +
(a.magnitude)*sin(a.angle*(3.14159F/180)) );
e=(magnitude*cos(angle*(3.14159F/180)) +
(a.magnitude)*cos(a.angle*(3.14159F/180)) );
docsity.com
pf2

Partial preview of the text

Download Vector Class Implementation with Overloaded Operators and Functions and more Exercises Object Oriented Programming in PDF only on Docsity!

using namespace std; class vector { private: float magnitude; float angle; public: vector operator+(vector& a);//it is a prototype for overloadin function of + vector operator-(vector& a);//it is a prototype for overloadin function of - float operator(vector& a);//it is a prototype for overloadin function of * vector operator%(vector& a);//it is a prototype for overloadin function of % void operator=(vector& y);//it is a prototype for overloadin function of = vector(float mag,float point)//constructor with arguments { magnitude=mag; angle=point; } vector():magnitude(0.0),angle(0.0) {};//constructor without arguments void set_angle(float g)//function for setting value of angle { angle=g; } void set_magnitude(float g)//function for setting value of magnitude { magnitude=g; } float get_magnitude()//function for getting value of angle { return magnitude; } float get_angle()//function for getting value of magnitude { return angle; } }; vector vector::operator+(vector& a)//+ is overloaded in this function { float b,c,d,e; b=pow( magnitudecos(angle(3.14159F/180)) + (a.magnitude)cos(a.angle(3.14159F/180)) ,2) + pow( magnitudesin(angle(3.14159F/180)) + (a.magnitude)sin(a.angle(3.14159F/180)) ,2); b=sqrt(b); d=( magnitudesin(angle(3.14159F/180)) + (a.magnitude)sin(a.angle(3.14159F/180)) ); e=(magnitudecos(angle(3.14159F/180)) + (a.magnitude)cos(a.angle*(3.14159F/180)) );

docsity.com

c=( atan2( d,e ) )*(180/3.14159265); return vector(b,c); } vector vector::operator-(vector& a)//- is overloaded in this function {

float b,c,d,e; b=pow( magnitudecos(angle(3.14159F/180)) - (a.magnitude)cos(a.angle(3.14159F/180)) ,2) + pow( magnitudesin(angle(3.14159F/180)) - (a.magnitude)sin(a.angle(3.14159F/180)) ,2); b=sqrt(b); d=( magnitudesin(angle(3.14159F/180)) - (a.magnitude)sin(a.angle(3.14159F/180)) ); e=(magnitudecos(angle(3.14159F/180)) - (a.magnitude)cos(a.angle(3.14159F/180)) ); c=( atan2( d,e ) )*(180/3.14159265); return vector(b,c);

} float vector::operator(vector& a)// is overloaded in this function { float b,c,d; b=magnitudecos(angle(3.14159F/180)) * (a.magnitude)cos(a.angle(3.14159F/180)); d=( magnitudesin(angle(3.14159F/180)) ) * ( (a.magnitude)sin(a.angle(3.14159F/180)) ); c=b+d; return c; } vector vector::operator%(vector& a)//% is overloaded in this function { float b,c,d; b=sqrt ( pow( magnitudecos(angle (3.14159F/180) ), 2) + pow( magnitudesin(angle(3.14159F/180)), 2) ); d=sqrt ( pow( a.magnitudecos(a.angle (3.14159F/180) ), 2) + pow( a.magnitudesin(a.angle(3.14159F/180)), 2) ); b=bd; d=bsin( (a.angle-angle)*(3.141592654/180) ); c=6000;//here 6000 ia an arbitrary angle as it is in 3-D return vector(d,c); } void vector::operator=(vector& y)//= is overloaded in this function { magnitude=y.magnitude; angle=y.angle; } void operator<<(ostream& out,vector& a)//<< is overloaded in this function { out<<a.get_magnitude()<<"<"; out<<a.get_angle()<<endl;; }

docsity.com