views:

585

answers:

10

As a way to understand the differences between OOP and Procedural languages I was looking for a sample program written in C and C++ or C# or Java. I just want to see the different approaches to the same problem to help me get a sense of the real differences. Does anyone know where I can find a tutorial like this?

+4  A: 

You can always look at Project Euler. People solves the same problems in a many different languages. Most people will post their solutions that you can access after you solve the problem also.

Web
This won't really show differences between OOP and Procedural approaches. Most of the people who do the problems in Java just write a few lines in a main method. These aren't really the kinds of problems that involve a lot of... well... objects.
Adam Jaskiewicz
but it's still interesting!
kotlinski
+1  A: 

This might be a bit simple for your purposes but the Hello World Collection is always fun to look through.

Chris Pebble
+5  A: 

I recommend the 99 bottles of beer website

dsm
+4  A: 

Take a look at The Computer Language Benchmarks Game. It's got implementations of various programs in just about every language you could imagine.

Sean
+1  A: 

Rosetta Code has a wealth of data but very little of it is related to the procedural/object-oriented distinction. You should also see their collection of related sites.

+1  A: 

Black Scholes in multiple languages has plenty of implementations of the Black-Scholes formula. The formula is implemented in Objective-C/iPhone, F#, Autoit, Fortress, Lua, APL, SAS, Mathcad, J, MEL, Postscript, VB.NET, Clean, Ruby, Lisp, Prolog, PL/SQL, LyME, ColdFusion, K, C#, HP48, Transact SQL, O'Caml, Rebol, Real Basic, Icon, Squeak, Haskell, JAVA , JavaScript, VBA, C++, Perl, Maple, Mathematica, Matlab, S-Plus, IDL, Pascal, Python, Fortran, Scheme, PHP, GNU, gnuplot.

weismat
+8  A: 

I don't think this is likely to teach you much. The program has to have a certain size before the differences between different programming paradigms really show. And people aren't likely to write identical copies if the same program in different languages unless the program is trivial.

Most real-life examples would also be polluted with a lot of extra noise, things that can be done within the standard library of one language, but requires third-party libraries in another. And the programmer who wrote it may be more familiar with one language than another, so his implementation in some languages isn't representative of how it "should" be done.

You're more likely to learn the difference between these paradigms the usual way. By learning what each means, and how to use it.

jalf
This is by far the best answer. A program to print "Hello world" or to find the sum of every other prime below two thousand isn't going to teach you the difference between procedural and OO.
Adam Jaskiewicz
A: 

Somebody posted Evil Walrus / ReFactory on Reddit the other day:

http://www.refactory.org/

endian
A: 

Here are two programs that implement n-body

Java implementation

C implementation

What differences do you find between them?

igouy
A: 

Consider the implementation of a snakes and ladders games

In a procedural design we might write a function like

function move(int n) {
    pos += n;
    switch(pos) {
        case 6: pos = 10; break;
        case 12: pos = 4; break;
        case 15: pos = 32; break;
        case 16: pos = 8; break;

        // ...

    }
 }

in an object design language we would create a linked list of Square instances, with some Ladder and Snake instances that branch to other squares.

class Square
  def initialize(next)
    @tokens = []
    @next = next
  end
  def next(n)
    n == 0 ? self : next.next(n-1)
  end
  def move(token,n)
    tokens.remove(token)
    target = self.next(n)
    target.tokens << token
  end
end

class SnakeOrLadder < Square
  def initialize(next,branch)
    super(next)
    @branch = branch
  end
  def next(n)
    # goes to branch when landing on this square!
    n == 0 ? @branch : next.next(n-1)
  end
end

as you can see, we implement the game rules in the objects as well as the way they are composed (rather than in a switch case statement). This has the advantage that

  • it is simple to add new game rules at development time, you'll just write a new subclass of Square
  • it is simple to change the layout of the game at runtime (might sound strange for a game, but for your average business app, this is what you want)

this flexibility is what makes OO so powerful.

Adrian