Sign in to follow this  
chadsxe

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by gharen2
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.


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

Share this post


Link to post
Share on other sites
Quote:
Original post by skittleo
Quote:
Original post by gharen2
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.


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 this post


Link to post
Share on other sites
Quote:
Original post by JohnBolton
You 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 this post


Link to post
Share on other sites
Quote:
Original post by Cowboy Coder
Quote:
Original post by JohnBolton
You 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 this post


Link to post
Share on other sites
Quote:
Original post by chadsxe
I 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 chadsxe
So 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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

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

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this