What's the difference between a procedural pro

2019-01-30 02:31发布

I'm fairly new to programming but I've been reading some interesting discussions on StackOverflow about various programming approaches. I'm still not 100% clear on what the difference is between procedural programming and object oriented programming. It sounds like object oriented programming still uses procedures (methods) but everything is organized differently because the object is the star of the show. But it seems to me that procedures still allow you to do all of the same things. Like in C, you can put all of your similar procedures into a library. So couldn't you really say that a library in C is similar to an object in C++?

17条回答
Summer. ? 凉城
2楼-- · 2019-01-30 02:53

In a procedural program, the code is king and the data is subordinate. In other words, you have programs which act on data and they're not usually tightly bound.

In the OO world, objects are the primary thing of interest. An object consists of data and the code that is allowed to act on that data, and they are very tightly bound. It is the concept of encapsulation, the hiding of information.

An example, let's say you have a number and you want to double it. A procedural way of doing this is:

n = n * 2

The code here quite explicitly multiplies n by 2 and stores the result back into n.

The OO way of doing this is to send a "message" to the number object telling it to double itself:

n.double();

The advantage of this is called polymorphism. What happens when you decide you want to be able to double a string like "bob". In the procedural world, you'd have to provide more code to do the doubling but you'd also have to call that code differently.

With OO, you create a string object which can also take the 'double' message. The code to double a string belongs to the string object so it knows it has to act differently to the number object. If it decided that "bob" * 2 was "bobbob", the code would look something like:

class number:                    class string:
    int n                           char array s
    procedure double:               procedure double:
        n = n * 2                       s = string_join(s,s)

Then you could call x.double() no matter what actual type x was (number or string) and it would know which code to run - this greatly simplifies your code. You can double integer, strings, matrices, complex numbers, reals, window sizes on your monitor and all sorts of different things.

And you're right, a C library can be made to look a little bit like objects. The classic example is stdio.h - you don't ever care what a FILE* actually points to, just the fact that it will behave in a certain way. The FILE*, fopen(), fclose() and other functions are an class of sorts representing the I/O capabilities of C.

查看更多
爷的心禁止访问
3楼-- · 2019-01-30 02:55

The way C++ is implemented just makes OO programming look a lot like procedural programming. You need to shift your thinking slightly.

In C++ objects have methods that are just procedures that act on the object. But in a real OO paradiam you should think of the methods as potential messages that the object can recieve (ie letters). The object recieves a message (the parameters represent the payload of the message ie the content of the letter) and changes its state based on the message.

查看更多
爷的心禁止访问
4楼-- · 2019-01-30 02:56

OO is mostly a mind set. You can program OO in C (if you really want to ... ), and you can perfectly have procedural code in C++/Java; what I mean is, even if you use classes on the surface, it could still be procedural.

The idea behind OO is abstraction of state. Instead of "thinking" in terms of "groupings of data", you "think" in terms of "objects", where an object is an "interface" for "grouping of data and ways to manipulate this data".

It all sounds philosophical, because it is.

There's a lot to say here, and it can't be all said in a small SO post, so I'll leave it here.

UPDATE
As mentioned in Flanagan's answer, OO languages implement constructs that utilize this abstraction.

I mean, you could technically "hack" classes and polymorphism in terms of structs, functions, and function pointers.

Here's an example of OO in C

查看更多
Rolldiameter
5楼-- · 2019-01-30 02:57

You are correct in your observation that object-oriented programs are based in many ways on the procedural paradigm. You are also correct in that syntactically all that really happens is that you invoke functions. In fact, you could implement many features of object oriented languages using procedural mechanisms (e.g., function pointers in C++). You could thus do an object-oriented design and still implement it in a procedural language (e.g., like old C++ compilers did).

The importance of the object oriented paradigm is not as much in the language mechanism as it is in the thinking and design process. In procedural programming the thinking is about operations and breaking those operations down using other operations, grouping them into modules, etc. This means that the data or state falls into a secondary importance. It is like thinking of mathematical operations.

The object oriented paradigm, on the other hand, says that you need to think of state and operations together as an entity, and then design your program as interactions between entities that exchange state and activate operations.

查看更多
6楼-- · 2019-01-30 02:57

[pardon the primer style, it's late and i'm tired]

procedures process data - data in, apply some processing, get data out

sometimes, some of the data elements are related to some of the other data elements, and it's convenient to group them together into a data structure, which can then be manipulated and addressed as a single unit.

now our procedure can take a data structure as input and alter it and/or produce another data structure as output

occasionally we notice that some procedures are only concerned with a certain kind of data structure; it is convenient to group these procedures together with their data structure, and call it an object.

a template for creating objects is called a class; an object is said to be an instance of a class

we may notice that one class is very much like another, so instead of copying and pasting code we let one class inherit from another: the subclass inherits from the superclass or "base class". In this way the subclass has access to all of the data structures and procedures of the superclass, and can augment or override them in certain ways

if we politely request an object to do something for us instead of brutally calling its procedures directly, this is called message passing, even if no actual 'message' is transmitted. The joy here is that many different kinds of objects may understand the same message, which leads to the notion of polymorphism. For example, we can ask many different kinds of documents to Print themselves, and they each respond appropriately.

a language that supports objects (via classes or not) with message passing and inheritance is called object-oriented. If there is no inheritance, the language is merely object-based.

good luck with your studies!

查看更多
唯我独甜
7楼-- · 2019-01-30 02:59

Procedural is part of the procedural/functional/logical (or logic oriented) distinction (compare c, lisp, and prolog) between different ways of describing what a program should do.

Object orientation is orthogonal to this other idea, and describes a means of grouping sub-programs with data. C++ and java are procedural languages with object oriented features; fortran77 is a procedural languages without object oriented features. Common lisp supports object orientation; some older lisps do not. Plain vanilla prolog does not support objects, and I can't name a logic oriented language that does (I don't do logic oriented programming, it is on my list of things to do when I have some copious spare time. I barely do functional programming).

As others have noted, however, proper object oriented thinking changes how you do your programming as much as a switch from procedural to functional.


BTW-- I see "procedural" used a lot to distinguish non-object-oriented procedural languages from their object-oriented brethren, but I think this is a poor usage driven by the lack of a clean adjective for "not object oriented". YMMV.

查看更多
登录 后发表回答