# Having trouble distinguishing between a "Class" and a "Object"

## Recommended Posts

O.k....so in my ever ending studies I am now trying to further my knowledge of true Object-Orented design and low and behold I can't even distinguish a "Class" from a "Object". So for example lets use oh I don't know a Bank to try and give examples. So... A Class of a bank could be : Vault And a Object of a Vault could be : Currency Does this sound correct?

##### Share on other sites
dave    2187
A class is the definition, an object is the instance of a class, for example:

MyClass is a new type here:
class MyClass{};

An object is the instance of a class:
MyClass myObjectOfTypeMyClass;

Using your code it might look like this:
class Bank{};

Bank bankOfScotland;Bank bankOfEngland;

bankOfScotland and bankoEngland are objects that you have created that are of type Bank.

Hope that clarifies.

Dave

##### Share on other sites
GamerYZ    169
A class is a data structure, an Object could mean an intance of a class. For example:

class Foo{//some code};// declear an intance of the class Foo//"bar" is an object of the class Foo.Foo bar;

##### Share on other sites
Promit    13246
Imagine a building.

Every building has a set of blueprints and schematics. They have all the details about how to build the building and how things are laid out inside the building. Those blueprints are not the actual buildings, of course. They just describe them.

Classes are blueprints. Objects are buildings.

##### Share on other sites
gharen2    520
A simpler example:

"Husky", "Beagle", and "Doberman" are classes. They represent a general type of dog.

"Rover", "Fido", and "Rex" are objects. The represent a particular individual dog, that belongs to one of the types of dog.

##### Share on other sites
gunning    749
Quote:
 Original post by gharen2A simpler example:"Husky", "Beagle", and "Doberman" are classes. They represent a general type of dog."Rover", "Fido", and "Rex" are objects. The represent a particular individual dog, that belongs to one of the types of dog.

And to that end Dog would be the abstract base class.

##### Share on other sites
JohnBolton    1372
You also need to realize that people commonly use the word "class" or the name of a class when the really mean "an object of the class".

##### Share on other sites
Cowboy Coder    365
Quote:
Original post by skittleo
Quote:
 Original post by gharen2A simpler example:"Husky", "Beagle", and "Doberman" are classes. They represent a general type of dog."Rover", "Fido", and "Rex" are objects. The represent a particular individual dog, that belongs to one of the types of dog.

And to that end Dog would be the abstract base class.

Clearly dog subclasses from "Animal"! (Or "Mammal"?).

Dog can also be an aggregation of various physical and qualitative components, which is better.

##### Share on other sites
Cowboy Coder    365
Quote:
 Original post by JohnBoltonYou also need to realize that people commonly use the word "class" when the really mean "an object of the class".

Yes, I was looking up random numbers in C# today, when I came across this beauty:

Random RandomClass = new Random();

RandomClass is an instance of the Random Class, but it is not a class. "Random" on the other hand, is the class.

##### Share on other sites
Zahlman    1682
Quote:
Original post by Cowboy Coder
Quote:
 Original post by JohnBoltonYou also need to realize that people commonly use the word "class" when the really mean "an object of the class".

Yes, I was looking up random numbers in C# today, when I came across this beauty:

*** Source Snippet Removed ***

RandomClass is an instance of the Random Class, but it is not a class. "Random" on the other hand, is the class.

Wow, what a horrible variable name. x.x

I personally have no problem saying "a Random" as shorthand for "a Random instance" or "an instance of the Random class" (instance here is a synonym for object; the emphasis is shifted away from the thing being an object and towards the class of which it is an object), but I am careful to avoid using the word "class" explicitly to mean "an object of the class", and correct people for this when their usage is particularly confusing.

##### Share on other sites
DevFred    840
Quote:
 Original post by chadsxeI can't even distinguish a "Class" from a "Object".

A class defines a data type: a set of values and operations on them.
An object is simply one of those defined values.

Although not technically correct, it might help you to think of "int" as a class.

set of values: ranging from -2,147,483,648 to 2,147,483,647
operations: add, subtract, multiply, divide, modulo, bitwise and/or/xor...

An object of that class could be 7, or 435, or -13

Quote:
 Original post by chadsxeSo for example lets use oh I don't know a Bank to try and give examples.

You have to ask yourself: what is it that defines a Bank in the context that you're interested in? Dou you want to store information about customers (financial context) or information about when it was built (historical context).. or maybe insurance data (for an insurance agency)?

##### Share on other sites
rip-off    10979
Quote:
 Although not technically correct, it might help you to think of "int" as a class.

The OP didn't specify a language, and in some OO languages (Ruby and Smalltalk are 2 I know of ) integers have classes and integer literals are objects.

##### Share on other sites
DevFred    840
Quote:
Original post by rip-off
Quote:
 Although not technically correct, it might help you to think of "int" as a class.

The OP didn't specify a language, and in some OO languages (Ruby and Smalltalk are 2 I know of ) integers have classes and integer literals are objects.

Right, but beginners apparently insist on learning C++ instead of nice languages, because OMG C++ is 10 times faster or something along those lines... ;)

##### Share on other sites
darren_mfuk    148
Go with Promit's answer [wink]

##### Share on other sites
Sometimes it is besides why sources are so cryptic. Thanks for the info everyone. It makes a lot more sense hearing it from this forum.

Regards

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account