What is the difference between an object and a class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scarecrow13
    New Member
    • Oct 2007
    • 3

    What is the difference between an object and a class?

    How are objects and classes alike? I take it that objects are instances of a class and classes are data types which the user has defined. Am I correct?
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Originally posted by scarecrow13
    I take it that objects are instances of a class and classes are data types which the user has defined. Am I correct?
    Without getting into various pedantry, yes, your characterizatio n of classes and objections is correct.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Classes an objects are not alike.

      A class (or struct) is a declaration. It provides information to the compiler on how to a) create and object and b) how to use the object.

      The object is the definition. The object technology folks like to use instance of a class but in C++ they are just variables. The compiler uses the class declaration to determine what data members are in the object and then follows specific rules to initialize those members.

      Yes, all objects have a type which is the class (or struct) name.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Changed thread title to better describe the problem (did you know that threads whose titles do not follow the Posting Guidelines actually get FEWER responses?).

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by scarecrow13
          How are objects and classes alike? I take it that objects are instances of a class and classes are data types which the user has defined. Am I correct?
          Yes, a class is a type defined by the user and an object is an instantiation of that
          type of class. Methods defined in that class are just 'implementation details' that
          make that class/type usefull to work with.

          kind regards,

          Jos

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by weaknessforcats
            Classes an objects are not alike.
            That's an 'implementation detail'. e.g. in Java classes are represented by Class
            objects. So an instantiation of the Class class represents a class.

            kind regards,

            Jos

            Comment

            Working...