Module 5
Module 5
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.
Each object contain data, and code to manipulate data.
The data is not accessible to the outside world, and only those functions which are
wrapped in the class can access it.
This insulation of the data from direct access by the program is called data hiding
or information hiding.
1. Through
eliminate inheritance,codeweextend
redundant can
2. the
We use of existing
can build Classes.
programs from that
the
standard
communicate working modules
with toone another,
rather
the code than
from having
scratch. start
This writing
leads to
saving
higher of development
productivity. time and
3. The
the principle
programmer of data
to hidingsecure
build helps
program
code in that parts
other can not
of abeprograms.
invaded by
4. It is possible
instances of an to haveto multiple
object co-exist
5. without any
It is possible interference.
to map object inin the
problem
program. domain to those the
6. It is easy
project to partition
based on the work in a
objects.
7. The
enables data-centered
us to capturedesign
more approach
detail of
8. aObject-oriented
model can implemental system form.
cantobe easily
upgraded
system. from small large
9. Message passing
communication techniques
between for
objects
makes
external tosystems
interface descriptions
much simpler. with
10. Software complexity can be easily
managed.
Application of OOP
1. Real-time system
2. Simulation and modeling
3. Object-oriented data bases
4. Hypertext, Hypermedia, and expertext
5. AI and expert systems
6. Neural networks and parallel programming
7. Decision support and office automation systems
8. CIM/CAM/CAD systems
Introduction of C++
A class is a way to bind the data and its associated functions together
Specifying a class has two parts:
class student
{
int rollno; // variable declaration
char name[20]; // private by default
public:
void getdata(); // function declarations
void putdata();
};
Creating objects
The objects of a class are declared after the class definition.
class definition does not define any objects of its type, but it defines the
properties of a class.
For utilizing the defined class, we need variables of the class type.
Syntax:
class_name object(’s)_name;
For example,
Example:
class student
public:
void putdata();
} s1,s2;
Accessing class members
The private data of class can be accessed only through the member
function of a class
Example:
s1.getdata();
getdata() // illegal
s1.rollno; //illegal
Defining Member Functions
Example:
void student :: getdata()
{
cin>>name>>rollno;
}
A C++ program with class
Example Programs(using classes and objects)