ComputersProgramming

OOP is what? Basic principles of object-oriented programming

Why object-oriented programming is preferred in most projects? OOP offers an effective way to deal with their complexity. Instead of considering the program as a sequence of executable instructions, it represents it as a group of objects with certain properties and performs certain actions with them. This leads to the creation of more clear, more reliable and easy-to-follow applications.

The basic principles of object-oriented programming (OOP) were formed because limitations were found in the earlier approaches. Among them - unlimited access to data and a large number of links that impose restrictions on the introduction of changes. Their awareness and reasons are important in order to understand what OOP is in programming and what are its benefits.

Procedural languages

C, Pascal, FORTRAN and similar languages are procedural. That is, each of their operators orders the computer to do something: get the data, add up the numbers, divide by six, display the result. The procedural language application is a list of instructions. If it is small, no other organizational principle (often called a paradigm) is required. The programmer creates a list of instructions, and the computer executes them.

Separation into functions

When applications get bigger, the list turns out to be cumbersome. Few can understand more than a few hundred instructions until they are grouped together. For this reason, the function has become a way to make applications more understandable for their creators. In some languages, the same concept can be called a subprogram or procedure.

The application is divided into functions, each of which has a clearly defined purpose and interface.

The idea of separation into procedures can be extended by grouping them into a larger object called a module, but the principle is similar: grouping components that execute instruction lists.

Separation into functions and modules is one of the cornerstones of structural programming, which for several decades before the emergence of OOP was a pre-emptive paradigm.

Problems of structural programming

As applications grew larger, structural programming began to experience difficulties. Projects became too complicated. The graphs shifted. A greater number of programmers were involved. The complexity grew. The costs skyrocketed, the chart shifted further, and the collapse came.

An analysis of the causes of these failures showed the shortcomings of the procedural paradigm. Regardless of how well a structured approach to programming is implemented, large applications become excessively complex.

What are the causes of these problems associated with procedural languages? First, functions have unlimited access to global data. Secondly, unrelated procedures and meanings do not model the real world.

If we consider these problems in the context of a stock-taking program, then one of the most important global data elements is the aggregate of accounting units. Different functions can access them to enter a new value, display it, change it, and so on.

Unlimited access

In a program written, for example, in C, there are two kinds of data. Local are hidden inside the function and other procedures are not used.

When two or more functions need to access the same data, then the latter must be global. These, for example, are information about the items to be taken into account. Global data can be accessed by any procedure.

In a large program there are many functions and many global elements. The problem with the procedural paradigm is that this leads to an even greater number of potential links between them.

Such a large number of compounds causes several difficulties. First, this complicates the understanding of the structure of the program. Secondly, it makes it difficult to make changes. A change in a global data item may require that all functions that have access to it be adjusted.

For example, in the accounting program, someone decides that the code of the item being considered should consist of not more than 5 digits, and out of 12. This will require changing the data type from short to long. Now the code-related functions must be changed to work with the new format.

When elements change in a large application, it is difficult to say which procedures have access to them. But even if this is clarified, changing them can lead to incorrect work with other global data. Everything is connected with everything else, so the change in one place will be in another.

Real-world modeling

The second and more important problem of the procedural paradigm is that its location of individual data and functions does not model things in the real world. Here we deal with such objects as people and cars. They do not look like data or functions. Complex real objects have attributes and behavior.

Attributes

Examples of attributes (sometimes called characteristics) for people are eye color and job title, for cars - power and number of doors. As it turned out, attributes in the real world are equivalent to data in the program. They have specific meanings, such as blue (eye color) or four (the number of doors).

Behavior

Behavior is what real-world objects produce in response to some kind of impact. If you ask the authorities to raise their salaries, the answer is "yes" or "no." If you press the brake, the car stops. Pronunciation and stop are examples of behavior. Behavior is like a procedure: it is called to do something, and it does it. Thus, data and functions alone do not model real-world objects efficiently.

Solution

The object in OOP is represented as a collection of data and functions. Only procedures that are called member functions in C ++ allow you to get its values. Data is hidden and protected from change. Values and functions are encapsulated in one unit. Encapsulation and hiding are the main terms in the description of OO-languages.

If you want to change the data, you know exactly which functions interact with them. No other procedures can access them. This simplifies writing, debugging and maintaining the program.

An application, as a rule, consists of several objects that interact with each other, invoking member functions.

Today the most widely used language of OOP (object-oriented programming) is C ++ (plus-plus). Java lacks some functions, such as pointers, templates, and multiple inheritance, which makes it less powerful and versatile than C ++. C # has not yet reached the C + + popularity.

