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?
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.
This might be a bit simple for your purposes but the Hello World Collection is always fun to look through.
Take a look at The Computer Language Benchmarks Game. It's got implementations of various programs in just about every language you could imagine.
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.
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.
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.
Here are two programs that implement n-body
What differences do you find between them?
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.