Chap-1 Principles of Object Oriented Programming: SR - No. POP OOP
Chap-1 Principles of Object Oriented Programming: SR - No. POP OOP
(1) In POP the primary or main focus is In OOP the primary or main focus is on data
on functions and procedures. instead of procedures.
(2) In POP large programs are divided In OOP programs are divided into an object
into smaller programs known as which is an instance of class.
functions.
(3) Data move openly around the system Data is hidden and cannot be accessed by
from function to function. external functions.
(4) Functions transform data from one Objects may communicate with each other
function to another. through functions.
(5) Mapping between data and function is Mapping between data and function is very
difficult and complicated. easy because it is accessed by using objects.
Objects
Objects are the basic run-time entities in an object-oriented system.
It is an instance of class.
Object can be anything and used to represent a person, a place, a bank account, a table of
data or any item that the program has to handle.
Object reserve a space in memory and have an address like a structure in a C language.
Each object contains data and code to manipulate the data.
Below figure shows the example of representation of an object.
Classes
Class is a collection of objects of similar types.
The entire set of data and code of an object can be made a user define data type with the help
of a class.
For example mango, orange, apple and banana are members of the class fruit.
Classes are user-define data types and behave like the built-in types of a programming
language.
The syntax used to create an object is as similar as create an integer object in c. If fruit has
been defined as a class, then the statement
fruit mango;
Data Encapsulation
Data encapsulation is a mechanism to binding up a data and functions into a single unit.
The data is not accessible to the outside world, only by functions which are bind in the class
can access.
Thus data encapsulation introduces a new OOP concept called data hiding or information
hiding.
Data Abstraction
Inheritance
Inheritance is a process by which object of one class can access the properties of another
class.
In OOP, the concept of inheritance providing the idea of reusability.
Inheritance is the process of creating new classes, which is called derived classes from the
existing classes. These existing classes are often called base classes.
Below example shows the derived classes which are creating from the base class.
In above example Vehicle class has two attributes petrol and diesel, this attributes are
accessed by another two classes Two Wheeler and Four Wheeler which is derived from the
Vehicle class.
Same way Two Wheeler class further divided into Scooter and Motor and Four Wheeler
class is divided into Car and Truck class and access same attributes (properties) of Vehicle
class.
Polymorphism
In OOP polymorphism means that same code of operations or objects behave differently in
different instances.
For example (+) operator in C++:
4+5 Integer Addition
3.14 + 2.0 Floating Point Addition
S1 + abc String Concatenation
In C++, that type of polymorphism is called overloading means thing used for different
purpose.
If we achieve polymorphism through function, then it is called function overloading. If we
use operator for achieving polymorphism, then it is called operator overloading.
A polymorphism are two types:
(i) Compile time polymorphism
(ii) Run-Time polymorphism
Below figure shows the polymorphism using function.
Dynamic Binding
In OOP dynamic binding refers to the linking a procedure call to the code that will be
executed only at run time.
Dynamic binding also known as late binding. It is one type of run time polymorphism.
In run time polymorphism, the selection of appropriate functions is called at run time not at
compile time.
The dynamic binding is achieved with virtual function.
Message Passing
An OOP consists of a set of object that communicates with each other through member
functions.
The objects communicate with each other by sending and receiving information same way
as people pass the message to one another.
Message passing invokes specifying the name of the object, the name of the function and the
information to be sent as shown in below example.
Emp.Salary (name);
Object Information
Message
In above example Emp is a object of class, Salary is the message of function to do and the
name is the information required.
Data Hiding: The principle of data hiding helps the programmer to build the secure
programs that cannot be accessed by the code in other parts of the program.
Inheritance: Through inheritance, we can eliminate redundant code and extend the use of
existing classes. So the idea of reusability can be provided.
Polymorphism: Using polymorphism we can achieve function overloading and operator
overloading which helps to do same thing using different instances.
Dynamic Binding: Using dynamic binding, we can select appropriate function at run time.
Message Passing: Technique of message passing for communication between objects makes
the system much simpler.
OOP can easily upgrade from small to large systems.
It is easy to partition the work based on objects, means different objects have different data
stored in different partitioned in memory, so problem of data loss can be solved.
Using OOP concepts software complexity can be easily managed.
Applications of OOP
OOP used in complex system and make it easier and simple to manage. The application of
OOP include:
Real-time systems
Object-Oriented Databases
What is C++
Application of C++
Below figure shows the typical structure of C++ program that contain four sections.
It is a common structure of C++ program. The class declarations are placed in a header file
and the definitions of member functions go into another file.
This approach enables the programmers to separate the abstract specification of the interface
(Class Definition) from the implementation details (Member Functions Definition).
Finally, the main program that uses as a third file which includes the previous two files as
well as any other files.
In C++ for the input and output there are some predefine operator and some predefine
stream objects are available.
INPUT OPERATOR
OUTPUT OPERATOR
The multiple use of << and >> operator in one statement is called cascading.
Lets start with a simple C++ Program that prints a string on the screen.
Line1 of program indicate that we include iostream file into our program.
IO stands for Input / Output and h stands for header.
The file contains the declaration for the predefine iostream objects like cin and cout for input
/ output operations.
Line2 of program contain main () function. In C++, main () returns an integer type value to
the operating system. Therefore, every main () in C++ should end with a return (0)
statement; otherwise a warning or an error might occur.
Line3 prints the string Hello World ! on the screen using output operator << .
Line4 indicate return type of main () function which return 0 value.
Like C, the C++ program is a collection of functions. In above example only one function
main () is used.
As usual, execution begins at main ().
Every C++ programs must have a main ().
Like C, C++ statements terminate with semicolons.
Comments in C++
It starts with a slash followed by asterisk sign /* and end with asterisk and slash */
Example: /* C++ is an Object-Oriented Programming Language and Superset of C
Language */
The compiling and linking of C++ programs depends upon the operating system. A few
popular editors for C++ programs are:
Turbo C++
Borland C++
Visual C++
Dev C++
Introduction of namespace