Skip to main content

Basic Concepts of OOP

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.


Resource Used:

1. Programming in C++ (By: E Balagurusamy)

Compiled By: Chaudhary Amit V.

Comments

Popular posts from this blog

Characteristics of a Good Programming Language

Till now there are many high level languages which are very popular, and there are others, which could not become so popular in-spite of being very powerful. There might be many reasons for the success of a language, but one obvious reason is the characteristics of the language. Several characteristics believed to be important with respect to making a programming language good are briefly discussed below. Simplicity A good programming language must be simple and easy to learn and use. For example, BASIC is liked by many programmers only because of its simplicity. Thus, a good programming language should provide a programmer with a clear, simple and unified set of concepts which can be easily grasped. It is also easy to develop and implement a compiler or an interpreter for a programming language that is simple. However, the power needed for the language should not be sacrificed for simplicity. The overall simplicity of a programming language strongly affects the readability of the pr...

Angular 4 and Firebase Authentication: Setup

If you have come to this article, it means you are keen to learn new technology and that too Firebase. I love Firebase because it provides all basic but important features of any web application. Take for example, authentication, which is cumbersome and risky if not implemented with utmost care. And when we have the integration of Angular and Firebase, it becomes a lot easier for a developer to build such crucial modules in less time with minimal efforts. In this article we will create a simple Angular application using Firebase. I am going ahead with the understanding that you know the benefits of Firebase and have little knowledge about it. There are plenty of posts out on the web if you are behind. Step 1: Install Angular CLI (if not installed). npm install -g angular-cli Step 2: Create new Angular 4 project. By default now angular CLI will create Angular 4 project so you need not fret. ng new firebase-authentication Step 3: Check whether the ne...

Switch-case, simple calculator . . .

11.      Write a program to perform some mathematic functions with the help of “switch-case” statement. #include<stdio.h> #include<conio.h> void main() { int a,b,ans; char c; clrscr(); printf("\ Enter a and b:"); scanf("%d%d",&a,&b); printf("\n Enter operator:"); fflush(stdin); scanf("%c",&c); switch(c) { case'+':ans=a+b;       break; case'-':ans=a-b;       break; case'*':ans=a*b;       break; case'/':ans=a/b;       break; } printf("\n ans %d",ans); getch(); } 12.      Print the Fibonacci series #include<stdio.h> #include<conio.h> void main() { int a=1,b=1,s=3,c,ch; clrscr(); printf("how many steps="); scanf("%d",&ch); printf("%5d%5d",a,b); for(s=3;s<=ch;s++) {       c=a+b;       printf("%5d",c);       a=b;    ...