Today I started reading Thinking in C++ Second Edition Volume I by Bruce Eckel.
Here’s a summary from his first chapter, “Introduction to objects”.
Objects and Classes Basics
Object Oriented Programming allows the programmer to represent elements from the problem in the solution space. This elements ,which are called objects, have characteristics(state) and behaviors (functions) and interact with other objects like they would in reality, by sending messages. This objects are Instances of a class, and have the same behaviors the do.
The class client would have characteristics common to every client, however each client’s characteristics value (state) will be different for every client, making each of them a different object. But since them all “come” from the same type (class) they all have the same behaviors. You can create as many objects from a class as you need.
The object interface establishes the functions a certain object can perform and the characteristics it has.
For example:
Car |
License Plate Color |
Start() Gas() Break() |
<---------------- Class name
<------------------ Characteristics
<------------------------- Functions
In order to expose only the code necessary to the client, there are certain keywords to set boundaries. These are:
§ public: means that it’s available to everyone.
§ private: only functions inside the class can access it.
§ protected: it works like private, except that inherited classes have access to protected members, unlike private ones.
Besides trying to represent elements from the problem space, classes should be reused. A way to do it is creating a characteristic, within the class, that is another class. Continuing with the car example, you could create the Engine class as a part of the car, since the car HAS – AN engine. The engine would have its own characteristics, like horse power. This concept is called composition. This benefits the programmer, because his able to change the private members of his member object without affecting the rest of the code.
Another way to reuse a class is inheritance. This is achieved by creating a base class that groups two or more other classes with characteristics and behaviors in common. The child classes inherit from the base class the functions and state except for private members.
There are two ways to differentiate the child class from the base class:
1. Adding new functions to the child class.
2. Overriding a function from the base class. Then you are using the same function, but in this class it will do something different.
If the first way is applied it breaks the substitution principle, which says that the derived class and base class have the same interface. If they don’t the base class “is not general enough” (Thinking in C++ Second Edition, Volume I: Introduction to Standar C++, Bruce Eckel). However that principle is not always correct to apply.
Introducing polymorphism:
OOP uses Late binding which means that the compiler can’t determine the code being called until runtime. You state late binding by using the keyword virtual.
By using virtual you can “upcast”, meaning that you can treat the child class as if it was the base. This allows you to write generic functions without previous knowledge of what derived class they will need by using the base class.
Extreme Programming:
Write tests first. This allows you to:
§ Clearly define what a class is supposed to be able to do.
§ You can run them every time you build your software. This will allow the catching of errors that you wouldn’t have had you been only using the built-in testing in the language. Therefore you are able of making important changes knowing that they won’t disrupt your project.
Programming in pairs:
§ It consists on to people working per work station. It allows one person to write the code while the other is thinking about it. And not only having the code in mind but also the guidelines for XP. If the coder gets stuck they can switch roles.