tags:

views:

1332

answers:

3

I've done a little bit of object oriented stuff before, mainly in C++, and have just started programming in C#. I'd like to do a small OOP test project to just get my head round it again and find out how to do OOP stuff in C#, but I can't think of any ideas for a relatively simple first project.

I'd like something that includes things like inheritance if possible, but something that will produce a semi-useful program at the end (rather than just an interface to a textbook example of OOP).

Any ideas?

+2  A: 

Rather than looking for an 'OO problem', try doing something you would normally do procedurally, using an OO pattern instead.

Examples:

  • any simple program that takes a variety of commands: make each command its own class, use polymorphism to work with them.
  • something with a state machine: instead of using a switch statement to traverse states, make a set of state classes, containing a method which performs logic and returns the next state object.

Both of these are classic GOF design patterns. If you know how to do something procedurally, and don't know how to tackle it with objects, the Design Patterns book will usually show you the way.

slim
A: 

A good book on object oriented design patterns. A classic is 'Design Patterns: Elements of Reusable Object-Oriented Software' of Gamma et al.

By the way, inheritence is a key concept of Object Oriented Programming - still you should use little as possible. When you think you need inheritence: try to find an alternative. Usually you don't need it. The listed book can help finding alternatives.

Gerbrand
While you shouldn't force inheritance onto a problem, don't run away from it when it fits.
slim
+5  A: 

I'd like to suggest a chess program:

  • Each type of piece (e.g. pawn, queen, ...) should be a different class
  • These classes should inherit from a common base class
    • All types of piece have similar concrete properties, for example a position on the board, and a colour (but, it's up to you to decide things like whether a piece's position on the board is a property of the piece, and/or a property of the board)
    • All types of piece have similar abstract behaviour, for example they can be moved (although in different ways)

To make it simpler, don't try to make a chess program which can play to win: instead, just one which can tell you all the legal moves given any set of pieces on the board.

ChrisW