Article No: 2
Object-oriented programming uses a vocabulary that is unfamiliar to the procedural programmer. Let us now briefly examine this vocabulary with regards to the major elements of object-oriented languages.
Objects
An object is the primitive element of a program written in an OOP language. Each object consists of a set of procedures (known as methods) and some data. It stores the data in variables and responds to messages received from other objects by executing when it receives a message that corresponds to that method.
“Objects are the basic run time entities”. They may represent a person, a place, a bank a/c…etc. In structured programming a problem is approached by dividing into functions. Unlike this, in object oriented programming the problem is divided into objects. Thinking in terms of objects rather than functions makes the designing of program easier. Each object contains data, and code to manipulate data. Following are the few candidates that can be treated as objects in different programming situations:
· Employees in a payroll processing system.
· Data structures like linked lists, stacks, queues etc.
· GUI elements like windows, menus, icons etc.
· Hardware devices like disk drives, keyboard, printer etc.
· Various elements in computer games like cannons, guns, animals etc.
· Customers, sales person in a sales tracking system.
· Computers in a network model.
Example:
If customer and account are two objects in a program, then the customer object may send a message to the account object asking about a bank balance.
If student and mark sheet are two objects than student can get data about mark sheet by request.
Classes
Most languages offer primitive data types like int, long and float. Their data representation and response to arithmetic, assignment and relational operators are defined as part of the language. However, the language does not know user-defined data types. The programmer defines its format and behavior by defining a class. For example, there can be a user-defined data type to represent dates. Programmers have to define the behavior of dates by designing a date class. This class express the format of date and the operations that can be performed on it. The way we can declare many variables of the primitive type int, we can define many objects of the date class. A class serves as a blueprint or a plan or a template. It specifies what data and what functions will be included in objects of that class. Defining the class doesn’t create any objects, just as the mere existence of a type int doesn’t create any variables. Thus a class is a collection of objects of similar type.
Fruit mango;
Here fruit is class and mango is an object.
Inheritance
Inheritance is the process by which object of one class gain the properties of objects of another class. 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. Using a concept called inheritance, new classes can be built from the old ones. The new class referred to as a derived class, can inherit the data structures and function of the original, or the base class. The new class can add data elements and functions to those it inherits from its base class.
Example:
If there is one class named “A” it contains some features in it, and now when I want to create any new class named “B” using some features of class A, than i can create the new class and this new class “B” is called derived class and class A is known as base class.
Polymorphism
Polymorphism is another important OOP concept. An operation may show different behaviors in different instances.
Operator overloading
The process of making an operator to show different behaviors in different instances is known as operator overloading. E.g. “<<” can be helped to print output and also it can be used in C for bitwise shift operator.
object insertion variable; cout << x; object extraction variable; cin >> x;
Function overloading
Using a single function name to perform different types of tasks is known as function overloading. E.g. using single function name “ADD()” we can perform many different tasks.
ADD(int,int)
ADD(double,int)
ADD(float, double,int)
Dynamic Binding
Dynamic refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding (also known as late binding) means that the code associated with given procedure call is not known until the time of the call at run-time. It is associated with polymorphism and inheritance.
Message Passing
The mechanism is to support communication between objects is through messages. That is, all computing in a program written in an OOP language is done by sending a message to an object to invoke one of its methods. A reply to a message is an object, which returns the requested information, or simply notifies the sender that the requested processing has been completed.
Objects communicate with one another by sending and receiving information much the same way as people pass message to one another. A message for an object is a request for execution of a procedure, and therefore will call a function (procedure) in the receiving object that generates the desired result.
1. Programming in C++ (By: E Balagurusamy)
Compiled By: Chaudhary Amit V.
Comments
Post a Comment