Module9 ObjectOrientedProgramming Concepts
Module9 ObjectOrientedProgramming Concepts
com
m
programs are made up functions and functions are often not reusable or it is very difficult to copy
co
a function from one program to another program and reuse because the functions are likely to
reference global variables, headers and refers to other functions. The structural programming
n.
languages are not suitable for high level abstraction for solving real world problems. In
procedural programming the programs are basically list of instructions and data and these
io
instructions works with data to give results. Instructions and data are the two separate entities
at
that are combined together to produce required results.
uc
Advantages of procedural programming languages
ed
• The data is global that exposed to the whole program, so no security for the data.
.s
• The procedural language does not have automatic memory management, so it makes the
w
OOPS:
The Object oriented programming languages are designed to overcome the following
problems.
www.sakshieducation.com
www.sakshieducation.com
m
a software entity inside the same box.
co
Object oriented programming provides the ability to design an ‘object’, and which
associated with the concepts of objects and having data fields and related member functions.
n.
This allows programs to be written in more modular way, which makes them easier to
io
understand and also provides higher degree of code reusability.
at
Object uc
An object can be considered as a ‘thing’ that can perform a set of related activities. The set
of activities that the object performs defines the objects behavior. Objects are instances of classes
ed
and are used to interact amongst each other to create application. The instance means the object
of class on which we are currently working. The C language with classes can be said as C++. In
hi
C++ everything revolves around the object of class, which their data members and methods
ks
(functions). For example consider a human body as class and multiple objects of this class with
variables as hair, color and etc. and methods as speaking, walking etc.
a
.s
w
w
w
The Procedural oriented languages are focus on procedures with functions as the basic
unit, that is first figure out all the functions and then think about how to represent the data.
Whereas the Object oriented languages focus on component that the user prospective with
www.sakshieducation.com
www.sakshieducation.com
objects as the basic unit, here figure out all the objects by putting all the data and instructions
that describe the user interaction with the data. In procedural programming the data and
functions are stored in separate memory locations, where as in object oriented programming the
data and methods (functions) are stored in same memory locations.
Advantages of OOPS
m
• OOPS supports and provides modular structure for developing applications.
co
• Object oriented programs are easier to understand, therefore easier to debug and maintain.
• It is easier to modify the programs and reusability which increases the speed of software
n.
development.
• Ease in software design as we think in the problem space rather than computer
io
terminology.
at
Disadvantages of OOPS
uc
• Object oriented programs are bigger in size because they have many lines of code, so they
ed
o Abstraction
o Encapsulation
a
o Inheritance
.s
o Polymorphism
w
w
w
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
io
at
uc
Figure1: Features of OOPS
ed
Now let us have some discussion on the features of object oriented programming which
will be used in C++ programming.
hi
C++ Class
a ks
The classes are the most important feature of C++ programming. A class is the collection
.s
of related data members (Variables) and methods (functions) under a single name. A class is a
user defined data type, which can be accessed and used by creating instance of that class. A C++
w
program can have any number of classes. The class helps to visualize the complex problem
w
www.sakshieducation.com
www.sakshieducation.com
m
co
n.
Figure2: Class
io
Defining a Class:
at
uc
Class is defined using a keyword ‘class’ followed by identifier which is the name of class
and the body of class starts with open curly brace and terminated with close curly brace with
ed
semicolon or with a list of objects declarations at the end. When a class is defined no memory is
allocated.
hi
Syntax:
ks
class Class_Name
a
{
.s
w
Access specifier:
w
Data_members;
w
Methods; // functions();
};
www.sakshieducation.com
www.sakshieducation.com
Example:
class Account
private:
m
int acc_no;
co
float balance;
n.
public:
io
void acc_details();
at
float deposit(float amt);
uc
float withdraw(float amt);
ed
}; or
hi
} obj1, obj2;
ks
As mentioned above the definition of class starts with keyword ‘class’ followed by name
‘account’ in the above example. The body of class starts with curly braces and terminated by
a
semicolon at the end. There are two keywords used in the above example private and public.
.s
These are called access specifiers and we will learn these following sections. By default every
w
C++ object
When a class is defined, only specification for the object is defined. Objects are instances
of class, which holds the data members declared in the class and methods work on these class
data members. Object has same relationship to the class as the variable has with the data type.
www.sakshieducation.com
www.sakshieducation.com
The objects can be defined in similar way as structure is defined. The objects get physical
memory and contain data members which are declared in the class.
Syntax:
Class_Name Object_Name;
m
For the above example the object definition is
co
Account obj1; Æ obj1 is the object of Account class.
n.
Example:
io
int main()
at
{
uc
Account obj1;
ed
Account obj2;
hi
}
ks
Access specifiers
a
Access specifiers are used for access control in classes. The access specifiers are used to
.s
set boundaries for availability of data members and member function (methods) of a class. By
w
default the class members and methods are private. We can use one, two or all three access
w
specifiers in the same class to set different boundaries for different class members.
w
1. Public
2. Private
3. Protected
www.sakshieducation.com
www.sakshieducation.com
Public keyword indicates all the class members declared under this section will be
available to everyone. The data members and methods declared under public section can be
accessed by other classes also, so there are chances that other classes can change the members.
Therefore the important data members must not be declared in the public section.
m
Example:
co
n.
io
at
uc
Figure3: Public member declarations
ed
Private keyword indicates that the class members declared under private section are not
available to everyone and no one can access these class members outside of that class. If
a
someone tries to access these data members then the compiler will through a compile time error.
.s
w
w
w
www.sakshieducation.com
www.sakshieducation.com
Protected keyword is similar to private access specifier that is class members are
inaccessible outside the class, but they can be accessed by any subclass of that class.
m
co
n.
io
Figure5: Protected member declarations
at
uc
Data members and methods
ed
The data within the class is known as data members and the functions defined within the
class are known as methods. The data members are declared with any of fundamental data types
hi
and also with array types, pointer types, reference types and with user defined data types also. By
ks
Accessing data members depends on the access control specifiers of the data members. If
w
the access control is public then the data members and methods can be accessed in similar way
w
the members of structure is accessed using dot (.) operator. If the access control is private and
protected then we cannot access the data members directly, so we have to create public member
w
functions or methods to access the private and protected data members. These member functions
are also called set or get functions.
www.sakshieducation.com
www.sakshieducation.com
The following example illustrates how to initialize and access the public data members
using the ‘.’ dot operator.
m
co
n.
io
at
uc
ed
hi
a ks
.s
w
www.sakshieducation.com
www.sakshieducation.com
The following example illustrates how to initialize and access the private data members, to
access the private data members we need to create setter and getter functions to set and get the
values of the data members. The both the functions must be defined as public, the setter function
will set the value passed as argument to private data members and getter function will return the
value of the private data member to be used. The protected data members can be accessed as
m
same as private data members, and inside the subclass the members can be accessed directly
co
using ‘.’ dot operator.
n.
io
at
uc
ed
hi
a ks
.s
w
w
w
www.sakshieducation.com