Thread: Question about concept of class

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    50

    Red face Question about concept of class

    I spent quite a lot of time to try to understand class but still not clear about the concept. Help me please.
    1. I saw something like this:
    Code:
    bool class::mem_f(int x) 
    {
         if (!(x%5))// means if x is not divisable by 5
         return true;
    }
    Can the condition be written as : if (x%5!=0) ?If the answer is 'no', why?
    2. When & why overload operators?
    3. I am not sure about when to use call by reference in arguments of member functions.
    When to use call by reference in class? Why use call by reference when overloading operators?Plz explain in simple English.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) No. x%5 is equivalent to x%5 != 0, therefore !(x%5) is equivalent to x%5 == 0. (The coment above the if gets it the wrong way round. As Mario says below me, doing the explicit one is probably clearer.)

    2) You overload operators when doing so makes using your class more intuitive. It takes experience to know when that is.

    3) You use call-by-ref-to-non-const whenever you wish to modify the argument.
    You use call-by-ref-to-const whenever passing the argument by value, i.e. copying it, would be expensive, or when the object cannot be copied.

    None of these questions had anything to do with the concept of a class.
    Last edited by CornedBee; 11-22-2006 at 08:53 AM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > Can the condition be written as : if (x%5!=0) ?If the answer is 'no', why?

    EDIT: I was wrong on this one. Check CornedBee's reply

    > When & why overload operators?

    I think the short answer fot this is never unless there is a reason.
    Check http://www.parashift.com/c++-faq-lit...erloading.html

    > 3. I am not sure about when to use call by reference in arguments of member functions.

    Of the top of my head, on three instances:

    - When the argument type doesn't allow for copy operations (a pass by value forces the creation of a new variable and the copying of the argument value into the new variable)

    - When you want to avoid the cost of creating a new variable (particularly meaningful when working with large objects)

    - When you wish to alter the object from inside the function and retain the new value when you exit the function.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    50
    Thank you for your detailed answers. At least I am more clear about when to use call by reference in class. It takes some time for me to digest. Thank you.

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    a class is the shape of a cookie cutter, and the little cookies you eat look the way they do because the cookie cutter class made them.

    simple concept.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    a class is the shape of a cookie cutter, and the little cookies you eat look the way they do because the cookie cutter class made them.
    
    simple concept.
    It would be totally fun to be a teacher, and explain all the concepts of object oriented programming with that cookie analogy. Ya know, sprinkles -- polymorphism, it's a simple concept. It would also be really fun to lock a baby in a basement and have it only hear beeps. It would totally learn how to express itself in beeps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 06:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM