Introduction to OOP

Thinking in OOP

Moving into OOP is what changes programming from dechipherable code into real, understandable thought that is easy to understand. You have to keep in mind that the computer thinks in specifics. In fact so specific that the only thing the computer truely understands (if understanding is proper terminology) is "electrical signal on." Perhaps not even "off" as that is simply the absence of an "on," but that's a matter of philosophy.

Layering

You may wonder then, how, from this one simple concept can the computer generate programs as smart as a spell checker and worlds as complex as Quake III. The explanation is layering. Could you understand how a program could use a sequence of electrical signals to create numbers in binary, a base 2 system? Not too hard (assuming you know binary ;) ). So now the computer is up to the level of counting. From counting you could teach the computer to add a number by seven by telling the computer how to combine the singals (binary add), and then to multiply -- skip a few steps and you have a math wizard. If you've ever taken a physics class you know pretty much anything can be accurately modeled, or at least approximated, through some sort of equation.

When you have a base command set, you make new commands on those, and so on. The fallback from this is that after every "level," you lose a little efficently in the process (ever wonder why Windows is so much slower?). The library your compiler provides is mid-way or so in the scheme of things. Just one C++ line, cout << "Hello World!"; executes hundreds of instructions, perhaps more, by the time it reaches a level of the simplest instructions the CPU understands. And then you make your functions could use cout statement many times... Now you can see where those billions and trillions of calculations a second go!

Containership

Now you may wonder how this applies to programming in OOP. Well look at what's on your desk. A computer. Think about what parts make up the computer. You might list a keyboard, CPU case, monitor, mouse, printer, and such, but in essence to the computer you would be wrong. There is much more to it, so let's go deeper. Look at the keyboard. What parts make up the keyboard. A lot. Starting to sound a bit like The Matrix? But if you notice there are really a few TYPES of keys. Since all of the letter, number, function, cursor keys, and a few others are the same size, as far a programming is concerned these could be the same object, with a char type variable holding what that key does, and the whole set of keys are in an array, which is an element of the keyboard struct, which is an element of the computer struct. This method of looking at OOP is called containership.

Inheritance

Another method of looking at OOP, which is just as valid, is inheritance. And instead of looking from the big to the small, you look from the generic to the specific. Try to start as generic as common sense dictates. Let's use "vechicle" as an example. What do all vechicles have in common? They all have tires and engines right? Wrong. Helicopters are vechicles and don't have tires and glider planes don't have engines. The comptuer takes everything 100% literally, so you have to be as specific as possible when planning your programming project. All vechicles can move -- that's what makes them vechicles. Now in our "real world," this matters worth squat since all matter can move but for a computer an object that can move can do something, and anything that happens needs to be coded. To continue, think up more specific groups, like cars, which share tires, engines, and more, and even farther to say four-door and two-door, then to specific models like a Nissan Sentra, which has all of the previous features in common but has unique characteristics such as X horsepower and X size fuel tank.

The point of doing this is for breaking down many complex objects and tasks into simpler subgroups which can be programmed by themselves and then used in all objects following that one. For example you could program an engine then anything that has an engine in it will use that code when they run their engine.

 

/ 0 نظر / 12 بازدید