You are here:About>Computing & Technology>C / C++ / C#> Getting Started> Introduction to Object Oriented Programming
About.comC / C++ / C#
Suggested Reading

Learn CLearn C++Learn C#

Introduction to Object Oriented Programming

From David Bolton,
Your Guide to C / C++ / C#.
FREE Newsletter. Sign Up Now!

An Overview of OOP

Imagine that we are writing an application to model a simple television. Our TV has the following controls that we need to handle.

  • On/Off switch
  • Volume Control - a number 1-10.
  • Channel Selector

Now in programming this, there are two ways to model a TV - the procedural approach in languages like C or Basic or the more modern OOP approach in C++, C# or Java.

In the procedural approach we use variables to hold the TVs 'values'. A boolean on/off value, an integer number for the volume control and another for the channel. These values are accessible anywhere in our source code where it accesses the TVs variables. Its easy to introduce subtle bugs such as too large a number in the volume control or writing to the wrong variable.

Think of the procedural approach as being as if our TV had no casing and that you had to use a screwdriver to change the volume or channel settings. It would be very dangerous! One slip and you might short something out. Accidentally changing a TV variable somewhere in your program could stop your program!

With OOP, the controls are wrapped up inside a TV object and the only way to change them is by calling 'accessor' functions. Our TV now has a 'case' and you can only change the settings by toggling the on/off switch or changing the volume and channel controls. That's much safer to operate. This is known as encapsulation - putting everything inside a 'case' and limiting access.

Our TV object has the following members. This is what it would look like in C++ syntax.

// private data, not publicly accessible

bool OnOff;
int volume;
int channel;

// accessor methods

SwitchOn() ;
SwitchOff() ;
SetVolume( int volume ) ;
SetChannel( int channel) ;
int GetVolume() ;
int GetChannel() ;

Suggested Reading

Learn CLearn C++Learn C#

 All Topics | Email Article | Print this Page | |
Advertising Info | News & Events | Work at About | SiteMap | Reprints | HelpOur Story | Be a Guide
User Agreement | Ethics Policy | Patent Info. | Privacy Policy©2008 About, Inc., A part of The New York Times Company. All rights reserved.