Jueves 29 , 05:17 am
Today’s literature review:
- 1.3 A look at Procedure-Oriented Programming
- 1.4 Object-Oriented Programming Paradigm(OOP)
- 1.5 Basic concepts
- 2.3 A simple program “Hello world”
Object-Oriented Programming (OOP) is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. Thus, an object is considered to be a partitioned area of computer memory that stores data and set of operations that can access that data. Since the memory partitions are independent, the objects can be used in a variety of different programs without modification…(Balagurusamy, 2008).
1.3 Characteristics exhibited by procedure-oriented programming (POP) are:
- The emphasis is on doing things (algorithms)
- Large programs are divided into small programs called functions.
- Most of the functions shared global data.
- Data move openly around the system from function to function.
- Functions transform data from one type to another,
- The technique employs top-down approach in program design.
1.4 Object-Oriented Programming (OOP) treats data as a critical element in the program development and does not allow it to flow freely around a system. It ties data more closely to the functions that operate on it, and protects it from accidental modifications from outside functions. OOP allows decomposition into a number of entities called objects and then builds data and function around these objects. The organization of dta and functions in object-oriented programming is shown in Figure 1.6
Object-Oriented Programming (OOP) is an approach that provides a way of modularizing programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand. Thus, an object is considered to be a partitioned area of computer memory that stores data and set of operations that can access that data. Since the memory partitions are independent, the objects can be used in a variety of different programs without modification.
An object-oriented program consists of a set of objects that communicate with each other. The process of programming in a object-oriented language, therefore, involves the following basic steps:
- Creating classes that define objects and their behaviour,
- Creating objects from class definitions and,
- Establishing communication among objects.
Objects communicate with one another by sending and receiving information much the same way as people pass messages to one another. That makes it easier to talk about building systems that directly model or simulate their real-world counterparts.
A message for an object is a request for execution of a procedure, and therefore will invoke a function (procedure) in the receiving object that generates the desired result. Message passing involves specifying the name of the object, the name of the function (message) and the information to be sent. Example:
Real-business systems are often much complex and contain many more objects with complicated attributes and methods. OOP is useful in these types of application domains because it can simplify a complex problem. The promising areas for application of OOP include:
- Real-time systems
- Simulation and modeling
- Object-oriented databases
- Hypertext, hypermedia and expertext
- AI and expert systems
- Neural networks and parallel programming
- Decision support and office automation systems
- CIM/CAM/CAD System
Procedure-oriented programming (POP) has to majors drawbacks: (1) data move freely around the program and are therefore vulnerable to changes caused by any function in the program; (2) POP does not model very well the real-world problems. OOP was invented to overcome the drawbacks of POP.
In OOP, a problem is considered as a collection of a number of entities called objects. Objects are instances of classes. Insulation of data from direct access by the program called data hiding.
Data abstraction refers to putting together essential features without including background details.
Inheritance is the process by which objects of one class acquire properties of objects of another class.
Polymorphism means one name, multiple forms. It allows us to have more than one function with the same name in a program. It also allows overloading of operators so that an operation can exhibit different behaviours in different instances. Dynamic binding means that the code associated with a given procedure is not known until the time of the call at run-time.
//“Hello World” program
using namespace std;
cout << «Hello World\n»;
A C++ program is a collection of functions. The above example contains only one function, main(). Execution begins at main(). Every C++ program must have a main().
cout ( C Out) represents the standard output stream in C++
The operator << the insertion or put to operator. It inserts the contents of the variable on its right to the object on its left, as it is shown in Figure 2.1
Here, the identifier cout is a predefined object representing the screen and it has a simple interface. If string represents a string variable, then the following statement will display its contents:
cout << string;
The operator << is also the bit-wise left-shift operator is an example of an operator overloading, an important aspect of polymorphism. We have used the following #include directive:
This directive causes the preprocessor to add the contents of the iostream file to the program. It contains declarations for the identifier cout and the operator<< . The header file iostream should be included at the beginning of all programs that use input/output statements.
Namespace is a new concept introduced by the ANSI C++ standards committee. This defines a scope for the identifiers that are used in a program. For using the identifiers defined in the namespace scope we must include the using directive,like:
using namespace std;
In C++, main() returns an integer type value to the operating system. Therefore, every main() in C++ should end with a return(0) statement; otherwise an error may occur. The return type for main() is thus explicitly specified as int.
A slightly more complex program
Assume that we would like to read two numbers from the keyboard and display their average on the screen.
using namespace std;
float number1, number2, sum, average;
cout << «Enter two numbers: «; // prompt
cin >> number1; // reads number
cin >> number2; // read number from the keyboard
sum = number1 + number2;
average = sum / 2;
cout << «Sum=» << sum << «\n«;
cout << «Average=» << average << «\n«;
The program uses four variables that are declared as type float by the statement
float number1, number2, sum, average;
All variables must be declared before they are used in the program.
Input Operator. The statement:
cin >> number1;
causes the program to wait for the user to type in a number, which is placed in the variable number1. The identifier cin ( C in) is a predefined object in C++ that corresponds to the standard input stream.
The operator >> is known as extraction or get from operator. It extracts the value from the keyboard and assigns it to the variable on its right. This operator can also be overloaded.
Cascading of Input/Output Operators
We have used the insertion operator << repeatedly in the last two statements for printing results:
cout << «Sum=» << sum << «\n»;
cout << «Average=» << average << «\n»;
First, send the string «Sum=» to cout and then sends the value of sum. Finally, it sends the newline character \n so that the next output will be in the new line. When cascading an output operator we should ensure blank spaces between different items. The two statement can be combined as:
cout << «Sum=» << sum << «\n»
<< «Average=» << average << «\n»;
This is one statement but provides two lines of output. We can also cascade input operator >> as shown below:
cin >> number1 >> number2;
The values are assigned from left to the right.
Review by: Larry Francis Obando – Technical Specialist – Educational Content Writer.
Escuela de Ingeniería Eléctrica de la Universidad Central de Venezuela, Caracas.
Escuela de Ingeniería Electrónica de la Universidad Simón Bolívar, Valle de Sartenejas.
Escuela de Turismo de la Universidad Simón Bolívar, Núcleo Litoral.
Contact: Caracas, Quito, Guayaquil, Cuenca – Telf. 00593998524011