Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
ConversionOperator
//g++ 7.4.0 //conversion operator: converting a Point class object into a Vector class object //this code is created by Rezaul Hoque on September 15,2021 //note: codes shared by Rezaul Hoque on rextester are not for sale; they are created and shared to facilitate the algorithm learning process; many like Hoque use this platform to practice programming ;Rezaul hopes his contribution helps others to fine tune their learning; #include <iostream> #include <cmath> class Vector { friend std::ostream& operator<<(std::ostream &, const Vector &);//stream extraction operator friend std::istream& operator>>(std::istream &, Vector &);//stream insertion operator friend Vector operator+(const Vector &, const Vector&);//binary addition operator friend Vector operator-(const Vector &, const Vector&);//binary subtraction operator public: Vector (int ,double );//default constructor Vector(double,double,double); Vector (const Vector &);//copy constructor ~Vector();//destructor const Vector & operator=(const Vector &);//assignment operator double& operator[](int) const;//subscript operator Vector norm(); int size; double a,b,c; double * data; }; std::ostream& operator<<(std::ostream& ostr,const Vector& v) { int l; ostr<<"( "; for(l=0;l<v.size-1;l++){ ostr<<v[l]<<","; if((l+1)%8 == 0) std::cout<<"\n"; } return ostr<<v[l]<<")\n"; } std::istream& operator>>(std::istream& istr,const Vector& v) { for(int l=0;l<=v.size-1;l++) { std::cout<<l<<":"; istr>>v[l]; } return istr; } Vector::Vector(int sz=1,double t=0.0): size(sz) { data = new double[size]; for(int l=0;l<size;l++) data[l]=t; } Vector::Vector(double t=0.0,double u=0.0,double v=0.0) { a=t; b=u; c=v; } Vector:: Vector(const Vector & v): size(v.size) { data = new double[v.size]; for(int l=0;l<v.size;l++) data[l]=v.data[l]; } Vector::~Vector() { delete [] data; data = NULL; size=0; } const Vector& Vector::operator=(const Vector & v) { if(&v != this) { delete [] data; size = v.size; data = new double[size]; for(int l=0;l<size;l++) data[l]=v.data[l]; } return *this; } double& Vector:: operator[](int I) const { return data[I]; } Vector operator+(const Vector & v,const Vector& w) { Vector z(4,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] + w.data[l];} return z; } Vector operator-(const Vector & v,const Vector& w) { Vector z(4,0.0); for(int l=0;l<z.size;l++){ z.data[l] = v.data[l] - w.data[l];} return z; } Vector Vector:: norm() { Vector z(4); for(int l=0; l<size; l++) z.data[l] +=sqrt(data[l]*data[l]); return z; } class Point{ friend std::ostream& operator<<(std::ostream&,const Point&); friend std::istream& operator>>(std::istream&,Point&); friend int operator==(const Point&,const Point&); friend Point operator+(const Point&,const Point&); friend Point operator&(Point&, Point&); public: double x,y,z; Point(double a=0,double b=0,double c=0): x(a),y(b),z(c) {} Point(const Point& p): x(p.x),y(p.y),z(p.z){} Point& operator=(const Point&); void negate(){x *= -1;y *= -1;z *= -1;} double norm(){ return sqrt(x*x+y*y+z*z);} operator Vector();//conversion operator double & operator[](int); }; std::ostream& operator<<(std::ostream & ost,const Point & r) { std::cout<<"("; ost<<r.x<<","<<r.y<<","<<r.z; std::cout<<")"; return ost; } std::istream& operator>>(std::istream & ist,Point & r) { std::cout<<"x:\n"; ist>>r.x; std::cout<<"y:\n"; ist>>r.y; std::cout<<"z:\n"; ist>>r.z; return ist; } int operator==(const Point& p,const Point& q) { if(p.x != q.x && p.y != q.y && p.z!=q.z) return 0; else return 1; } Point& Point::operator=(const Point& p) { x=p.x; y=p.y; z=p.z; return *this; } Point operator+(const Point& v,const Point& w){ Point k; k.x= v.x+w.x; k.y=v.y+w.y; k.z=v.z+w.z; return k; } double & Point::operator[](int l) { if (l==1) return x; if (l==2) return y; if (l==3) return z; std::cerr<<"Error: index out of range\n"; exit(0); } Point operator&(Point& v, Point & w) { Point z( v.y*w.z-v.z*w.y,v.z*w.x-v.x*w.z,v.x*w.y-w.x*v.y); return z; } Point::operator Vector() { Vector g(3); g.a=x; g.b=y; g.b=z; return g; } int main() { Point m,n,o,l; std::cin>>m>>n; std::cout<<"m: "<<m<<"\nn: "<<n; l=n; std::cout<<"\nl is assigned value of n.\nl: "<<l<<" \n"; std::cout<<"o=m+n\n"; std::cout<<"o: "<<o; Point s; s=m&n; std::cout<<"\nCross product of m and n:\ns: "<<s; std::cout<<"\ns.negate():\n"; s.negate(); std::cout<<s; std::cout<<"\ns.norm():\n"; double b; b=s.norm(); std::cout<<b; Point k; Vector t = Vector(k); std::cout<<"\nPoint k: "<<k; std::cout<<"\nVector t: "<<t; return 0; }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Sort an array of 0s, 1s and 2s
Rubix
Templet
merge without extra space Gap method ALgorithm
Power of an element
ADVENTURE CODE CSCI40
Get all anagrams from given words
Height of a binary tree
Template
MinCostKStops_DFS
Please log in to post a comment.