U
U
Urukhayy2014-10-29 16:03:47
Programming
Urukhayy, 2014-10-29 16:03:47

Examples of classes in programming?

Actually, can you give vivid examples of why classes are needed? What did not please many conventional, modular or procedural design? I mean, how is a variable in a class "better" than a global variable? And how is enum in C++ related to classes?
After all, you can do without classes ..

Answer the question

In order to leave comments, you need to log in

5 answer(s)
A
aush, 2014-10-29
@Urukhayy

The venerable master Qc Na was walking with his student, Anton. Hoping to prompt the master into a discussion, Anton said "Master, I have heard that objects are a very good thing - is this true?" Qc Na looked pityingly at his student and replied, "Foolish pupil - objects are merely a poor man's closures."
Chastised, Anton took his leave from his master and returned to his cell, intent on studying closures. He carefully read the entire "Lambda: The Ultimate..." series of papers and its cousins, and implemented a small Scheme interpreter with a closure-based object system. He learned much, and looked forward to informing his master of his progress.
On his next walk with Qc Na, Anton attempted to impress his master by saying "Master, I have diligently studied the matter, and now understand that objects are truly a poor man's closures." Qc Na responded by hitting Anton with his stick, saying "When will you learn? Closures are a poor man's object." At that moment, Anton became enlightened.

Anton van Straaten
Classes can be perfectly dispensed with. Moreover, using the example of development in object-oriented C #, you can see that if you strictly follow the principles of Single responsibility and Open / closed from SO LID, then the code begins to strongly resemble functional.
There is no such thing as better/worse. There are different tasks, different approaches and different tools.
We can say that the main task that classes solve is encapsulation. Suppose two people work on the same project and both create some kind of global variable for their task and work with it in their functions. As a result, none of them can be sure what value this variable has at a given point in time. If each of them follows the principle of encapsulation, then this problem usually does not arise.

V
Viktor Vsk, 2014-10-29
@viktorvsk

The assembler is great. But life is short.

C
cjey, 2014-10-29
@cjey

Classes are needed to implement an object-oriented approach in programming.
Therefore, it is more correct to compare OOP and Procedural or Modular Programming.
All approaches have their pros and cons, somewhere one approach is more convenient, somewhere else.
In short, the advantages of OOP:
* Encapsulation. Checking data access rights at the language level. You can't accidentally mess up someone else's variable. Combining data and data manipulation methods into one entity.
* Abstraction. You can work with objects as with a black box, without thinking about how it works inside.
* Inheritance. You can generate an entity based on an existing one by changing it a little. There is no need to fully describe the new entity, it is sufficient to describe only the differences.
* Polymorphism. You can work with a group of different entities through a common interface.

B
bromzh, 2014-10-29
@bromzh

Yes, it is possible to implement all OOP patterns without using classes, objects and other things. But in fact, OOP allows you to do this more clearly and more conveniently in some cases. Those. you do not need to implement any patterns in your program, it's all done in the language itself for you. OOP is a kind of abstraction for working with data, as with objects in the real world.
It's just that there are areas where OOP is best suited for solving a problem. There are areas where it will not be optimal. The main thing is to know and understand where it is needed and where it is not. This already comes with experience.

R
Rinat Zakiryanov, 2014-10-29
@Rinz

Supporters of the "procedural approach" are strange, but I do not deny that everything can be done without OOP, but everyone forgot about the "readability of the code", in the "procedural approach" very often there is a wild shit code with which you can't figure out what and where it comes from (if the project is large), OOP is not needed for small projects, there will obviously be no reason for it (if this project does not need to be developed).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question