Basics of OOP
Chapter # 13
Object Oriented Programming (OOP)
OOP is a programming technique in which programs are written on the
basis of objects.
Object is a collection of data and functions.
Object may represent the a person, thing or place in real world.
It is easier to learn and modify.
It is used to develop software and design applications.
Some of the object oriented languages are :
C++
CLOS
Java
Features of OOP
Objects
Classes
Real world modeling
Reusability (Inheritance)
Information Hiding (Encapsulation)
Polymorphism (Multiple behaviours of Object)
Objects
An object represents an entity in the real world such as person ,thing
and place etc.
It is identified by its name.
It consists of two things:
1- properties (characteristics of an object)
For Example: Model , Colour , Price are properties of car (object).
2- functions (actions)
For Example: Start , Stop , Accelerate , reverse are actions.
Classes
A collection of objects with same properties and functions is known as class
It is used to define the characteristics of the objects.
It is a model for creating different objects of same type.
It has same properties and functions
like; a class person may contain Usman, Ali , Umar etc.
A class is declared in the same way as a structure is declared .
Syntax
Class identifier
{
body of class
};
Access specifiers
The commands that are used to specify the access level of class
members .
Two most important access specifiers
1. Private Access Specifier
2. Public Access Specifier
An object is also known as instance . The process of creating an
object of a class is called instantiation.
Syntax:
class_name object_name;
Executing member functions
An object of a particular class contains data members as well as
member functions defined in that class.
The member functions are used to manipulate data members.
The member functions can be executed only after creating an object.
Syntax:
object _ name. function();
Example
Test obj;
Obj.input();
Defining member functions outside of class
It can be defined outside of class
The declaration of member function is specified inside and function is
specified outside of class.
The scope resolution operator is used in function declarator if the
function is defined outside of class.
Syntax
Return_type class_name :: function_name (parameters)
{
body of function
}
Constructors
A type of member function that is executed automatically when an object
of that class is created.
It has no return type and same name that of class.
It cannot return any value like function return value.
It is usually defined in classes to initialize data member.
Syntax:
Name()
{
Body
}
Passing parameters to constructors
The method of passing parameters to constructers is same as
functions. The only difference is that the parameters are passed to
constructors when the object is declared.
The parameters are passed to constructers in parenthesis along with
the object name in declaration statement.
Syntax:
Type object _name (parameters);
Constructor overloading
The process of declaring multiple constructors with same name but
different parameters is known as constructor overloading.
They must be different in following ways :
i. No. of parameters
ii. Type of parameters
iii. Sequence in parameters
Default copy constructor
Type of constructor that is used to initialize an object with another
object of the same type known as default copy constructor .
User doesn’t need to write this constructor because it is available By
Default in all classes.
Parameters can be passed with parenthesis or assignment operator.
Syntax:
Class_Name object_Name (parameter); or
Class_Name object_Name = parameter;
Destructors
A type of member function that is automatically executed when an object of that
class is destroyed.
It has no return type and same name as a class.
It cannot return any value.
It cannot accept parameters.
It uses tilde sign ~ .
Syntax:
~Name()
{
destructor Body
}
Static data Member
A type of data members that is shared among all objects of class is
known as static data members.
It uses “static” keyword .
It is defined in the class.
If a data member is defined in the class with static only one variable is
created in the memory no matter how many objects that class have.
It is visible in the class only where it is defined .
It lifetime starts when a program start execution and ends when
terminated .
Friend functions
A type of function that is allowed to access the private and protected
members of a particular class from outside of class.
It allows the user to access the members from outside of class.
It uses keyword “friend”.
Syntax:
Friend_returntype_Function_Name(parameters);
Without friend functions it is unable to access private and protected
members from outside.
Friend classes
A type of class that is allowed to access the private and protected
members of a particular class from outside of class .
It allows the user to access the members from outside of class.
It uses keyword “friend”.
Without friend classes it is unable to access private and protected
from outside.
A class that is declared in another class with “friend” keyword
becomes the friend of that class.
Static functions
A type of member function that can be access any object of class is
known as static function.
It uses “static” keyword.
Normally a member function of any class cannot be accessed or
executed without creating an object of that class.
In some conditions member functions has to be executed without
referencing any object.
Static member functions can be used to access a static data member.
THE END