Structured programming and object-oriented programming

In early programming, programming flowchart and top-down method. With this design method, the programmer will be a big problem into smaller tasks, and a write process (or function) for each of the smaller tasks. Finally, programmers will write a master process to start the program flow, then according to program flow, call other procedures you want. This type of programming is known as structured programming . There are many structured programming language is widely used, the most prominent is the C language.

In structured programming, the program around tasks to be solved by design. Programming is the process of preparing to perform specific tasks needed during the data transfer to the process by process parameters. Procedure to view and change the data passed in, and value can be returned to the calling process.

After the 1970s, OOP ( Object Oriented Programming , referred to as OOP) became popular. OOP takes a completely different approach to the development of computer applications. In this method, the program is not around to solve the task to design, but around the target problem to be solved in the design. For each object, we will write a class to describe the properties and behavior of objects. It is a description of the object class. Similarly, an object is an instance of the class. Objects from the properties and behavior of components. Attribute object has characteristics, and behavior objects can do the action. Each attribute of the object is represented as a class member variable. Each object's behavior has become a method in the class.

To demonstrate the difference between structured programming and object-oriented programming, we use a simple example to do the payroll program to help understand.

If a company wants us to write a payroll program. The company's employees get paid weekly, but the staff salary system is different: some employees wages in accordance with or daily basis, remuneration is calculated based on weekly working hours; some wages are fixed annual salary. Also, the calculation of wages to be deducted from Social Security, Medicare and income taxes.

If we use a structured programming approach to programming, then the program will write as many processes payroll tasks. For example, we can write a procedure called computePay () is to enter an employee's payroll data, and return to their wages. You can also write process called computeMedicareTax (), computeSSTax () calculation of health insurance, social security and so on. Each process requires employee data passed in. For example, if you want to call computePay () process, we need to staff remuneration information (such as working hours, hourly) passed in. Data transfer between processes, which is structured programming occur frequently. The process of changing data passed in and returns data to the calling process.

With the development of employee wages program in order to allow each work smoothly, we will find the need to add and change process. For example, when you start writing computePay () process, we will find that there are two different types of employees: salary and staff take the time to take the annual salary of employees. In this case, we need to write two computePay () procedure, one for employees to take paid time and one for employees to take annual salary.

Now, we'll procedures wages for employees from a different perspective. This time, we will not issue as a small task to be performed, but starting from the object to determine the problem domain, that is, by way of object-oriented programming to write programs.

Objects can be any person appearing in the problem domain, thing or entity, simply put, it is "something." In the payroll example, we give employees wages, so the staff (Employee) is the object. Employees working for the company, so the company is another object. After more in-depth analysis, we may find that payroll department is also an object. After the start of programming, we will find that in the initial design of other less obvious objects.

After the problem domain objects have been found, we will write a class to describe the properties and behavior of each object. For example, we will need an Employee class to accommodate an employee's attributes and behavior.

Property of an Employee object refers to what employees, for example, name, address, employee number, social security number, and so on. Each property will be represented by a member variable Employee class.

Employee behavior of a target object is what employees can do, or what we want to do these objects. Employees can do many things, but wages for the program, we only need to be able to calculate their payment, and then mailed them a week to send a pay check. These unwanted behavior has become the method of the Employee class.

For each employee in the company, we instantiate an Employee object. If you have 50 employees, you need 50 Employee object. In memory, there are 50 names, addresses, salary and so on. Each employee can be distinguished, so we need 50 reference variable reference type variable by a. In the back, we will see how to instantiate an object and assign a reference to it.

Note: Object-oriented programming and structured programming, are transmitted to the data by calling between the method, but there are considerable differences between the two.

If the structure of a program to process data required to perform a task, the required data is passed to the procedure. In object-oriented programming, objects of our mission, ways to access the required data, without the need to pass data to the method.

For example, if an employee's remuneration is to be calculated, we do not need to pass the relevant Employee object to computepay () method, just call computePay Employee object on behalf of the employee () method can be. Because computePay () method is part of an Employee object, so it can access all member variables Employee object, including the remuneration per hour Employee object, salaries, and other required data.

Guess you like

Origin blog.csdn.net/qq_45097560/article/details/90757039