Please check my C++

This is a discussion on Please check my C++ within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by matsp All of those strings should be const reference values. Mats You mean each parameter should have ...

  1. #106
    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?

  2. #107
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    string firstLine
    Should be
    const string& firstLine

    And the same for the rest.
    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.

  3. #108
    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()?

  4. #109
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    string firstLine
    Should be
    const string& firstLine

    And the same for the rest.
    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?

  5. #110
    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.

  6. #111
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    string firstLine
    Should be
    const string& firstLine

    And the same for the rest.
    Code:
    void Address::setAddress(const string fline, const string sline, const string sbb, 
    				const string city, const string code)
    {
    	firstLine = fline;
    	secondLine = sline;
    	surbub = sbb;
    	city = city;
    	code = code;
    }

  7. #112
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Better, but you should make them references too, e.g.
    Code:
    const string &fline
    . That is what really lets the compiler pass the address instead of a copy of the string.

    --
    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. #113
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,450
    Code:
    void Address::setAddress(const string& fline, const string& sline, const string& sbb, 
    				const string& city, const string& code)
    Don't forget the reference.
    Passing by reference means the original object is passed (typically its address). Otherwise the entire object needs to be duplicated which is slow.
    By appending const you're telling the compiler you won't change it. Because it's a reference, the changes will reflect the original object and that's not good. You can stop that by adding const.
    When passing by value, it hardly matters, because the original variable won't be affected anyway.
    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.

  9. #114
    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?

  10. #115
    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....

  11. #116
    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.

  12. #117
    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;
    }

  13. #118
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    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;
    }
    But your get functions are lower-case, which made me think that ALL functions start with lowercase. Are you using the naming convention of "lowercase initial if it returns something, uppercase initial if it's a void function?" That would be fine, if that's the case. If it's "depending on how you felt like when you wrote that particular function", then you should make it more consistent. It appears that "setDateAndTime" is also lowercase initial - so I suspect most of your functions are just lowercase initial, and randomly, you decide to make some of them uppercase - that's definitely not a good plan.

    --
    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.

  14. #119
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Are you using the naming convention of "lowercase initial if it returns something, uppercase initial if it's a void function?" That would be fine, if that's the case.
    Elysia's guilty as charged... He's the one who suggested this, and YES is the answer....

  15. #120
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Elysia's guilty as charged... He's the one who suggested this, and YES is the answer....
    Techically, that would be "SHE suggested". That's fine, as long as it's consistent.

    --
    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.

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, 12: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, 10: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

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