Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
PointClassOpLoad
//g++ 7.4.0 //implement a Point class with default constructor,copy constructor,a negate function to transform the data into negative value,a norm function that returns point's distance from origin, and overload equality operator,assignment operator,addition operator, stream operators,subscript operator and cross-product operator; //credits for default constructor,copy constructor,negate function and norm function codes go to John R Hubbard,University of Richmond; //codes for overloading equality operator,assignment operator,addition operator, stream operators,subscript operator,cross-product operator and test driver are created by Rezaul Hoque on September 13,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 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);} 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; } 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"; o=m+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; return 0; }
run
|
edit
|
history
|
help
0
const example
qsort
remove dublicates from string using recursion
no copy elision
pointer array of functions
Aplicatie-Proiect
Making pyramid using nested loop 2/2
vector destruction - gcc
fb_series
cache 内存消耗