Your definition of Class is quite good, but you have not described an Object well.
In your example, a SWITCH is a class - a definition or blueprint of what properties/attributes a switch has and what methods/functions it has, i.e what it can do. In this case it represents ALL types of switches, or at least the common behaviour of all switches, so we can call it a BASE class.
You then go on to explain different types of switches - SPST, SPDT etc. But these are not yet objects. They are still definitions or blueprints of more specialised types of switches, so they are still CLASSES. Because they all INHERIT the same behaviour of the base switch, and include all their common behaviour and properties, we call them a SUBCLASS. So what you are actually explaining is INHERITANCE, which leads onto POLYMORPHISM.
An OBJECT is an actual implementation of a class blueprint (not a TYPE of a class).
As we are talking about blueprints, let's look at an analogy of a house.
We can all write a definition of what a HOUSE is, and create a BLUEPRINT for one, having a door, some windows etc.
But It's just a class definition, not a real house.
If you go to a new housing estate, the showroom will show you looks of plans of differnt types or designed of houses: a Belmont, a Bellview, a Warwick etc. Again these are just class definitions. they still have a door, some windows etc, but they may look a bit different, or have a different number of bedrooms.
But you don't want to buy the class (blueprint), you want to buy your own real house. This is the implementation of one of those house designs. i.e. not just A Belmont type, but a particular one, with an address, and your choice of bathroom suite and kitchen untis etc. All the Belmonts will have the same features, but only yours will be unique to you.
So back to your switches, an SPST is a particular type of switch, but you could have several of them performing specific functions. So they would be implemented as OBJECTS/instances. i.e SPST1, SPST2, SPST3 etc.
In C/C++
Code: Select all
class CSwitch
{
/* public, protected and private
variables and functions */
};
class CSPST : public CSwitch
{
/* public, protected and private
variables and functions */
};
int main(...)
{
CSPST MySPST1 = new CSPST(); /* Create an Object of SPST */
CSPST MySPST2 = new CSPST(); /* Create an Object of SPST */
/* Do something with your switches */
delete MySPST1; /* Delete your instance/object. It no longer exists! */
delete MySPST2; /* Delete your instance/object. It no longer exists! */
}
So a CLASS is a definition. The definition always exists.
An OBJECT is an implementation of that class and has a LIFETIME. i.e. it only exists within a particular scope within your program.
Hope that helps.
*(House names are fictitious and any resemblance to actual house styles is purely coincidental!)