|
||||||
Programming in Simple Steps: the ClassUnderstanding the Concept of a Class in Program Development
The key to object oriented programming is understanding about classes. This straightforward article explains the concept of a class using everyday examples.
For anyone new to the world of OOP (Object Oriented Programming) it can be little bit of a baptism by fire - not because classes are hard to work with, but because there are so many new terms to deal with. So, rather than jump straight in with data members and functions, inheritance and instantiation, it may be better to start with a simple example - the car. An Example of a Class: the CarMention the phrase 'a xyz is a kind of car' to anyone and they will immediately have a general concept of what an 'xyz' will look like. It will have:
The car will also be able to do particular things:
It's easy to see that the car consists of a number of variables (or data members) and a number of methods (or member functions ), and it's therefore possible to build a stylised definition of the car - in other words the car class: car {
steering_wheel
wheels: 4
no_doors: 5
color: red
transmission: manual
engine_cc: 1000
accelerate {step on gas}
decelerate {stamp on brake}
}
Now that there is a generic concept of a car then this can be used to produce a rather more specific concept - a Golden Eagle Jeep. An Example of Inheritance: the Golden Eagle JeepAt this point a car is still just a concept - an uninstantiated class - but that concept can be built on to produce a new class, the Golden Eagle Jeep: golden_eagle_jeep {
is a type of car
no_doors: 0
color: blue
engine_cc: 2400
engine_type: I4
}
The Golden Eagle Jeep class is based on a car class and so inherits all of the characteristics of a car (steering wheel, 4 wheels and a manual transmission), but then has some characteristics of its own (no doors and a 2.4 litre engine); and, at this point, it is still just a concept - the next stage is to create an object from the class. An Example of an Object: My JeepAbout three decades ago Chrysler took the concept of the Golden Eagle Jeep and actually built one - they instantiated the class to produce an object; and that object is my_jeep: my_jeep {
is a golden_eagle_jeep
engine_cc: 2792
engine_type: V6
}
The object my_jeep has all of the characteristics of the class golden_eagle_jeep and therefore also has all of the characteristics of the class car; however, the object itself can be modified once it has been created (for example having a 2.8 litre V6 engine instead of a 2.4 I4). And so, if programmer were to say "My object is a Golden Eagle Jeep and is a type of car" then any other programmer will know (more or less) what a Golden Eagle Jeep is and how to use it. ConclusionA class:
The copyright of the article Programming in Simple Steps: the Class in Computer Programming is owned by Mark Alexander Bain. Permission to republish Programming in Simple Steps: the Class in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||