Classes and Objects in C++: Differences Explained

  • Context: C/C++ 
  • Thread starter Thread starter torquerotates
  • Start date Start date
  • Tags Tags
    Classes
Click For Summary

Discussion Overview

The discussion centers on the differences between classes and objects in C++, exploring their definitions, relationships, and examples. It includes conceptual clarifications and technical explanations related to object-oriented programming in C++.

Discussion Character

  • Conceptual clarification
  • Technical explanation

Main Points Raised

  • Some participants assert that an object is instantiated from a class, which is defined once in a program.
  • Others describe a class as a template or category, while an object is a specific instance of that class.
  • One participant compares the relationship to variable types in programming, stating that a class is akin to a type and an object is a variable of that type.
  • Another analogy presented is that a class is like a cookie cutter and an object is like a cookie made from that cutter.
  • One participant provides an example of a class definition and instantiation of objects in C++, illustrating the concept with code.
  • A participant mentions a tutorial related to PHP to further explain the instantiation of objects from classes, suggesting it may help clarify the role of objects.

Areas of Agreement / Disagreement

Participants generally agree on the distinction between classes and objects, but the discussion includes various analogies and explanations that reflect different perspectives on the relationship between the two concepts.

Contextual Notes

Some explanations rely on specific programming examples and analogies, which may not capture all nuances of object-oriented programming in C++. There is also a reference to PHP, which may introduce additional context not directly related to C++.

torquerotates
Messages
207
Reaction score
0
In C++ what is the difference between an object and a class? I'm confused, is an object always a class?
 
Technology news on Phys.org
An object is what is instantiated from a class.
A class is defined once in a program. From this class, an object can be created (instantiated) from the class, according to the parameters specified. The object so created is an instance of the class. Hence it is possible to instantiate multiple object from the same class.

Here's an example:
// following is the class definition
class Cars
{
char *name;
public:
double price;
Cars(char *n, double p){name=n;price=p;}
};
int main(int argc, char *argv[])
{
// following are pointers to objects, or instances of the class Cars
Cars *Lambourghini=new Cars("Lambourghini", 300000.0);
Cars *Civic=new Cars("Civic", 35000.0);
return 0;
}
 
An object is a specific thing. A class is a kind (or type or category) of thing.

Another way of looking at it is to say that a class is a template or "cookie cutter" for creating things (objects) that have the same set of properties.
 
It's the same as if you say "int i". "int" is the type and i is a variable of that type.

If I define a class "myClass" and then say "myClass x", "myClass" is the class and "x", a variable of that type, is the object.
 
Object is every person like "you" "me" and "HallsofIvy"...
Class is a definition say: Physicsforumer
 
If you are familiar with PHP, this tutorial explains how objects are instantiated from a class; it gives a good feel about what role objects play. The tutorial is light-hearted.

http://devzone.zend.com/node/view/id/638
 
Last edited by a moderator:
A class is a cookie cutter. An object is a cookie.
 

Similar threads

  • · Replies 35 ·
2
Replies
35
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
6K
Replies
16
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K