Thread: Please check my C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Sounds like an interesting challenge.

    Not quite (IMO). You would put the car id and the driver id in the contract, not the car and the driver themselves.

    Technically, you could have Car &car, so you hold a reference to a car - but I would just store the id itself, and look it up when/if I need it.

    --
    Mats
    Well even that, i think i still need the two classes... But as you say, remove the Car & Driver from contract class and replace them with car registration & person's ID No.... But the use the driver class for calling up the lender when needs be....

    Looking forward to this!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Well even that, i think i still need the two classes... But as you say, remove the Car & Driver from contract class and replace them with car registration & person's ID No.... But the use the driver class for calling up the lender when needs be....

    Looking forward to this!
    Yes, you obviously need a driver class, and a contract class - my only objection was the actual content of the contract class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    My new classes

    Code:
    // Class CONTRACT: Handles the rental contract details
    
    #include <string>
    #include "Date.h"
    
    using namespace std;
    
    class Contract
    {
    public:
    	Contract();
    	void setContractDetails(Date i, Date r, unsigned carR, 
    		unsigned lndrID, string contID);
    	string getContractID();
    	Date getIssueDate();
    	Date getReturnDate();
    	unsigned getLenderID();
    	unsigned getCarReg();
    
    private:
    	string contractID;
    	Date issued, returned;
    	unsigned lenderID;
    	unsigned carRegistration;
    };
    Code:
    // Class ADDRESS : Handles the physical address of the lender
    
    #include <string>
    
    using namespace std;
    
    class Address						
    {
    public:
    	Address();
    
    	Address(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    	void setAddress(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    	string getFirstLine();
    	string getSecondLine();
    	string getSurbub();
    	string getCity();
    	short getCode();
    private:
    	string firstLine;
    	string secondLine;
    	string surbub;
    	string city;
    	short code;
    };
    Code:
    // Class DRIVER : Handles the driver's or lender's details
    
    #include <string>
    #include "Address.h"
    
    using namespace std;
    
    class Driver						
    {
    public:
    	Driver();
    
    	void SetDriverDetails(string fname, string lname, unsigned IDno, 
    		string contact, string license);
    	string getFName();
    	string getSName();
    	Address getAddress();
    	string getContact();
    	unsigned getIDNo();
    	string getLicense();
    
    private:
    	string firstName;
    	string lastName;
    	unsigned idNo;
    	string license;
    	Address address;
    	string contact;
    };
    Code:
    // Class DATE : Handles the fleet return and issue dates
    
    #ifndef DATE
    #define DATE 1
    #endif
    
    class Date						
    {
    public:
    	Date();
    	~Date();
    	Date(int d,int m,int y,int h,int mn);
    	void setTimeAndDate(int d,int m,int y,int h,int mn);
    	int getDay();
    	int getMonth();
    	int getYear();
    	int getHour();
    	int getMinute();
    private:
    	int day;
    	int month;
    	int year;
    	int hour;
    	int minute;
    };
    I think that's about enough classes i need for this program ...
    Last edited by csonx_p; 07-16-2008 at 02:01 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Looks ok, but I would not store the postal code of an address as "short" - it prevents your code from being portable to other countries. In the UK for example, the postal code is X(X)9(9) 9XX, where X is "any letter", and 9 is any digit, the () being optional. So that would require a string. Canada uses a similar patter, but slightly jumbled up.

    In Sweden and the US, the postal code is a 5-digit number that may reach 99999 [and in the US, there's a sub-zip code of another 4 digits, so the zip-code for a particular location may be 99999-9999, which means that you probably want a string to store that too].

    A second note on address would be that you may want a country code in there too - as in car rental it isn't entirely unusual to rent a car in a different country than the country of residence.

    On a more coding side of things:
    Code:
    	Address(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    	void setAddress(string firstLine, string secondLine, string surbub, 
    		string city, short code);
    ...
    	void SetDriverDetails(string fname, string lname, unsigned IDno, 
    		string contact, string license);
    All of those strings should be const reference values.

    I expect you need to be able to "set" the address of a driver as well?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    All of those strings should be const reference values.
    Mats
    You mean each parameter should have a leading const keyword?

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post

    I expect you need to be able to "set" the address of a driver as well?
    Isn't the reason i have setAddress()?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Isn't the reason i have setAddress()?
    Code:
    class Driver						
    {
    public:
    	Driver();
    
    	void SetDriverDetails(string fname, string lname, unsigned IDno, 
    		string contact, string license);
    	string getFName();
    	string getSName();
    	Address getAddress();
    	string getContact();
    	unsigned getIDNo();
    	string getLicense();
    ...
    Where is setAddress? I guess you just forgot it, but I didn't know you'd thought of it and forgot it, rather than just missed it out because of a "thinko".

    If you declare a variable const without initializing it, how does the compiler allow you to initialize it later, or is this an exception with the setters?
    The const reference , in for example the SetDriverDetails parameter list, would just tell the compiler "I just need the string of first name to read from, I'm not changing it, I promise", which means that the compiler doesn't have to COPY the string before passing it to the SetDriverDetails function. It also makes it easier to see that firstname ISN'T being changed by SetDriverDetails when viewing an overview of the code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Where is setAddress?
    Hangon, it is a member function of Address class. If Driver has an object of address, why can't i just use that setAddress from Address class?

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post

    I expect you need to be able to "set" the address of a driver as well?
    Code:
    void Driver::SetAddress(Address add)
    {
    	address = add;
    }
    just added....

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Code:
    void Driver::SetAddress(Address add)
    {
    	address = add;
    }
    just added....
    I suppose that SHOULD be Driver::setAddress, rather than Driver::SetAddress? Just to be consistant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    I suppose that SHOULD be Driver::setAddress, rather than Driver::SetAddress? Just to be consistant.

    --
    Mats
    BTW i now have two SetAddress both with captal letter S... They are member functions to address and driver respectively...

    here's the other one..

    Code:
    void Address::SetAddress(const string& fline, const string& sline, const string& sbb, 
    				const string& cty, const string& cde)
    {
    	firstLine = fline;
    	secondLine = sline;
    	surbub = sbb;
    	city = cty;
    	code = cde;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. how to check input is decimal or not?
    By kalamram in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 07:07 PM
  3. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  4. A way to check for Win98 or WinXP
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 10-31-2002, 11:06 AM
  5. how to check for end of line in a text file
    By anooj123 in forum C++ Programming
    Replies: 6
    Last Post: 10-24-2002, 11:21 PM