Click here to Skip to main content
15,888,271 members
Articles / Programming Languages / C++
Article

Simplifying Object Oriented Programming

Rate me:
Please Sign up or sign in to vote.
2.67/5 (6 votes)
2 Oct 20014 min read 82.5K   27   6
Simplifying Object Oriented Programming

Simplifying Object Oriented Programming

Object Oriented Programming is the most dramatic innovation in software development in the last decade. It ranks in importance with the development of the higher-level languages at the dawn of the computer age. Sooner or later every programmer will be affected by the object oriented approach to program design. An object is a self-contained element of a computer program that represents a related group of features and is designed to accomplish specific tasks. Objects also called instances. Each objects has specific role in a program, and all objects can work with other objects in defined ways.

The fundamental idea behind object-oriented language languages is to combine into a single unit both data and the functions that operate on the data. Such a unit is called an object. An object's functions, called member functions in C++, typically provide the only way to access its data. If you want to read a data item an object. It will read a data and return the value to you. You can't access the data directly. The data is hidden, so it is safe from accidental alteration. Data and its functions are said to be encapsulated into a single entity. Data encapsulation and data hiding are key termed in the description of object-oriented languages. Objects-oriented programming is modeled in the observation that in the real world, objects are, made up of many kinds of smaller objects. However, the capability to combine objects is only a general aspect of object-oriented programming. It also includes concepts and features that make the creation and use of objects easier and more flexible. The most important of these feature is the class.

A class is a template used to create multiple objects with similar features. Classes embody all features of a particular set of objects. When you write a program in an object-oriented language. You don't define individual objects. Instead, you define classes of objects.

The Three OOP Principles

All object-oriented programming languages provide mechanisms that help you implement the object-oriented model.

They are:

Encapsulation

Encapsulation is the mechanism that binds together code and the data if manipulates, and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. Conclusion: The wrapping up of data and methods into a single unit (called class) is known as encapsulation.

Examples

C++

// listing incap.cpp
#include<iostream.h>
#include<conio.h>
class one   // declare a class
{
	private:
	int a;
	public:
	void printdata()
{
	a = 7;
	cout<<"Value of a is "<<a;
}
};
void main()
{
	clrscr();
	one b;   //declare a object for the class one
	cout<<"Now I am in main function and going to call a function from class one";
	b.printdata();   // calling function from class one
	getch();
}

Java

Java
// listing incap.java
class one
{
	int a;
	void printdata()
{
	a = 7;
	System.out.println("Value of a is " + a);
}
}
class incap
	public static void main(String[] args)
	{
		one b = new one();   // declare a object for class one
		System.out.println("Now I am in main function and going to " + 
                                   "call a function from class one");
		b.printdata();   // calling function from class one
	}
}

Inheritance

Inheritance is the process by which object of one class acquires the properties of another class. Inheritance supports the concept of hierarchical classification. For example, the atlas is a part of class bicycle, which is again a part of the class cycle. As illustrated in the principal behind this sort of division is that each derived class shares common characteristics with the class from which it is derived. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible deriving a new class from the existing one. The new class will have the combined features of both the classes. Thus the real appeal and power of the inheritance mechanism is that allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class is such a way that is does not introduce any undesirable side effects into the rest of the class. The drive class is knows as 'subclass'.

Examples

C++

// listing helloworld.cpp
#include<iostream.h>
#include<conio.h>
class one   // declare class one
{
	public:
	void test()
	{
		cout<<"Hello from class one";
	}
};
class two : public one   // inherit class two by class one
{
	public:
	void test()
	{
		cout<<"Hello from class two";
		one::test();   // calling function from class one
	}
};
void main()
{
	clrscr();
	two t;   // declare a object for class two
	cout<<"Hello from main function";
	t.test();   // calling function from class two
	getch();
}

Java

