OOP-Unit 1
OOP-Unit 1
Concept of OOPS
UNIT – 1: Concept of OOPS
Divided Into In POP, program is divided into small In OOP, program is divided into parts
parts called functions. called objects.
Importance In POP, Importance is not given In OOP, Importance is given to the data
to data but to functions as well rather than procedures or functions
as sequence of actions to be done. because it works as a real world.
Approach POP follows Top Down approach. OOP follows Bottom Up approach.
AccessSpecifies POP does not have any access OOP has access specifiesnamed Public,
specified. Private, Protected, etc.
Data Moving In POP, Data can move freely from In OOP, objects can move and
function to function in the system. communicate with each other through
member functions.
Expansion To add new data and function in POP OOP provides an easy way to add new
is not so easy. data and function.
Data Access In POP, Most function uses Global In OOP, datacannotmove easily from
data for sharing that can be accessed function tofunction, itcan be kept public
freely from function to function in the or private so we can control the access of
system. data.
Data Hiding POP does not have any proper way OOP provides Data Hiding so
for hiding data so it is less secure. provides more security.
Examples Example of POP is:C, VB, Example of OOP is:C++, JAVA, VB.NET,
FORTRAN,and Pascal. C#.NET.
2
UNIT – 1: Concept of OOPS
C++ Language offers its users a variety of functions, one of which is included in header
files. In C++, all the header files may or may not end with the “.h” extension.
It offers the features like library functions, data types, macros, etc by importing them into the
program with the help of a preprocessor directive “#include”. These preprocessor directives are
used to instruct the compiler that these files need to be processed before compilation.
Header
File Description
<iostream> It is used as a stream of Input and Output using cin and cout.
It is used to control the data to read from a file as an input and data
<fstream>
to write into the file as an output.
1. Primitive Data Types: These data types are built-in or predefined data types and can be used
directly by the user to declare variables. example: int, char, float etc.
Integer: The keyword used for integer data types is int. Integers typically require 2 bytes of
memory space and range from -2147483648 to 2147483647.
Boolean: Boolean data type is used for storing Boolean or logical values. A Boolean variable can
store either true or false. The keyword used for the Boolean data type is bool.
Floating Point: Floating Point data type is used for storing single-precision floating-point values
or decimal values. The keyword used for the floating-point data type is float. Float variables
typically require 4 bytes of memory space.
3
UNIT – 1: Concept of OOPS
Double Floating Point: Double Floating Point data type is used for storing double-precision
floating-point values or decimal values. The keyword used for the double floating-point data
type is double. Double variables typically require 8 bytes of memory space.
void: Void means without any value. void data type represents a valueless entity. A void data
type is used for those function which does not return a value.
1.4.2 Pointer to character Array: when the character pointer is used, cout prints the
complete array of characters (till it encounters a null character) instead of printing the
base address of the character array.
#include <iostream>
using namespace std;
int main()
{
// Character array
char ch[] = "abc";
4
UNIT – 1: Concept of OOPS
return 0;
}
Objects
Objects are the basic run time entities in an object-oriented system. They may represent
a person, a place, a bank account, a table of data or any item that the program has to handle.
They may also represent user-defined data such as vectors, time and lists.
Programming problem is analyzed in term of objects and the nature of communication
between them. Program objects should be chosen such that they match closely with the real-
world objects.
Objects take up space in the memory and have an associated address like a record in
Pascal, or a structure in c.
When a program is executed, the objects interact by sending messages to one another.
For example, if “customer” and “account” are to object in a program, then the customer object
may send a message to the account object requesting for the bank balance.
Each object contain data, and code to manipulate data. Objects can interact without
having to know details of each other’s data or code. It is a sufficient to know the type of
message accepted, and the type of response returned by the objects.
Classes
We just mentioned that objects contain data, and code to manipulate that data. The
entire set of data and code of an object can be made a user-defined data type with the help of
class.
In fact, objects are variables of the type class. Once a class has been defined, we can
create any number of objects belonging to that class. Each object is associated with the data of
type class with which they are created.
A class is thus a collection of objects similar types. For examples, Mango, Apple and
orange members of class fruit.
Classes are user-defined Data types and behave like the built-in types of a programming
language. The syntax used to create an object is not different then the syntax used to create an
integer object in C.
If fruit has been defines as a class, then the statementFruit Mango;Will create an object
mango belonging to the class fruit.
5
UNIT – 1: Concept of OOPS
Class vs Object
Class Object
A class is a blueprint for declaring and An object is a class instance that allows
creating objects. programmers to use variables and methods
from inside the class.
Memory is not allocated to classes. Classes When objects are created, memory is
have no physical existence. allocated to them in the heap memory.
You can declare a class only once. A class can be used to create many objects.
Class is a logical entity. An object is a physical entity.
We cannot manipulate class as it is not Objects can be manipulated.
available in memory.
Example: Mobile is a class. If Mobile is the class
then iphone, redmi, blackberry, samsung are
its objects which have different properties
and behaviours.
6
UNIT – 1: Concept of OOPS
Polymorphism
Dynamic binding
Message passing
Encapsulation
The wrapping up of data and function into a single unit (called class) is known as
encapsulation. Data encapsulation is the most striking feature of a class.
The data is not accessible to the outside world, and only those functions which are
wrapped in the class can access it. These functions provide the interface between the object’s
data and the program.
This wrapping of the data from direct access by the program is called data hiding or
information hiding.
Abstraction
Abstraction refers to the act of representing essential features without including the
background details or explanation.
Classes use the concept of abstraction and are defined as a list of Data Members and
function operate on these Data Members. They encapsulate all the essential properties of the
object that are to be created.
The functions that operate on these data are sometimes called methods or member
function.
Inheritance
Inheritance is the process by which objects of one class acquired the properties of
objects of another classes. It supports the concept of hierarchical classification.
For example, the bird, ‘robin’ is a part of class ‘flying bird’ which is again a part of the class
‘bird’. 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 by deriving
a new class from the existing one. The new class will have the combined feature of both the
classes.
The real appeal and power of the inheritance mechanism is that it allows the
programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the
class in such a way that it does not introduced any undesirable side-effects into the rest of
classes.
7
UNIT – 1: Concept of OOPS
Polymorphism
Polymorphism, a Greek term, means the ability to take more than on form. An
operation may exhibit different behavior is different instances. The behavior depends upon the
types of data used in the operation.
For example, consider the operation of addition. For two numbers, the operation will generate
a sum. If the operands are strings, then the operation would produce a third string by
concatenation. The process of making an operator to exhibit different behaviors in different
instances is known as operator overloading.
A single function name can be used to handle different number and different types of
argument. This is something similar to a particular word having several different meanings
depending upon the context. Using a single function name to perform different type of task is
known as function overloading.
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 action associated with each
operation may differ.
Polymorphism is extensively used in implementing inheritance.
Dynamic Binding
Binding refers to the linking of a procedure call to the code to be executed in response
to the call. Dynamic binding (also Called Late Binding) means that the code associated with a
given procedure call is not known until the time of the call at run time.
It is associated with polymorphism and inheritance. A function call associated with a
polymorphic reference depends on the dynamic type of that reference.
Message Passing
Objects communicate with one another by sending and receiving information much the
same way as people pass messages to one another. The concept of message passing makes it
easier to talk about building systems that directly model or simulate their real world
counterparts.
A Message for is a concept in which one object can interact with other object and also
provide provision to compare two or more objects with each other.
8
UNIT – 1: Concept of OOPS
OOP offers several benefits to both the program designer and the user. ObjectOrientation
contributes to the solution of many problems associated with the development and quality of
software products.
The new technology promises greater programmer productivity, better quality of software and
lesser maintenance cost.
The principal advantages are:
Through inheritance, we can eliminate redundant code extend the use of existing
Classes.
We can build programs from the standard working modules that communicate with one
another, rather than having to start writing the code from scratch. This leads to saving
of development time and higher productivity.
The principle of data hiding helps the programmer to build secure program that can not
be invaded by code in other parts of a programs.
9
UNIT – 1: Concept of OOPS
Specifying a Class
A class in C++ combines related data and functions together. It makes a data type which
is used for creating objects of this type.
Classes represent real world entities that have both data type properties
(characteristics) and associated operations (behavior).
Generally a class specification has two parts :
Class Declaration
Class Function definition
Class Declaration
Here, the keyword class specifies that we are using a new data type and is followed by the class
name.
10
UNIT – 1: Concept of OOPS
Name_of_the_class :: function_name
The syntax for a member function definition outside the class definition is :
Here the operator :: known as scope resolution operator helps in defining the member function
outside the class. Earlier the scope resolution operator(::)was is use in situations where a global
variable exists with the same name as a local variable and it identifies the global variable.
Example of Class :
Class student
{
private:
char reg_no[10];
char name[30];
int age;
char address[25];
public :
void init_data()//Inside Class Defenation
{
- - - - - //body of function - - - - -
}
11
UNIT – 1: Concept of OOPS
void display_data();
}; //End of class
void student :: display_data()//Outside class Defination
{
----------//body of function -------
}
Int main()
{
student ob; //class variable (object) created
Ob.init_data(); //Access the member function
ob.display_data(); //Access the member function
return 0;
}
Important Questions
1. Explain the difference between POP and OOP.
2. Explain Data Types in C++.
3. What is Class and Object? Explain difference between them.
4. Explain concept of Object Oriented Programming.
5. Advantages of Object Oriented Programming.
12