|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Simplifying Object Oriented ProgrammingObject 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 PrinciplesAll object-oriented programming languages provide mechanisms that help you implement the object-oriented model. They are: Encapsulation
ExamplesC++ // 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
// 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
ExamplesC++ // 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 // 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
ExamplesC++ // 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 // 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 TogetherWhen 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||