Need help(3D point class)

This is a discussion on Need help(3D point class) within the C++ Programming forums, part of the General Programming Boards category; Hello ,, there is some error with member funtion distance can anyone help me with it. Code: #include<iostream.h> #include<conio.h> #include<math.h> ...

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

    Need help(3D point class)

    Hello ,, there is some error with member funtion distance can anyone help me with it.

    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<math.h>
    class point
    {
    int x;
    int y;
    int z;
    public:
    void setPoint()
    {
    	cin>>x>>y>>z;
    }
    void printPoint()
    {
    	cout<<"("<<x<<","<<y<<","<<z<<")"<<"\n";
    }
    void negatePoint()
    {
    	cout<<"Negate of point is:("<<-x<<","<<-y<<","<<-z<<")\n";
    }
    void distancePoint(float)
    { point dis;
      dis=pow 1(x,2)+ pow(y,2) +pow(z,2);
      dis=sqrt(dis);
    }
    };
    int main()
    {
    point a;
    clrscr();
    a.setPoint();
    a.printPoint();
    a.negatePoint();
    a.distancePoint();
    getch();
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Code:
    include<iostream>
    #include<math.h>
    class point
    {
      int x;
      int y;
      int z;
    public:
      void setPoint()
      {
        std::cin>>x>>y>>z;
      }
      void printPoint()
      {
        std::cout<<"("<<x<<","<<y<<","<<z<<")"<<"\n";
      }
      void negatePoint()
      {
        std::cout<<"Negate of point is:("<<-x<<","<<-y<<","<<-z<<")\n";
      }
    #if 0
      // This peice of code is blocked coz i cant understand what                                                                                                                                                     
      // u want to do in distance point if u want to knw the distance                                                                                                                                                 
      // of two points and return the diff                                                                                                                                                                            
      void distancePoint(const float distance)
      {
        point dis;
        dis.x = pow 1(x,2)+ pow(y,2) +pow(z,2);
        dis.y = sqrt(dis.x);
      }
    #else
      point Distance(const point point_a,
                     const point point_b) {
        point result;
        result.x = point_a.x - point_b.x;
        result.y = point_a.y - point_b.y;
        result.z = point_a.z - point_b.z;
        return result;
      }
    
    #endif
    };
    int main()
    {
      point a;
    #if 0
      clrscr(); // Sorry i m compiling with g++                                                                                                                                                                       
    #endif
      a.setPoint();
      a.printPoint();
      a.negatePoint();
    #if 0
      a.distancePoint(); // Changed the signature u can use however u want :)                                                                                                                                         
    #endif
    #if 0
      getch(); // window's specific                                                                                                                                                                                   
    #endif
      return 0;
    }


    Check this out......
    And i compiled this with g++ 4.3.2 on linux kernel 2.6.2
    so microsoft specific function has been commented and abt distancepoint i dont understand what u want to do with that

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    What error are you having?
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    distance function calculates the distance from the origin, sqrt((x-0)^2,(y-0)^2,(z-0)^2)

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    K then write the code and u will find any problem tell me, sorry i was not aware of that

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    And by the way what is the origin in your sample code ?????

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Quote Originally Posted by Fatima Rizwan View Post
    distance function calculates the distance from the origin, sqrt((x-0)^2,(y-0)^2,(z-0)^2)
    Why u are having the forumla like above that can be written like
    sqrt((x)^2,(y)^2,(z)^2)

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    origin is 0

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<math.h>
    class point
    {
    int x;
    int y;
    int z;
    public:
    void setPoint()
    {
    	cin>>x>>y>>z;
    }
    void printPoint()
    {
    	cout<<"("<<x<<","<<y<<","<<z<<")"<<"\n";
    }
    void negatePoint()
    {
    	cout<<"Negate of point is:("<<-x<<","<<-y<<","<<-z<<")\n";
    }
    void distancePoint()
    { double dis;
    
      dis=sqrt(pow(x,2)+pow(y,2)+pow(z,2));
    
    }
    };
    int main()
    {
    point a;
    clrscr();
    a.setPoint();
    a.printPoint();
    a.negatePoint();
    a.distancePoint();
    cout<<a.distancePoint();    //ILLEGAL STRUCTURE OPERATION
    getch();
    return 0;
    }
    hOW TO PRINT IT??

  10. #10
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    distancepoint returns void.

  11. #11
    The larch
    Join Date
    May 2006
    Posts
    3,573
    To answer the last question, the distance function should return the result.

    Similarly it would more sense for negate to return a point with negated values, and for setPoint not to ask for input but to accept three values through arguments.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Fatima is ur problem solved or not ??? or just stuck in distance caluclation ????

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    112
    ya its solved,,,=))

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 09:17 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 09:44 AM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 03:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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