Object Oriented Programming – Theory and Examples

What is Object Oriented Programming?

Object Oriented Programming (OOP) is commonly used in programs which require doing the same things over and over again. Much like a function or method can be ran multiple times, OOP classes can be ran multiple times, simultaneously.

When is OOP used?

OOP is used in games and some other software. The reason it is used in games is because the game requires the same things to happen over and over again at the same time, for example…

Example (Theory):
In games there are entities (characters, animals, keys, doors, etc). Each entity has its own class with it’s own variables which is where OOP comes in to play. Say if we had one class which controlled the doors in our map, the objective of the game is to get the specific keys for each door. If we just had one class handling all of our doors, once we got a key it would set the door variable “open” to true and it would open all of the doors in our map. Whereas if we use OOP to control the doors in our map, we still only have one class but all the variables in the class only refer to a single door. Therefore giving us the ability to control seperate doors, with the same amount (or less) of code than that without using OOP.

What is an Object?

An object can be considered a “thing” that can perform a set of related activities. The set of activities that the object performs defines the object’s behavior. For example, the hand can grip something or a Student (object) can give the name or address.

In pure OOP terms an object is an instance of a class.

For more examples and projects on C and C++, see C/C++ Projects and Examples Source Codes

What is a Class?

class.gif

class is simply a representation of a type of object. It is the blueprint/ plan/ template that describe the details of anobject. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations.

public class Student
{
}

According to the sample given below we can say that the student object, named objectStudent, has created out of theStudent class.

Student objectStudent = new Student();

In real world, you’ll often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles.

In the software world, though you may not have realized it, you have already used classes. For example, the TextBoxcontrol, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.

Features of Object-Oriented Programming Languages

There is no formal definition of object-oriented programming. Hence there is some confusion surrounding what features a programming language must support in order to claim that it is object-oriented. Despite this, however, most agree that in order for a language to claim that it is object-oriented, it must provide support for three major concepts, as inferred from Booch’s definition.

The important features of Object Oriented programming are:

  • Inheritance
  • Polymorphism
  • Data Hiding
  • Encapsulation
  • Overloading
  • Reusability

Let us see a brief overview of these important features of Object Oriented programming

But before that it is important to know some new terminologies used in Object Oriented programming namely

  • Objects
  • Classes

Objects:

In other words object is an instance of a class.

Classes:

These contain data and functions bundled together under a unit. In other words class is a collection of similar objects. When we define a class it just creates template or Skelton. So no memory is created when class is created. Memory is occupied only by object.

Example:

Class classname
{
Data
Functions
};
main ( )
{
classname objectname1,objectname2,..;
}

In other words classes acts as data types for objects.

Member functions:

The functions defined inside the class as above are called member functions.
Here the concept of Data Hiding figures

Data Hiding:

This concept is the main heart of an Object oriented programming. The data is hidden inside the class by declaring it as private inside the class. When data or functions are defined as private it can be accessed only by the class in which it is defined. When data or functions are defined as public then it can be accessed anywhere outside the class. Object Oriented programming gives importance to protecting data which in any system. This is done by declaring data as private and making it accessible only to the class in which it is defined. This concept is called data hiding. But one can keep member functions as public.

So above class structure becomes

Example:

   Class classname
{
private:
datatype data;

public:
Member functions
};
main ( )
{
classname objectname1,objectname2,..;
}

Encapsulation:

The technical term for combining data and functions together as a bundle is encapsulation.

Inheritance:

Inheritance as the name suggests is the concept of inheriting or deriving properties of an exiting class to get new class or classes. In other words we may have common features or characteristics that may be needed by number of classes. So those features can be placed in a common tree class called base class and the other classes which have these charaterisics can take the tree class and define only the new things that they have on their own in their classes. These classes are called derived class. The main advantage of using this concept of inheritance in Object oriented programming is it helps in reducing the code size since the common characteristic is placed separately called as base class and it is just referred in the derived class. This provide the users the important usage of terminology called as reusability

Reusability:

This usage is achieved by the above explained terminology called as inheritance. Reusability is nothing but re- usage of structure without changing the existing one but adding new features or characteristics to it. It is very much needed for any programmers in different situations. Reusability gives the following advantages to users

It helps in reducing the code size since classes can be just derived from existing one and one need to add only the new features and it helps users to save their time.

For instance if there is a class defined to draw different graphical figures say there is a user who want to draw graphical figure and also add the features of adding color to the graphical figure. In this scenario instead of defining a class to draw a graphical figure and coloring it what the user can do is make use of the existing class for drawing graphical figure by deriving the class and add new feature to the derived class namely add the feature of adding colors to the graphical figure.

Polymorphism and Overloading:

Poly refers many. So Polymorphism as the name suggests is a certain item appearing in different forms or ways. That is making a function or operator to act in different forms depending on the place they are present is called Polymorphism. Overloading is a kind of polymorphism. In other words say for instance we know that +, – operate on integer data type and is used to perform arithmetic additions and subtractions. But operator overloading is one in which we define new operations to these operators and make them operate on different data types in other words overloading the existing functionality with new one. This is a very important feature of object oriented programming methodology which extended the handling of data type and operations.
Thus the above given important features of object oriented programming among the numerous features it have gives the following advantages to the programming world. The advantages are namely:

  • Data Protection or security of data is achieved by concept of data hiding
  • Reduces program size and saves time by the concept of reusability which is achieved by the terminology of Inheritance
  • Operators can be given new functions as per user which extends the usage.

Additional Features from Wiki

  • Encapsulation enforces modularity - Encapsulation refers to the creation of self-contained modules that bind processing functions to the data. These user-defined data types are called “classes”, and one instance of a class is an “object”. For example, in a payroll system, a class could be Manager, and Pat and Jan could be two instances (two objects) of the Manager class. Encapsulation ensures good code modularity, which keeps routines separate and less prone to conflict with each other.
  • Inheritance passes “knowledge” down –  Classes are created in hierarchies, and inheritance lets the structure and methods in one class pass down the hierarchy. That means less programming is required when adding functions to complex systems. If a step is added at the bottom of a hierarchy, only the processing and data associated with that unique step must be added. Everything else above that step is inherited. The ability to reuse existing objects is considered a major advantage of object technology.
  • Polymorphism takes any shape – Object-oriented programming lets programmers create procedures for objects whose exact type is not known until runtime. For example, a screen cursor may change its shape from an arrow to a line depending on the program mode. The routine to move the cursor on screen in response to mouse movement can be written for “cursor”, and polymorphism lets that cursor take simulating system behaviour.

References for Object Oriented Programming – Theory and Examples were taken from:

codeproject.com

wikipedia.com

stackoverflow.com

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>