object approach

advertisement
Programming paradigms (Means an approach to programming)
1) Procedural Programming Approach: This is a Top-Down approach of solving the
problem. In this approach the programme is divided into a number of modules. Each module
works on its local data as well as global data.
Main Program
Global Data
Main program divided
into several modules (functions)
module 1
Local
Data
All
functions
are
changing the global data.
module 2
Local
Data
module 3
Local
Data
Now we have to understand two three important points.
In any program, there is a global data which is accessible (used) by all the modules, and a
local data of any particular module which can be accessed by that module only.
Since the global data is accessible to every module, if one module is changing the global data,
then it will effect the other modules also because other modules may be unaware of this
change in data.
Hence we can say that the data is insecure in this kind of approach.
To solve this problem, when a module changes the global data, it should make the other
module aware of this change. This makes the process very complex.
Drawbacks:



The program is divided into functions (modules) and functions are the main point of
concern rather than the data.
Maintaining the state of data is difficult since all functions can change it. Updating
this information is a headache.
This approach does not cope with real world scenario.
2) Object Oriented Programming (Bottom Up Approach) – In OOP we deal with
“Objects”. Anything in the real world which has State, Behaviour and Identity (SBI) is called
as object.
Now these objects consist of space for data and functions also. These functions
operate on the corresponding data. The scope of data is within the object boundaries.
The objects in turn can communicate with other objects (Hence data is not changed by
some other module etc. like that of procedural programming approach). Thus data is secure
here.
OBJECT 1
OBJECT 2
Data
Data
Functions
Functions
Data
Functions
OBJECT 3
Advantages over procedural/structural approach:




Data is the main point of concern.
Data is secure here. It cannot be changed by all the functions or module.
No need to take extra headache on maintaining the state of data by informing every
functions about the changes happened to the data (was the case of procedural
approach)
OOP deals with the real world programing since we work on objects here.
BASIC CONCEPT OF OBJECT ORIENTED PROGRAMING
1) Data Abstraction: (Data Hiding) – Only the needed part is exposed to the others without
disclosing everything.
2) Encapsulation (Wrapping/folding up): The data is not alone, rather it is wrapped up with
functions in a OBJECT as shown in the above diagram.
3) Inheritance: “The capacity to EXTEND the properties of a previous concept” is a very
important characteristic of OOP.
4) Polymorphism (Same name of function, different way of its processing): In polymorphism,
we have two or more functions with same name. But the logic of them (the way of their
functioning/processing) may be different (on the basis of data provided) (Diagram 5.7 at page
139)
Note: Please have a look on the pages 134 to 139.
USER DEFINED DERIVED DATA TYPES
There are some predefined data types present in C & C++ but as the level of our programing
rises, the logic cannot be carried out with the help of these pre-defined data types alone.
For that we need to create/define our own data types. These are called as User-defined datatypes.
1) Array: Array is a collection of similar data types.
2) Structures: These are the collections of different data types.
3) Class: Class is a collection of not only different data types but also functions.
FORMATTING OUTPUTS




Outputs are displayed in the monitor screen.
The outputs can be formatted using two popular functions; setw( ) and setprecision( ).
These functions are present in the header file “ iomanip.h” so, you have to include this
header file in your program before using these functions.
setw( ) : Set the width of the value of variable
o Syntax: setw( width which we want)
o Example: cout<< setw(7)<< “R”
Output: _ _ _ _ _ _ R
Example:
cout<< setw(7)<< 22
cout<< setw(7)<< 224
cout<< setw(7)<< 65478
Output:
_____22
____224
__65478

setprecision ( ): When a decimal (floating type) number is to be displayed, this
function sets the total length of value including the decimal part.
Example: cout<< setprecision( 5 ) << 347.6789 // output: 347.67
cout<< setprecision( 6 ) << 347.6789 // output: 347.678
cout<< setprecision( 4 ) << 347.6789 // output: 347.6
C++ SHORTHANDS



For adding 1 in the same variable: a=a+1 or a++
For subtracting 1 from the same variable: a= a- 1 or a - For adding some other no.
o a= a + 10
or a+= 10
o a= a – 10
or a - = 10
o a=a*7
or a * = 7
o a=a/8
or a / = 8
o a=a%b
or a % = b
Download