Thread: Class that defines a location??

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    9

    Class that defines a location??

    I am tring to write a class that defines location based on latitude & longitude. I want to include members for East/West, North/South, and all latitude and longitude. Also a function must be included that will display the class's info and a function to set the info. Can anyone help?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What have you tried?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    9
    I'm working with Visual C++ 2008 Express.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    What s/he meant is, post your current code. What have you done so far?

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    9
    This is what I have so far for the **.h file.

    Code:
    //location.h
    
    class location
    {
    private:
    	int degrees;
    	int minutes;
    	int	seconds;
    
    public:
    	location(int d, int m, int s);
    	~location();
    
    	void setdegrees(int d);
    	void setminutes(int m);
    	void setseconds(int s);
    
    	int getdegrees();
    	int getminutes();
    	int getseconds();
    	void properties();
    };

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    These should be const functions:
    Code:
    int getdegrees() const;
    int getminutes() const;
    int getseconds() const;
    since they don't (or at least shouldn't) modify the objects state.

    It looks OK so far. So which parts are you having trouble with?

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    9
    I'm not sure how where or how to get the East/West, North/South into the code. I would also like to initialize the class to a specific location. Is this something that can be done?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I'm not sure how where or how to get the East/West, North/South into the code.
    Your location class has member functions to set the angle. Therefore, to set a location object to east/west/north/south, you can provide non-member non-friend functions, e.g., define:
    Code:
    void setNorth(location& loc)
    {
        loc.setdegrees(0);
        loc.setminutes(0);
        loc.setseconds(0);
    }
    I would also like to initialize the class to a specific location. Is this something that can be done?
    Yes, by using an initialisation list in the constructor, e.g.,
    Code:
    location::location(int d, int m, int s) : degrees(d), minutes(m), seconds(s) {}
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Consider say
    Code:
    struct angle {
      int degrees;
      int minutes;
      int seconds;
    };
    Then in your class
    Code:
    class location
    {
    private:
    	angle latitude;
    	angle longitude;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2008
    Posts
    9
    Here is what I have for my complete code, so far. If I make my funtions const I get errors.

    Code:
    //location.h
    
    class location
    {
    private:
    	int degrees;
    	int minutes;
    	int	seconds;
    
    public:
    	location(int d, int m, int s);
    	~location();
    
    	void setdegrees(int d);
    	void setminutes(int m);
    	void setseconds(int s);
    
    
    	int getdegrees();
    	int getminutes();
    	int getseconds();
    	void properties();
    };
    
    
    
    //location.cpp
    
    #include<iostream>
    #include"location.h"
    
    using namespace std;
    
    location::location(int d, int m, int s)
    {
    	degrees = d;
    	minutes = m;
    	seconds = s;
    }
    
    location::~location()
    {
    
    }
    
    void location::setdegrees(int d)
    {
    	degrees = d;
    }
    
    void location::setminutes(int m)
    {
    	minutes = m;
    }
    
    void location::setseconds(int s)
    {
    	seconds = s;
    }
    
    int location::getdegrees()
    {
    	return degrees;
    }
    
    int location::getminutes()
    {
    	return minutes;
    }
    
    int location::getseconds()
    {
    	return seconds;
    }
    
    void location::properties()
    {
    	cout<<"  Degrees"<<getdegrees() <<endl;
    	cout<<"  Minutes"<<getminutes() <<endl;
    	cout<<"  Seconds"<<getseconds() <<endl;
    }
    
    
    
    
    //main
    
    #include"location.h"
    #include<iostream>
    
    int main(int argc, char *argv[])
    {
    	location city(51, 30, 0);
    	city.properties();
    	
    	system("PAUSE");
    	return 0;
    }
    This is only giving lat or lon. Not both.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uhh yes, it prints out the coordinates you've stored inside the class. You need to have code to transform/convert if you want both.
    You should only put const on your get* functions. Const is meant to tell the function does not modify the object state (it does not change any member functions), so obviously and set function will generate errors if const.
    I also suggest you take the code inside properties and put it inside main, and then delete the member function because it's not really suited for the class (ie the class shouldn't print information unless it's for debugging purposes).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    You can enhance your constructor in the following way:

    Code:
    location::location(int const& d, int const& m, int const& s) : degrees( d), minutes( m), seconds( s) { }
    Initializing the attributes in the initializer list is faster.

    Code:
    void location::setdegrees(int const& d)
    {
    	degrees = d;
    }
    Passing by reference is faster, passing by const reference is faster and can prevent errors (if you erroneously modify the argument, the compiler will tell you). A good coding practice is to make input arguments 'const&' .

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Passing by reference is faster, passing by const reference is faster and can prevent errors (if you erroneously modify the argument, the compiler will tell you).
    It typically makes no or minimal difference for the built-in types though.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Just a link for my comment on the initializer list:

    http://www.ubookcase.com/book/Addiso...8lev1sec2.html

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 07:57 PM