Basics of OOP: Chapter # 13
Basics of OOP: Chapter # 13
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