It should be noted that the so-called member functions in C ++ are called methods in some other OO languages, such as Smalltalk. Data elements are called attributes. Calling an object method is sending a message to it.

Analogy

You can submit objects to the company's departments. In most organizations, employees do not work one day with cadres, the next time they pay salaries, and then spend a week doing retail business. Each department has its own staff with clearly assigned responsibilities. There are also own data: salaries, sales figures, employee records, etc. People in the departments work with their information. Separation of the company, thus, facilitates the control over its activities and maintains the integrity of the data. Accounting is responsible for payroll. If you need to know the total amount of wages paid in the southern branch in July, you do not need to rummage in the archive. It is enough to send a note to the person in charge, wait for this person to get access to the data and send a response with the required information. This ensures compliance with the regulations and the absence of outside interference. In the same way, the object in OOP provides the organization of the application.

It should be remembered that the orientation to objects does not concern the details of the program. Most C ++ statements correspond to procedural language operators such as C. Indeed, member functions in C ++ are very similar to functions in C. Only a broader context will allow to establish whether the instruction is procedural or object-oriented.

Object in OOP: definition

When considering the problem of programming in an OO language, instead of questions about its separation into separate functions, the problem of separation into objects arises. OOP thinking makes it much easier to develop applications. This is due to the similarity of software and real objects.

What things become objects in OOP? Below are typical categories.

The physical object in OOP is:

  • Transport in flow patterns;
  • Electrical elements in circuit design programs;
  • Countries in the model of economy;
  • Aircraft in the air traffic control system.

Elements of the computer environment of the user:

  • menu;
  • window;
  • Graphics (line, rectangle, circle);
  • Keyboard, mouse, printer, disk drives.

People:

  • Employees;
  • Students;
  • Customers;
  • Sellers.

Data:

  • Ledger;
  • private bussiness;
  • dictionary;
  • Table of latitudes and longitudes of settlements.

The connection between real-world objects and OOP was the result of a combination of functions and data: they revolutionized programming. There is no such close correspondence in the procedural languages.

Class

Objects in OOP are members of classes. What does it mean? Programming languages have built-in data types. The int type , that is, an integer, is predefined in C ++. You can declare any number of int variables.

Similarly, a set of objects of the same class is defined. It defines the functions and data included in its objects without creating them, just as int does not create variables.

A class in OOP is a description of a number of similar objects. Prince, Sting and Madonna are singers. There is not a single person with that name, but people can be so called if they have the appropriate characteristics. The OOP object is an instance of the class.

Inheritance

In life, classes are divided into subclasses. For example, animals are divided into amphibians, mammals, birds, insects, etc.

The principle of this kind of division is that each subclass has common characteristics with the class from which it occurs. All cars have wheels and an engine. These are the defining characteristics of vehicles. In addition to the general characteristics, each subclass has its own characteristics. The buses have many seats, and trucks have space for the transport of heavy goods.

Similarly, the base class can become the parent of several derived subclasses that can be defined so that they will share its characteristics with the addition of their own. Inheritance is similar to a function that simplifies the procedural program. If several parts of the code do almost the same, you can extract the common elements and put them into one procedure. Three application sections can call a function to perform common actions, but they can also perform their own operations. Similarly, the base class contains data common to the derived group. Like functions, inheritance reduces the OO program and clarifies the interconnection of its elements.

Reuse

After the class is created and debugged, it can be passed on to other programmers for reuse in their own applications. This is similar to a library of functions that can be included in different applications.

In OOP, inheritance is an extension of the idea of multiple use. From the existing class, without changing it, you can create a new one with the addition of other functions. The ease of reusing existing software is an important advantage of OOP. It is believed that this provides an increase in returns on initial investment.

Creating new data types

Objects are convenient for creating new types of data. Suppose the program uses two-dimensional values (for example, coordinates or latitude and longitude), and there is a desire to express actions with them arithmetic operations:

Position1 = position + origin,

Where position1, position and origin are pairs of independent numerical values. Creating a class that includes these two values and declaring variables with its objects creates a new data type.

Polymorphism, overload

The operators = (equal) and + (plus) used in positional arithmetic above do not act in the same way as with built-in types, such as int. Objects position and others are not predefined, but are programmed. How do these operators know how to handle them? The answer lies in the fact that you can set new behavior patterns for them. These operations are member functions of the Position class.

The use of operators or procedures, depending on what they work with, is called polymorphism. When an existing operator, such as + or =, is able to work with a new type of data, it is said to be overloaded. Overloading in OOP is a kind of polymorphism. It is its important feature.

The book about the OOP "Object-Oriented Programming for Dummies" will allow everyone to get acquainted with this topic in more detail.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.