
Chapter One
Introduction:
C++ is an Object Oriented Programming (OOP) Language. After the resounding success of C, it was indeed a stroke of genius on the part of Dr. Stroustrup to make a bold and decisive initiative to extend it to include class mechanism. In fact, Dr Stroustrup initially named C++ as ‘C with classes’. It may be regarded as an extension of C or a superset of C, developed with the sole purpose of supporting Object Oriented Programming. Classes are not only a new, extended data type in C++, but also the core of design for OOP implementation.
Structured Programming Vs. Object Oriented Programming
Structured Programming is a method of developing high quality programs through orderly and logical organization of independent yet interconnected blocks or modules. This highly systematic technique consists of: (a) sequential structure (top-down pattern) where instructions are carried out in the given order from start to end; (b) loop structure which causes the particular block or part of the code to be repeated as per specified conditions; and (c) decision making or option structure where a test-condition is used to determine whether the corresponding instructions are to be executed or skipped. You may note at this stage that this is precisely the reason why your teacher always discouraged you from using GOTO statements in BASIC programming – they violate structured programming norms and lead to clumsy and bad programs.
The approach hitherto had been to break down a given task into the smallest possible units and to develop codes for those units, which are subsequently integrated into a complete program.
A further improvement in programming technique has now evolved in keeping with the complexities of real-life situations. The new, modern style is called Object Oriented Programming (OOP). The emphasis here is on the what aspect rather than the how aspect of the programming requirement. OOP is a strictly disciplined programming concept based on the idea of the eventual unit as a comprehensive OBJECT, instead of the usual functional decomposition method mentioned above. It builds on the concept of reusability of code through the use of OBJECTS.
The advantage of OOP over traditional structured approach are briefly tabulated below:
|
OOP |
Non-OOP structured program |
|
1. It is built on the concept of OBJECTS which have both state (attributes) and behavior (operations) |
1. It is built on a hierarchy of processes, each process being the smallest functional decomposition of the problem |
|
2. It hides data, showing only behavior |
2. May not always be so |
|
3. Objects are modular and are highly reusable |
3. Process may or may not be reusable, depending on the implementation |
|
4. OOP is based on message-based or event-driven paradigm. Objects may pass message to other objects |
4. This style results in linear, algorithm-based implementation |
|
5. Information (data-variables and their values) and processes (functions / operations on those data) are close-knit and insulated from accidental outside influences |
5. Such a high degree of cohesion, integration and security cannot be guaranteed
|
Compiler and Interpreter: Any program written in high-level language is called the source code and has to be converted into machine-readable language or object code. A program that converts source code (in C++ or BASIC or COBOL or any other) into object code is called the TRANSLATOR.
Translators may be interpreters or compilers. Interpreters translate the code line-by-line or statement-by-statement and they are immediately executed, the translated object code is not saved. Thus, translations take place every time the code is run. Compilers on the other hand translate the entire program and save the translated version (object code) as a separate object file. Any subsequent run is directly from the object file and hence needs no translation, making it very fast. Also interpreted program runs until an error is encountered, while compiling automatically involves checking the entire program and ensuring it to be error free before the object file is created. C++ is a compiled program.
OBJECTS – STATE & BEHAVIOR:
A clear concept about OBJECT is imperative at the outset as it is the basis of all the skills required in developing OOP.
An Object is an independent entity exhibiting a state and a set of behaviors that serves to build an application.
The following examples shall make the meaning clear.
Suppose we are asked to write a program to monitor the roll-strength of a school class-wise. Each classroom may be regarded as an Object.
The state of the Object is one or more attributes of the Object, represented in many cases by one or more numerical values.
Thus the number of students in the classroom (n) defines the state of the Object (a particular classroom) at a given time – say, n = 30.
The behaviors of the Object may be defined by the processes or operations working upon the Object or affecting its state.
Thus, each new admission to the particular classroom increases its strength (n) by one. Similarly, each student leaving it will decrease the state (namely its roll-strength or n) by one. These are represented by the operations of increment and decrement to the value of n. The relevant functions (let us name them JOINING and LEAVING) will consist of adding to or subtracting from the Object’s current state (value of n).
An integrated capsule (so to speak) comprising of the state (value of n) and the processes (the functions called JOINING and LEAVING) forms an Object.
A Queue is another example of an Object – its chief attribute (state) is surely the number of persons (x) waiting in the queue. People joining the queue will increase the value of x and those leaving the queue will decrease the value of x, representing the behaviors of the Object.
Multiple Attributes:
Although for the sake of simplicity we had assumed the state of the Object to be represented by a single attribute, this may not always be the case in real-life situations. In the first example, suppose that the Principal wishes to maintain the number of boys and girls separately. So the state of the Object (a particular classroom) should be represented by two attributes – num_boys and num_girls. Entry or exit of a boy would affect num_boys only.
In another school, it is required to maintain the numbers of free students, half-free students, general students and total strength separately for every classroom. The state of the object would then be represented by four attributes – a, b, c, t – where t (total) = a + b + c. If a free student leaves, it only affects a and t. If a half-free student joins, it increases the value of b and t. If a half-free student is granted full free-ship, it would increase the value of a and decrease the value of b leaving the total unchanged. Thus the object has a bunch of attributes to define its state, and likewise, admits of several operations to control its behavior.
CLASS:
A full description of CLASS will be taken up later, but a brief idea of it may be in order at this stage. A CLASS, unlike an Object, is not an entity. A Class is an abstract concept, it defines somewhat as a template the features of all Objects similar in attributes and behaviors. A Class is a generalized description of some state and behaviors that all Objects of the type would possess in common. Once Class is described, it serves as a ‘type’, which may be used to generate any number of Objects. Hence Classes may be regarded as Object factories. We may think of Class as abstract models or patterns, and Objects to be instances or concrete entities thereof possessing all features of the respective Class.
Thus, Class in C++ consists of user defined data structures, member functions and custom operators. Classes bind together data descriptions (member variables) and algorithms (associated member functions to make a data type, which in turn serves to create instances (objects) of the type.
For example, let us describe Class Queue. As we saw earlier, the state of any Queue would be the count of people in the line and its behavior is the increment or decrement to that number as people come and go. We may describe this pattern for Class Queue.
Now suppose that there are three queues – say in front of a railway ticket counter, a bus ticket counter and a telephone bill counter. Each of these queues – say Q1, Q2, Q3 – is an Object of Class Queue, sharing in common the attribute and behavior of Class Queue.
Similarly, if we define Class Boy with attributes like height, weight, etc., it only means that every Object belonging to this Class Boy has a height and a weight. A boy called John is an Object under Class Boy, he shares the attributes and behaviors with all Objects of Class Boy – like Tom, Peter, Harris, etc.
what's the matter dear
no post
MOKSHA
Sorry.. there is nothing..