I need help

This is a discussion on I need help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <cmath> using namespace std; class point{ private: double x; double y; public: // constructors point(){ x=0; ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    4

    I need help

    Code:
     #include <iostream>
    #include <cmath>
    using namespace std;
    
    class point{
    	private:
    		double x;
    		double y;
    	
    	public:
    		// constructors	
    		point(){
    			x=0;
    			y=0;
    			return;
    			}
    			
    		point( double v){
    			x=v;
    			y=v;
    			return;
    			}
    			
    		point( double xv, double yv){
    			x=xv;
    			y=yv;
    			}
    	
    		// accessors
    		double getx(){
    			return x;
    			}
    			
    		double gety(){
    			return y;
    			}
    			
    		// modifiers
    		double setx( double v ){
    			x =v;
    			return v;
    			}
    			
    		double sety( double v){
    			y = v;
    			return v;
    			}
    		
    		// operations
    		double fromorigin(){
    			return sqrt(x*x + y*y);
    			}
    			
    		double distance( point p){
    			return  sqrt( (p.getx() - x) * (p.getx() -x) 
    					+(p.gety() - y) * (p.gety() -y) );
    			}
    		
    	};
    	
    int main(){
    	point p1(5), p2(3,4);
    	cout << p1.getx() << p1.gety()<< endl;
    	p1.setx(2);
    	p1.sety(4);
    	cout << p1.getx() << p1.gety()<<endl;
    	
    	cout << p2.fromorigin() << endl;
    	cout << p1.distance(p2) << endl;
    }
    I dont now how to add in the following
    1)Operators < and > to relate points to their distance from (0,0).
    2)Operators == and != to compare points
    3)Operators + and - to modify points ( x = x1 +x2, y=y1+y2)
    4)Another variable like angle mode that will cause the point to print in rectangular( x,y) or polar (r<theta) with its accessor and modifier.
    5)Modification to the ostream to allow the point to print in polar form.
    6)Modification to istream to allow a point to be input in polar form.

    I would love to have someone help me and I thank everyone in advance for their help

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Can you be more specific about what problems you're having?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    1. Get rid of the return statements in your constructors.

    2. Prefer to initialize your values in the constructors using initialization lists:
    Code:
    point(double v) : x(v), y(v)
    {
    }
    3. Any functions/methods that do not modify the values within the class should be const:
    Code:
    // accessors
    double getx() const {
        return x;
    }
    			
    double gety() const {
        return y;
    }
    
    ...
    
    // operations
    double fromorigin() const {
        return sqrt(x*x + y*y);
    }
    			
    double distance( point p) const {
        return  sqrt( (p.getx() - x) * (p.getx() -x) 
                     +(p.gety() - y) * (p.gety() -y) );
    }
    4.
    Quote Originally Posted by co8check
    2)Operators == and != to compare points
    Let's take a stab at one of these (it's really quite easy). Maybe this can give you a clue as to how to begin an attempt at the other.
    Code:
    class point
    {
    ...
    public:
    ...
        bool operator==(const point& rhs)
        {
            return x == rhs.x && y == rhs.y;
        }
    };
    5. The distance and fromorigin methods are basically the same (distance from a point either given or assumed to be (0,0)). These can be combined using a default argument to give a single distance method that will either provide the distance to a given point or, lacking that given point, the origin.
    Code:
    class point{
    ...
    public:
    ...
        double distance(const point& p = point(0,0)) const
        {
            return  sqrt( (p.getx() - x) * (p.getx() -x)  +(p.gety() - y) * (p.gety() -y) );
        }
    };
    You can now call the single distance method as follows:
    Code:
    cout << "P1's distance from the origin is: " << p1.distance() << endl;
    cout << "P1's distance from point P2 is: " << p1.distance(p2) << endl;
    6.
    Quote Originally Posted by co8check
    5)Modification to the ostream to allow the point to print in polar form.
    Here is an example of overloading the ostream operator<< for a custom class
    Code:
    class foo
    {
        ...
    public:
        std::ostream& print(std::ostream& os) const
        {
            return os << /* Your specific print stuff here */;
        }
    };
    
    std::ostream& operator<<( std::ostream & os, const foo& rhs)
    {
        return rhs.print(os);
    }
    Try to work some on the others and post your attempts and specific questions.

    [edit]
    BTW, from the Forum Guidlines:
    2. Use descriptive subject lines. Do not put URGENT!, or NEED HELP NOW, etc. in your title; it will not make people look at it any faster. Doing this makes many old time helpers on this board not look at the post at all.
    [/edit]
    Last edited by hk_mp5kpdw; 03-26-2009 at 11:50 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21