0% found this document useful (0 votes)
37 views

OOP-Unit 1

The document discusses object-oriented programming concepts including procedure oriented vs object oriented programming, data types in C++, the concept of strings using character arrays and pointers, and concepts of classes and objects including the differences between classes and objects.

Uploaded by

svtiwari58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

OOP-Unit 1

The document discusses object-oriented programming concepts including procedure oriented vs object oriented programming, data types in C++, the concept of strings using character arrays and pointers, and concepts of classes and objects including the differences between classes and objects.

Uploaded by

svtiwari58
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIT – 1:

Concept of OOPS
UNIT – 1: Concept of OOPS

1.1 Procedure Oriented Programming Vs Object Oriented Programming

Procedure Oriented Programming Object Oriented Programming

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.

Overloading In POP, Overloading is not possible. In OOP, overloading is possible in the


form of Function Overloading and
Operator Overloading.

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

1.2 Various library(header) files require for C++

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.

<new> It contains dynamic memory allocation methods.

<vector> It contains the definition of the vector class container

It contains the definition of the map class container. A map allows


<map>
fast access to the value using the key

1.3 Data Types in C++

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 Concept of string


1.4.1 Character Array: "Char" data type or a character data type is used to store
letters.
 A Character array is a derived data type in C that is used to store a collection of
characters or strings.
 A char data type takes 1 byte of memory, so a character array has the memory of the
number of elements in the array. (1* number_of_elements_in_array).
 Each character in a character array has an index that shows the position of the character
in the string.
 The first character will be indexed 0 and the successive characters are indexed 1,2,3
etc...
 The null character \0 is used to find the end of characters in the array and is always
stored in the index after the last character or in the last index.
 Consider a string "character", it is indexed as the following image in a character array.

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";

cout << ch << endl;

4
UNIT – 1: Concept of OOPS

return 0;
}

1.5 Concepts of Class and Objects.

 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.

The major motivating factor in the invention of object-oriented approach is to remove


some of the flaws encountered in the procedural approach. OOP treats data as a critical
element in the program development and does not allow it to flow freely around the system. It
ties data more closely to the function that operate on it, and protects it from accidental
modification from outside function. OOP allows decomposition of a problem into a number of
entities called objects and then builds data and function around these objects.
Some of the features of object oriented programming are:

 Emphasis is on data rather than procedure.


 Programs are divided into what are known as objects.
 Data structures are designed such that they characterize the objects.
 Functions that operate on the data of an object are ties together in the data structure.
 Data is hidden and cannot be accessed by external function.
 Objects may communicate with each other through function.
 New data and functions can be easily added whenever necessary.
 Follows bottom up approach in program design.

“Object Originated Programming is an approach that provides a way of modularizing


programs by creating partitioned memory area for both data and function that can be used as
templates for creating copies of such modules on demand.”

Basic Concepts of Object Oriented Programming


 Objects
 Classes
 Data abstraction and encapsulation
 Inheritance

6
UNIT – 1: Concept of OOPS

 Polymorphism
 Dynamic binding
 Message passing

 Data Abstraction and Encapsulation

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

An object-oriented program consists of a set of objects that communicate with each


other. The process of programming in an object-oriented language, involves the following basic
steps:

1. Creating classes that define object and their behavior,


2. Creating objects from class definitions, and
3. Establishing communication among objects.

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

Benefits of Object Oriented Programming

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.

 It is possible to have multiple instances of an object to co-exist without any interference.

 It is possible to map object in the problem domain to those in the program.

 It is easy to partition the work in a project based on objects.

 The data-centered design approach enables us to capture more detail of a model in


implementable form.

 Object-oriented system can be easily upgraded from small to large system.

 Message passing techniques for communication between objects makes to interface


descriptions with external systems much simpler.

 Software complexity can be easily managed.

 While it is possible to incorporate all these features in an object-oriented system, their


importance depends on the type of the project and the preference of the programmer.

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

The syntax of a class definition is shown below :

class name_of _class


{
private :
variable declaration; // data member
Function declaration; // Member Function (Method)
protected:
Variable declaration;
Function declaration;
public :
variable declaration;
Function declaration;
};

Here, the keyword class specifies that we are using a new data type and is followed by the class
name.

The body of the class has two keywords namely :


(i) private(ii) public
In C++, the keywords private and public are called access specifiers. The data hiding
concept in C++ is achieved by using the keyword private. Private data and functions can only be
accessed from within the class itself. Public data and functions are accessible outside the class
also.
The data declared under Private section are hidden and safe from accidental
manipulation. Though the user can use the private data but not by accident.
The functions that operate on the data are generally public so that they can be accessed from
outside the class but this is not a rule that we must follow.

10
UNIT – 1: Concept of OOPS

 Class Function definition

In C++, the member functions can be coded in two ways :


(a) Inside class definition
(b) Outside class definition using scope resolution operator (::)
The code of the function is same in both the cases, but the function header is different as

Inside Class Definition:


When a member function is defined inside a class, we do not require to place a
membership label along with the function name. We use only small functions inside the class
definition and such functions are known as inline functions. In case of inline function the
compiler inserts the code of the body of the function at the place where it is invoked (called)
and in doing so the program execution is faster.

Outside Class Definition Using Scope Resolution Operator (::) :

In this case the function’s full name (qualified_name) is written as shown:

Name_of_the_class :: function_name

The syntax for a member function definition outside the class definition is :

return_type name_of_the_class::function_name (argument list) { body of function }

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

You might also like