Thread: Please check my C++

  1. #121
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Read & Write

    Alright... I have the read/write functions used in Car class, to read/write from file/screen/keyboard. Now, i also added similar functions inside Contract since i'm gonna read a different file (contract), and display differently too. This i suppose suggests that i should overload '>>' / '<<' for these extra reads/writes. If so, could mean this should happen for any extra read/write i add for whatever class...

    Q. My thoughts are to give them same names (i.e read(istream& i) )... My concern is whether the compiler will be confused especially if all have same parameter list, but appearing in different classes!!!

  2. #122
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Alright... I have the read/write functions used in Car class, to read/write from file/screen/keyboard. Now, i also added similar functions inside Contract since i'm gonna read a different file (contract), and display differently too. This i suppose suggests that i should overload '>>' / '<<' for these extra reads/writes. If so, could mean this should happen for any extra read/write i add for whatever class...

    Q. My thoughts are to give them same names (i.e read(istream& i) )... My concern is whether the compiler will be confused especially if all have same parameter list, but appearing in different classes!!!
    Nope, no problem. It will know from the class itself which of the read() functions you mean.

    --
    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. #123
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by matsp View Post
    Technically


    My gr8 apologies to you Elysia... wow, quite interesting though... are u guys a spouses? i mean your brains are almost similar...

    k, enough with jokes!

  4. #124
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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....
    Did I? I can't remember suggesting something like that.
    Though I am guilty as charged with not keeping consistent naming conventions.
    Sometimes I do MyFunction, sometimes I do my_function and sometimes I do MyVar and sometimes I do myVar and sometimes I do my_var.
    Oh well.
    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.

  5. #125
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    My gr8 apologies to you Elysia... wow, quite interesting though... are u guys a spouses? i mean your brains are almost similar...
    Jokes aside, I can certainly say that Elysia is not my spouse (to my knowledge, we have never met)...

    --
    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. #126
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Did I? I can't remember suggesting something like that.
    Though I am guilty as charged with not keeping consistent naming conventions.
    Sometimes I do MyFunction, sometimes I do my_function and sometimes I do MyVar and sometimes I do myVar and sometimes I do my_var.
    Oh well.
    http://cboard.cprogramming.com/showthread.php?t=103264

    No actually, it was Mats... Confuses the two of you...

  7. #127
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    Reference VS Pointer

    Quote Originally Posted by matsp View Post
    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
    Am confused ... If i pass a reference to a variable, then am allowing the parameter receiving the address to change it... So, in this case, why do i pass a reference as a const?

  8. #128
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because you don't want the original to be changed in this case.
    [It would be a programming error and const allows you to catch that mistake at compile time.]
    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. #129
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    My question will be explained by code...

    Code:
    Class A {
    
    public :
          int getX();
     
    private :
          int x;
    };
    
    A::getX()
    {
          return x;
    }
    
    Class B {
    
    public :
          int getValues();
    
    private :
          A data;
    };
    
    B::getValues()
    {
        data.x; // This doesn't work????? pressing the dot after x doesn't even show x, or getX()
       
    }
    how do i access x in B?

  10. #130
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    Because you don't want the original to be changed in this case.
    [It would be a programming error and const allows you to catch that mistake at compile time.]
    Maybe i should rephrase my question, what difference would it make if i leave the '&' out, but still use 'const'?

  11. #131
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by csonx_p View Post
    Maybe i should rephrase my question, what difference would it make if i leave the '&' out, but still use 'const'?
    IF you don't pass by reference, the entire string will be duplicated.
    If you pass by reference, the object will not be duplicated. More efficient.
    There's no need for const if you don't pass by reference because we use it to protect unwanted changes to the original object.

    Quote Originally Posted by csonx_p View Post
    My question will be explained by code...

    Code:
    Class A {
    
    public :
          int getX();
     
    private :
          int x;
    };
    
    A::getX()
    {
          return x;
    }
    
    Class B {
    
    public :
          int getValues();
    
    private :
          A data;
    };
    
    B::getValues()
    {
        data.x; // This doesn't work????? pressing the dot after x doesn't even show x, or getX()
       
    }
    how do i access x in B?
    You can't. The design is simply flawed.
    If B is stored in A, then you must use a public interface.
    In other words, you may "own" a car, but you still can't access its internals. You must use its public interface (it, pedestals and wheel) to drive it.
    This is different from a is-a relationship where a cat is an animal and should therefore be able to access the animal's basic properties.
    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. #132
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by csonx_p View Post
    Maybe i should rephrase my question, what difference would it make if i leave the '&' out, but still use 'const'?
    instead of copying just the address (4-8 bytes) you will copy the whole string calling the copy-constructor

    And when the function will be ended - the destructor will be called to clear the temp var created...

    A lot of overhead involved
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #133
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by Elysia View Post
    IF you don't pass by reference, the entire string will be duplicated.
    If you pass by reference, the object will not be duplicated. More efficient.
    There's no need for const if you don't pass by reference because we use it to protect unwanted changes to the original object.


    You can't. The design is simply flawed.
    If B is stored in A, then you must use a public interface.
    In other words, you may "own" a car, but you still can't access its internals. You must use its public interface (it, pedestals and wheel) to drive it.
    This is different from a is-a relationship where a cat is an animal and should therefore be able to access the animal's basic properties.
    Could you give an example on how to access x in this case?

  14. #134
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    data.getX();
    Your public interface.
    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.

  15. #135
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by csonx_p View Post
    Could you give an example on how to access x in this case?
    Since getX() is public, you should be able to use data.getX() to get it's value. You can't use x since it's a private member of A.

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