0% found this document useful (0 votes)
24 views16 pages

OOPS

The document provides an overview of procedure-oriented and object-oriented programming, highlighting their characteristics and differences. It discusses key object-oriented concepts such as classes, objects, inheritance, polymorphism, and encapsulation, along with the benefits and applications of OOP. Additionally, it introduces C++ as an object-oriented programming language, detailing its structure, classes, objects, constructors, and destructors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views16 pages

OOPS

The document provides an overview of procedure-oriented and object-oriented programming, highlighting their characteristics and differences. It discusses key object-oriented concepts such as classes, objects, inheritance, polymorphism, and encapsulation, along with the benefits and applications of OOP. Additionally, it introduces C++ as an object-oriented programming language, detailing its structure, classes, objects, constructors, and destructors.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODULE 1 & 2

PROCEDURE ORIENTED LANGUAGE


In the procedure oriented approach, the problem is viewed as a sequence of things
to be done such as reading, calculating and printing. A number of functions are
written to accomplish these tasks. The primary focus is on functions. Procedure
oriented programming basically consists of writing a list of instructions (or actions)
for the computer to follow, and organizing these instructions into groups known as
functions.
Some characteristics exhibited by procedure-oriented programming are:
• Emphasis is on doing things (algorithms).
• Large programs are divided into smaller programs known as functions. • Most of
the functions share global data.
• Data move openly around the system from function to function.
• Functions transform data from one form to another.
• Employs top-down approach in program design

OBJECT ORIENTED PROGRAMMING


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 operates on
it, and protects it from accidental modification from outside functions. OOP allows
decomposition of a problem into a number of entities called objects and then builds
data and functions around these objects. The data of an object can be accessed only
by the functions associated with that object. However, functions of one object can
access the functions of other objects.
Some of the striking 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 tied together in the data
structure
• Data is hidden and cannot be accessed by external functions.
• Objects may communicate with each other through functions.
• New data and functions can be easily added whenever necessary.
• Follows bottom-up approach in program design.

OBJECT ORIENTED CONCEPTS


The object oriented concepts include the following:
• Objects
• Classes
• Data abstraction and encapsulation
• Inheritance
• Polymorphism
• Dynamic binding
• Message passing
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. Program objects should be chosen such that they match
closely with the real-world objects.
When a program is executed, the objects interact by sending messages to one
another. For example, if "'customer" and “account" are two objects in a program,
then the customer object may send a message to the account object requesting for
the bank balance. Each object contains data, and code to manipulate the data.

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 a 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 I.hat
class. Each object is associated with the data of type class with which they are
created. A class is thus a collection of .objects of similar type. For example,
mango, apple and orange are members of the class fruit. Classes are user-defined
data types and behave like the built-in types of a programming language.

Data Abstraction and Encapsulation


The wrapping up of data and functions into a single unit (called c1ass) 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 insulation of the data from direct access by
the program is called data hiding or information hiding.
Abstraction refers to the act of representing essential features without
including the background details or explanations. Classes use the concept of
abstraction and are defined as a list of abstract attribute, such as size, weight and
cost and functions to operate on these attributes. They encapsulate all the essential
properties of the objects that are to be created. The attributes are sometimes called
data numbers because they hold information. The functions that operate on these
data are sometimes called methods or member functions.
Since the classes use the concept of data abstraction, they are known as
Abstract Data Types (ADT).

Inheritance
Inheritance is the process by which objects of one class acquire the properties of
objects of another class. It supports the concept of hierarchical classification. For
example, the bird 'robin' is a part of the class 'flying bird' which is again a part of
the class 'bird'. 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 features of both the classes.

Polymorphism
Polymorphism is another important OOP concept. Polymorphism a Greek term,
means the ability to take more than one form. An operation may exhibit different
behaviors in 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. . Using a single function name to perform different types or tasks is
known as function overloading.
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 known as 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.

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, therefore,
involves the following basic steps:
1. Creating classes that define objects 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.

BENEFITS 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 and 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 c.ode from scratch. This
leads to saving of development time and higher productivity.
• The principle of data hiding helps the programmer to build secure programs that
cannot be invaded by code in other parts of the program.
• It is possible to have multiple instances of an object to co-exist without any
interference.
• It is possible to map objects 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 details of a model
in implementable form.
• Object-oriented systems can be easily upgraded from small to large systems.
• A message passing techniques for communication between objects makes the
interface descriptions with external systems much simpler.
• Software complexity can be easily managed.

APPLICATIONS OF OOPS
Applications of OOP are beginning to gain importance in many areas. The most
popular application of object oriented programming, up to now, has been in the
area of user interface design such as windows.
Real-business systems are often much more complex and contain many more
objects with complicated attributes and methods. OOP is useful in these types of
applications because it can simplify a complex problem. The promising areas for
application of OOP include:
• Real-time systems
• Simulation and modeling
• Object-oriented databases
• Hypertext, hypermedia and expert text
• Al and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems

