1. Home
  2. Computing & Technology
  3. C / C++ / C#

C# Tutorial - An Object Oriented Approach to Programming

By , About.com Guide

1 of 10

An Object Oriented Approach to Programming

In the previous lesson we had a quick look at C#. This time, we will be going more into Object Oriented Programming in a lot more depth. We'll start with a very quick introduction to OOP. If you want a bit more detail on Object Oriented Programming, read the article Objects are a more natural way to develop applications because they can model the problem domain and help reduce complexity. Before OOP came along, the popular programming style was procedural. This is how C programs are written (and some C++). This means that data and code are separated. Let me explain by way of a simple application, a car simulation.

Let say we want to model a car travelling along a road. This is a very simple model. Our road is a list of vectors- each a distance plus a speed limit. Our car has a location - which is the road segment plus the position along that segment. It also has a speed.

Now if we were modelling this procedurally we'd store the list perhaps in an array of structs (in C/C++), perhaps have a car struct with data and a bunch of procedures to move the car along.- with names like

  • EnterRoadSegment( int RoadSegment);
  • MoveCarAlongRoadSegment(struct * car,int RoadSegment);
  • SetCarSpeed(Struct * car,int NewSpeed);
  • IsCarAtRoadSegmentEnd(struct * car,int RodSegment);
This doesn't make for very flexible software. What if we reach a junction or try to go the wrong way up a one way street?

With OOP though we create classes

  • For the car
  • For a road segment
  • For the Simulation that holds the other classes.
We'd then write methods for the car, perhaps
  • Travel();
  • IncreaseSpeed(int NewSpeed);

On the Next Page : Creating the Classes

Explore C / C++ / C#
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C# / C Sharp
  5. Learn C Sharp
  6. C# Tutorial - An Object Oriented Approach to Programming>

©2009 About.com, a part of The New York Times Company.

All rights reserved.