|


Answers
Answer from TaradinoC
2 people found this helpful

Structured programming is task-centric, object oriented programming is data-centric.


Task-centric vs. Data-centric

Structured programming is based around data structures and subroutines. The subroutines are where stuff actually "happens", and the data structures are simply containers for the information needed by those subroutines.

Object oriented programming, on the other hand, shifts your primary focus to the data itself. Instead of asking "what do I want to do and what will I need to know to do it", you ask "what kind of things do I want to have and what can those things do for me". Instead of designing your functions first and then coming up with data structures to support them, you design types first and then come up with the operations needed to work with them.

Three OOP Principles

Perhaps the most important feature of OOP is polymorphism, the ability to identify certain aspects that several data types have in common, and write code that works equally well with all of them by ignoring the differences in situations where they don’t matter.

For example, consider a simple drawing program where you have a set of shapes (circles, rectangles, etc.) that share certain things in common (they all have a location, size, and color) but are different in other ways (how they look or whether they can be rotated). In a structured program, you’d write a function to draw a shape, containing logic like "if the shape is a circle, do ABC; if it’s a rectangle, do XYZ" and so on.

But in an OO program, you’d simply tell the shape to draw itself, and the shape would know, based on its own type, what to do: you write a specialized drawing function when you define each shape, and when you send a "draw" message to any shape, it automatically calls the one for the correct shape type. Polymorphism eliminates the need for you to check what kind of shape it is: you just have to know that shapes can draw themselves, and let the shape worry about how it actually happens.

Another important OO principle is encapsulation, the ability to bundle code and data together in one place and hide that data from the outside world, forcing anyone who wants to access it to go through the associated code. For example, all shapes have a location and a size, but the best representation might be different. A circle only needs three numbers (center X, center Y, and radius) but a rectangle needs four (top, bottom, left, right). Structured programming encourages code everywhere to deal directly with the innards of data structures, so most likely you’d need to use the same representation for all shapes in order to avoid checking the type every time you wanted to measure a shape, even though that representation is wasteful for circles.

Object oriented programming addresses that problem two ways: first, encapsulation says that the internal representation of a shape is off-limits to anyone else, so if you want to know how big a shape is, you have to call its getSize() method instead of reading its size directly out of memory. Second, polymorphism allows different shapes to implement their getSize() methods differently, allowing circles to use a more efficient version while presenting the same interface to the outside world.

Finally, there’s inheritance, which makes it easy to extend existing structures to produce new structures with slightly different behavior. For example, a filled circle is mostly the same as a regular circle, but it draws itself differently and also has a fill color. In a structured program, you’d probably handle filled circles by adding a fill color to all shapes and a flag that indicates whether the shape is filled or not, and the fill color would simply go unused (wasting memory) in unfilled shapes. In an object-oriented program, you can make FilledCircle a subclass of Circle, inheriting all the existing circle behavior, and then replace the draw() method and add a place to store the fill color. Then if you changed something about Circle later, the change would automatically propagate to FilledCircle, while changes you made to FilledCircle would not affect any other shapes.

Design vs. Language

Whether your code is object oriented or merely structured depends partly on your choice of language, but also on your design. For example, the C language doesn’t offer any features for object oriented programming, but with enough discipline you can still write object-oriented code in C, such as the GTK windowing library. On the other hand, you can write a Java program that completely fails to take advantage of Java’s OOP features, by putting all your code in a single class and using classes with public members just as you’d use structs in C.

 

Answer from newbie2552
2 people found this helpful

One organizes code by comprehensiveness while the other organizes code by the data affected.


Structured programming consists of breaking big problems into smaller problems, then further breaking those into still smaller problems, and so on, until a level of such simplicity is reached that the implementation is obvious to the programmer expected to do the coding.  Object-oriented programming consists of grouping code with the data on which it operates so that this "object" can function independently of the rest of the software system.  Structured programming and object-oriented programming are not mutually exclusive.  You can structure the code in an object, and you can use objects to implement the modules of code in a structured program.
Sources: personal experience

 

Answer from newbie2305861
1 people found this helpful

Procedural vs. OO programming require different approaches


Similarities: Both require a rudimentary understanding of programming concepts and basic control flow. Loops, conditional statements, and variables are concepts that are important whether you are using a procedural language or an object oriented language.

Differences: Typically object oriented is viewed as more difficult. This is because an entirely different problem solving approach must be taking. In addition, there are a variety of object-oriented-only concepts such as classes and inheritance. For simple programs, procedural is often preferred. The more complicated the project, the easier it is to leverage the strengths of object oriented design.

Other notes: Not all languages fall strictly into procedural or object oriented baskets. In actuality, it is more of a spectrum. Languages like Basic and C are pretty much entirely procedural. Languages like C++ and Pascal can be written in either procedural or object oriented styles. Languages like Java and Python adhere much more strictly to object oriented design (although some programmers argue these aren't TRUE object oriented languages).
Sources: My Opinion

 

 

Comments on this question:

Wow, this really sounds like a computer science take-home exam question.

Is it?


 

 
Not at all. I made the question up hoping to gain a better understanding of the two types.