OVERVIEW OF C++
C++ is an object-oriented programming language. It was developed by
Bjarne Stroustrup at AT&T Bell Laboratories. C++ is a superset of C. Most of hat
we already know about C applies to C++ also. Therefore almost all C programs are
also C++ programs.
The most important facilities that C++ adds on to C are classes, inheritance,
function overloading, and operator overloading. Those features enable creating of
abstract data types, inherit properties from existing data types and support
polymorphism, thereby making C++ a truly object-oriented language.
The object-oriented features in C++ allow programmers to build large
programs with clarity, extensibility and ease of maintenance, incorporating t.he
spirit and efficiency of C.

STRUCTURE OF C++

A typical C++ program contains the following section.


1. Documentation section
It contains the comment lines, comments can appear anywhere in a program that is
used to explain a point about a code. The comment lines begins with /* and ends
with */ or a single set of // is given in the beginning of the line. Comment lines are
not executable.
2. Preprocessor section
It is used to link system library files, for defining the macros conditional inclusion.
It provides the special instructions to the preprocessor that tells how to prepare the
program for compilation. For example,
#include<iostream.h>,
#include<conio.h> etc.
3. Global declaration section
In this section, we declare some variables before starting of the main program or
outside the main program. These variables are globally declared and used by the
main function or sub function. These variables are known as global variable.
4. Class declaration and member function definition section
This section can be considered as sub section for the global declaration section.
Class declaration and all methods of that class are defined here.
5. Main function
This is the entry point for all the function. Every method is called indirectly
through main function. Class objects are created and the methods are called.
6. Method definition section
This is the optional section. This method was generally used in C programming.

CLASSES AND OBJECTS


The classes are the most important feature of C++ that leads to Object
Oriented programming. A class is a blueprint for the object.
Note: We can think of class as a sketch (prototype) of a house. It contains all the
details about the floors, doors, windows etc. Based on these descriptions we build
the house. House is the object. As many houses can be made from the same
description, we can create many objects from a class.
When you define a class, you define a blueprint for a data type. This doesn't
actually define any data, but it does define what the class name means, that is, what
an object of the class will consist of and what operations can be performed on such
an object. Class is a user defined data type, which holds its own data members and
member functions, which can be accessed and used by creating instance (object) of
that class.
Data members are the data variables and member functions are the functions
used to manipulate these variables and together these data members and member
functions defines the properties and behavior of the objects in a Class.
An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is allocated.

Defining Class and Declaring Objects


A class is defined in C++ using keyword class followed by the name of class. The
body of class is defined inside the curly brackets and terminated by a semicolon at
the end.

Declaring Objects:
When a class is defined, only the specification for the object is defined. no memory
or storage is allocated. To use the data and access functions defined in the class,
you need to create objects.
Syntax:
ClassName ObjectName;

Accessing data members and member functions:


The data members and member functions of class can be accessed using the
dot(„.‟) operator with the object. For example if the name of object is obj and you
want to access the member function with the name displayName() then you will
have to write obj.displayName()

#include<iostream> using namespace std;


class student
{ Data members
int id,age;
char name[100];
public:
void readdata() Member funtion
{
cout<<"enter the id:";
cin>>id; cout<<"enter
the name:";
cin>>name;
cout<<"enter
the age:"; Member function
cin>>age;
}
void dispay() { cout<<”
Details:\n”; cout<<"ID:
"<<id; cout<<"\nName:
"<<name;
cout<<"\n Age : "<<age;
} };
int Creating object for class student

main() {
student a;
a.readdata();
a.dispay();
return 0;
}

Accessing member function using


object a

Defining Member Functions:

There are 2 ways to define a member function:

- Inside class definition


- Outside class definition

To define a member function outside the class definition we have to use the scope
resolution :: operator along with class name and function name. And function
declaration should be there inside the class.

CONSTRUCTORS
Constructer is a special type of member function that initializes an object
automatically when it is created. The compiler calls the constructor whenever an
object is created. Constructors have the same name as the class. Constructor can be
defined inside or outside the class definition. A constructer is defined without a
return type. By default constructors are defined in the public section of class, so
that constructor is called automatically when an object is created outside the class.
A constructer is defined without a return type

There are 3 types of constructors:


• Default constructors
• Parameterized constructors
• Copy constructors

Default constructors
A default constructor is the constructor which doesn‟t take any argument. It has no
parameter. Above program is an example of default constructor.
Note: Even if we do not define any constructor explicitly, the compiler will
automatically provide a default constructor implicitly. The default value of all data
members will be initialized to 0 Example:

Parameterized constructors
It is possible to pass arguments to constructors. Using this constructor you can
provide different values to data members of different objects. To create a
parameterized constructor, simply add parameters to it, similar to any other
functions. When you define the constructor‟s body, use the parameters to initialize
the object.

DESTRUCTORS
Destructor is another special member function which destroys the object as soon as
the scope of the object ends. The destructor is automatically called by the compiler
when the object goes out of the scope. Syntax of destructor is same that of
constructor, the class name is used as the name of destructor, with a tilde „~‟ sign
as prefix to it.

You might also like