Java
// listing dasForm.java
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class dasForm extends JFrame // inherite by JFrame
{

 public dasForm()
  {
   super("Shankar Das"); // set the application title
   }

 public static void main(String[] args)
  {
   JFrame frame = new dasForm(); // declare a new frame
		// windows common property
   WindowListener w = new WindowAdapter() 
    {
       public void windowClosing(WindowEvent e)
        {
           System.exit(0);
           }
        };

    frame.addWindowListener(w); // sets winwods common property
    frame.pack();
    frame.setVisible(true);
    frame.setSize(180,80);

   }
 }

Polymorphism

Polymorphism is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the solution. Polymorphism means the ability to take more than one form. For example, consider the operation of addition. For two numbers, the operation will generate the sum. If the operands are three numbers, then the operation would produce the product of them. That a single function name can be used to handle different number and different arguments. This is something similar to a particular word having several different meanings depending on the context. Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ. Polymorphism is extensively used in implementing inheritance.

Examples

C++

// listing polymor.cpp
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
class poly
{
 private:
  int gd,gm;
 public:
   void graph()   // for initialize graphics
    {
     gd=DETECT;
     initgraph(&gd,&gm,"");
     }
   void draw(int a)   // function with one args
    {
     cleardevice();
     circle(getmaxx()/2,getmaxy()/2,a);
     getch();
    }
   void draw(int a, int b)    // function with two args
    {
      cleardevice();
      rectangle(getmaxx()/2-a/2,getmaxy()/2-b/2,
                getmaxx()/2+a/2,getmaxy()/2+b/2);
      getch();
     }
 };

void main()
{
  clrscr();
  poly p;
  p.graph();   // initialize graphics
  p.draw(50);   // calling one args function
  p.draw(50,50);   // calling two args function
  closegraph();   // closing graphics
 }

Java

Java
// listing polymor.java
class plm
{
 void hello(String s)   // function with one args
 {
   System.out.println("My name is " + s);
   }
 void hello(String s, int ag)   // function with two args
 {
    System.out.println("My name is " + s + " & Age is " + ag);
    }
 void hello(String s, int ag, String ct)   // function with three args
 {
    System.out.println("My name is " + s + " & age is " + ag);
    System.out.println("I am from " + ct + " city");
    }
 }

class polymor
{
  public static void main(String[] agrs)
  {
    plm p = new plm();   // declaring a object for class plm
    p.hello("Shankar Das");   // calling one args function
    p.hello("Shankar Das",25);   // calling two args function
    p.hello("Shankar Das",25,"Bhopal");   // calling three args
    }
  }

Encapsulation, Inheritance and Polymorphism Work Together

When properly applied polymorphism, encapsulation and inheritance combine to produce a programming environment that support the development of far more robust and scaleable programs than does the process-oriented model. A well-designed hierarchy of classes is the basis for reusing the code in which you have invested time and effort developing and testing. Encapsulation allows you to migrate your implementations over time without breaking the code that depends on the public interface of your classes. Polymorphism allows you to create clean, sensible, readable and resilient code.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy on code-level..? Pin
AlexMarbus6-Jun-02 9:24
AlexMarbus6-Jun-02 9:24 
.. while the kewl thing about OOP is, that you should be able to teach it without using code as an example. (That's kinda why UML)

Good article, tho

--
Alex Marbus
www.marbus.net
But then again, I could be wrong.
GeneralSome minor issues... Pin
Stan Shannon3-Oct-01 11:29
Stan Shannon3-Oct-01 11:29 
GeneralRe: Some minor issues... Pin
3-Oct-01 16:47
suss3-Oct-01 16:47 
GeneralRe: Some minor issues... Pin
Stan Shannon4-Oct-01 2:49
Stan Shannon4-Oct-01 2:49 
GeneralRe: Some minor issues... Pin
George3-Oct-01 19:27
George3-Oct-01 19:27 
GeneralRe: Some minor issues... Pin
24-Feb-02 10:51
suss24-Feb-02 10:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.