Wow!, that post RepoMan made is great!.

Originally Posted by
HRose
The only thing I currently know about OOP: instead of just using predefined variables and functions, you can organize the predefined variables and functions together in a more complex object, and then use the emergent object while hiding the complexity within. The language simply gives syntax to support these custom data-types.
And then inheritance and polymorphism allow mostly to reuse the code and spare the time by making clones of objects slightly modified and adapted to a different task.
You are still thinking in procedural coding, while objects exist in a superior level of abstraction.
Doing this:
Code:
//[1]
for(t=0;t<maxWeapons;t++){
weaponShot(t);
}
is completely different than doing this:
Code:
//[2]
foreach(weapons){
this.Shot();
}
//[2] (alternate syntax)
weapons.each().Shot();
[2] is thinking in bigger building blocks. And there are less possible errors. In [1] maxWeapons need to be defined, can be wrong, t may iterate outside the valid range, or 1 less than the needed range. Procedure weaponShot can be defined everywhere, but method Shot is defined on the class Weapon.
In [1] your mind is busy with the problems of iterating T, limits for the for, and other LOW LEVEL problems. These managerial problems just don't exist in [2], your mind can focus on HIGHER LEVEL problems.
Is less keystrokes, and is easier to understand... but these are just good side effects, and not important. Are important, but not why you do this.
Since Shot() is a method defined in the class Weapon, It may need a lot of different data, that can get directly from the object instance. The Shot method may work already just by the implicit "this". This is important, because in procedural programming, wen you need more variables, you usually have to pass these variables as parameters of the function.
Code:
function weaponShot(index t){
/* implementation here */
}
become:
Code:
function weaponShot(index t){
weaponShotMultiple(t,1);
}
function weaponShotMultiple(index t, int numberBullets){
/* implementation here */
}
In OOP you may not need to create new method. Just take the number of bullets from this.numBullets. It also make so you can fill numBullets in the constructor. So the constructor create a weapon that shot 1 bullet or M bullets. The constructor is a really sexy place to define these things. You don't have to change any code after that. A weapon that used to shot 1 bullet, now shot 2000 bullets. No code changes needed. The procedural version is inferior, need a lot more changes, these changes can introduce errors. It was tested and debugged code, but is now alpha again.
Old code:
Code:
Weapon *current = new Weapon();
New code:
Code:
Weapon *shotgun = new Weapon(100);//100 pellets
Weapon *pistol = new Weapon(1);//1 bullet
Weapon *current = shotgun;
You can even move /changes/ further, and load this numberOfBullets parameter from a database or a file. Having the number of bullets of a weapon in weaponstats.ini is much better than in weaponstats.cpp or weaponstats.hpp
Horrible code:
Code:
function playerShot(){
switch(playerWeaponType){
case "pistol":
if(ammo>1){
weaponShot(1);
ammo--;
}
break;
case "shotgun":
if(ammo>40){
weaponShotMultiple(2,40);
ammo -= 40;
}
break;
}
}
Cool code:
Code:
player->currentWeapon->Shot();