OOPS Object-oriented programming syndrome
OOPS (Object-oriented programming syndrome) is the paradigm behind many a modern language today, like Java, C#, Visual Basic, to name a few. Methodologies differ in strange and illogical ways in the field of information technology. Many a question is usually raised by the uninitiated on the need for so many different methodologies in programming technique. C has its own methodology, as we have seen already; Basic has its own; COBOL has its own; C++, too, has its own methodology OOPS, the most fascinating, organized and highly scientific set of principles that leads to a highly logical approach to programming manual problems. The need for so many different methodologies is the same need that drives many a car of different makes on the roads in the world !
OOPS is a solid foundation of functions and data-types based on classes and objects. This may sound rather vague but it is noteworthy, here, that the OOPS terminology is based on English verbs or words which are highly self-explanatory, i.e. these words carry the same English meaning which leads to a faster understanding of the technique that the word implies. Objects, in OOPS, are abstract in context not like the real-life objects. We shall go into a deeper discussion as we progress. A story goes that the birth of todays OOPS occurred in an university in Scotland. This university had to automate the live stock in its premises. And there were three kinds of live stocks humans, animals and plants. When this program was getting implemented, the programmers found that they could better handle the problem if each live stock were to be treated as classes. That is, a human class, an animal class and a plant class. Newer technologies like attributes, properties, members of the class were added with more thought on the new methodology which was called, OOPS - maybe because it was stumbled upon ! Slowly, newer techniques like polymorphism, inheritance, data-abstraction, prototyping, friends, overloading instances, constructors and other concepts came into being and soon, OOPS became and still is, the most powerful methodology that is adopted for programming.
Classes & Objects
A class, in layman terms, means classification of entity into groups according to the commonalities and relationships of the entity with its members. Class, in OOPS, is an abstract structure. It is a container for methods and variables which are called member functions and data-members respectively. As you know, in C, there is a "main" function which is the entry point for the OS into your program. The same applies in C++, too. The only difference being, functions and data-members are defined inside a class i.e., a class acts as the container for function and classes and through an object, which is an instance of the class, the members of the class are accessed. This forms part, albeit a miniscule part, of the data-abstraction design of OOPS in C++. That is, functions cannot be randomly accessed or called, as in C and further, functions cannot randomly access any variable in the program, even if the variable is declared as global. The word global , henceforth, will be replaced with the word public and the word local with the word private or protected. An object can be defined as an instance of a class. You will understand more about objects as we progress through the next few pages in our quest for better understanding of OOPS.
Access specifiers
Public, private and protected are keywords which serve as access specifiers and are usually used within the class to assign a scope to a variable or a class. For example,
class a{
public:
int x;
private:
int y;
protected:
void show(){
cout << "Hello world !";
}
};
A class called a has been defined, above, with one private and one public data-member and a protected member function.
They act as scope regulators for the members defined within them. The default scope, in C++ within a class, is private. If the program were to be compiled for execution, it will not compile correctly. The reason being, the compiler will complain that function _main is not to be found. The above program requires a main function to be defined which will activate the class through instancing an object of type class which shall further be used to access the class. The main function will be defined as follows for the above class
void main(){
a myobj; // Instance created of type class where myobj is the name of
// the object
myobj.show(); // Through the object the member function show() is being
// called.
}
Now the program is ready to be run !
The three access specifiers are part of the data-abstraction principles of OOPS. Data is hidden from accidental reference or changes by unauthorized functions. This also forms the more important stress on the design of the programming logic of your program. At the time of the conception itself, you will be required to identify the permissions that can be allowed on certain data-members and the need for controlling needless changes to these members. So, in the above program, x and y, although are of same types, cannot be so easily related to each other or accessed from anywhere in the program. We cannot say,
x=y; // Error !
because x is public scoped and y is private scoped. For many reasons, at many a times, a programmer needs to declare certain variables as private which can only be accessed by and through the member functions of the class. We cannot write,
void main(){
a myobj;
myobj.x=7;
myobj.y=12; // Error !
}
Because, the compiler will generate an error "variable y does not exist !. So well is the private data member hidden from accidental manipulation from other entities of the same program ! So, how do we access the private data member ? The answer is simple. Through, as we have noted already, the member function of the class.
protected:
void show(){
y=10; // Okay !
x=5; // Okay !
y=x+y; // Okay !
cout << y;}
}; // End of the class
All right ! Now, we have access to the private member but still, the program is not complete. Because we have not yet called the show() function. In C++, the function is called similar to the way the data member is accessed through the instance of the class, the object. As below.
void main(){
a myobj;
myobj.show(); // Call to member function
}
Here we have arrived at the most fascinating and most powerful feature of OOPS Data-Encapsulation.