KEMBAR78
Object Oriented Pogramming (OOP) | PDF | Object Oriented Programming | Class (Computer Programming)
0% found this document useful (0 votes)
22 views28 pages

Object Oriented Pogramming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which encapsulate data and functions. Key features include classes for object creation, real-world modeling, reusability through inheritance, information hiding via encapsulation, and polymorphism. OOP facilitates easier program modification and learning, with examples of languages including C++, Java, and Smalltalk.

Uploaded by

ranaibrahim453
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)
22 views28 pages

Object Oriented Pogramming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of objects, which encapsulate data and functions. Key features include classes for object creation, real-world modeling, reusability through inheritance, information hiding via encapsulation, and polymorphism. OOP facilitates easier program modification and learning, with examples of languages including C++, Java, and Smalltalk.

Uploaded by

ranaibrahim453
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/ 28

OBJECT ORIENTED

PROGRAMMING
(OOP)
OOP

Object-Oriented programming (OOP) is a programming technique in which programs are written on the basis
of objects. An object is a collection of data and functions. Object may represent a person, thing or place in
real world. Object oriented programs are easier to learn and modify.
Some of the object-oriented languages that have been developed are:
• C++

• Smalltalk
• Eiffel

• CLOS
• Java
1. Objects

Features of OOP provides the facility of programming based on objects. Object is an entity that consists of data
and functions.
Object 2. Classes

Oriented Classes are designs for creating objects. OOP provides the facility to design classes for creating
different objects.
Programming 3. Real-world Modeling

OOP is based on real-world modeling. As in real world, things have properties and working capabilities.
Similarly, objects have data and functions. Data represents properties and functions represent working
of objects.

4. Reusability

OOP provides ways of reusing the data and code. Inheritance is a technique that allows a programmer
to use the code of existing program to create new programs.

5. Information Hiding

OOP allows the programmer to hide important data from the user. It is performed by encapsulation.

6. Polymorphism

Polymorphism is an ability of an object to behave in multiple ways.


Examples:
Some examples of objects of different types are as
follows:
Objects Physical Objects
An object represents an entity in • Vehicles such as car, bus, truck etc.
the real world such as a person,
thing or concept etc. An object is • Electrical components
identified by its name. Elements of Computer-User Environment
An object consists of the following • Windows
two things:
• Menus
Properties:
• Graphics objects
Properties are the characteristics
of an object. • Mouse and Keyboard
Functions: User-defined Data Types
Functions are the action that can • Time
be performed by an object.
• Angles
Properties of Objects:

The characteristics of an object is called as its properties or attributes. Each object has its own
properties that is used to describe the Object.
For Example the properties of an object CAR ca be as follows: CAR
• Color Model Honda City
Color Silver
• Price
Price 1,200,000
• Model Engine Power 1600 CC
• Engine Power

The set of values of the attributes of a particular object is called its state. It means that the
state of an object can be determined by the values of its attributes. The figure shows the values
of the attributes of an object CAR.
Functions of Objects
An object can perform different tasks and actions. The actions that can be performed by
an object are known as functions or methods.
For example, the object Car can perform the following functions:
• Start
• Stop
• Accelerate
• Reverse
The set of all functions of an object represents the behavior of the object. It means
that the overall behavior of an object can be determined by the list of functions of that
object.
Classes

A collection of objects with the same properties and functions is known as class. A class is used
to define the characteristics of the objects.
For example, a class Person can be used to define the characteristics and functions of a
person. It can be used to create many objects of type Person such as Ali, Usman, Abdullah etc.
All objects of the class Person will have the same characteristics and functions. However, the
values of each object can be different. The values of the objects are assigned after creating an
object.
Each object of a class is known as an instance of its class. For example, Ali, Usman and
Abdullah are three instances of a class Person.
The variables declared in class are Data members and the functions declared are Member
Functions.
Declaring a Class
The keyword class is used to declare a class. A class declaration specifies the variables
and functions that are common to all objects of that class.
• Example
Syntax Class Test
The syntax of declaring a class is as follows: {
class identifier
int n;
{

body of the class char c;


}; float x;
class is the keyword that is used to declare a class.
};
identifier is the name of the class. The rules for class name are same as the rules for
declaring a variable.

The class declaration always ends with semi colon. All data members and member
functions are declared within the braces known as the body of the class.
Access Specifiers
The commands that are used to specify the access level of class members are known as
access specifiers.
• The 'private' Access Specifier • The 'public' Access Specifier
The private access specifier is used to restrict The public access specifier is used to allow the
the use of class member within the class. Any user to access a class member within the class as
member of the class declared with private well as outside the class. Any member of the class
access specifier can only be accessed within declared with public access specifier can be
the class. It cannot be accessed from outside accessed from anywhere in the program.
the class. It is also the default access specifier. The members functions are normally declared with
The data members are normally declared with public access specifier. It is because the users
private access specifier. It is because the data access functions of an object from outside the
of an object is more sensitive. The private class. The class cannot be used directly if both
access specifier is used to protect the data data members and member functions are declared
member from direct access from outside the as private.
class. These data members can only be used
by the functions declared within the class.
Test

private;
int a;
char c;
float x;

Public;
Show(); Outside the class
Input();

The above figure shows that data members a, c and x of class Test cannot be accessed from
outside the class because they are declared with private access specifier. The member
functions show() and input() are accessible from outside the class because they are declared
with public access specifier. The data members are accessed and manipulated indirectly
through member functions.
2/16/2025 10
Creating Objects
A class is simply a model or prototype for creating objects. It is like a
new data type that contains both data and functions. An object is
created in the same way as other variables are created. When an
object of a class is created, the space for all data members defined in
the class is allocated in the memory according to their data types. An
object is also known as instance. The process of creating an object of
a class is also called instantiation.
Syntax Example:
The syntax of creating an object of a class is as follows:
Test obj;
class_name object_name;

class_name is the name of the class whose type of object is to be created.


object_name is the name of the object to be created. The rules for object
name are the same as the rules for declaring a variable.

2/16/2025 11
Executing Member Functions
An object of a particular class contains all data members as well as member functions defined in that
class. The data members contains the value related to the object. The member functions are used to
manipulate data members. The member functions can be executed only after creating an object.
Syntax
The syntax of executing member functions is as follows: Example:

Test obj;
object name function(); obj.input();

object_name is the name of the object whose member function is to be executed.


function is the name of the member function to be executed. Any required parameters are also
passed to the member function in parenthesis.

The object name and member function are separated by dot operator.
2/16/2025 12
Program: Write a program that declares a class with one integer data member and
two member functions in() and out() to input and output data in data
member.
#include<iostream.h> void out()
{
#include<conio.h>
cout<<"The value of n =" <<n;
class Test }
}
{
void main()
private: {
int n;
clrscr()
public: Test obj;
void in()
obj.in(); Output:
obj.out();
{ getch();
Enter a number:10
cout<<'' Enter a number:";
} The value of n =10
cin>>n;

}
2/16/2025 13
Constructors
• A type of member function that is automatically executed when an object of that class is
created is known as constructor. The constructor has no return type and has same name as
that of class name. The constructor can work as a normal function but it cannot return any
value. It is normally defined in classes to initialize data member.
Syntax:
The syntax of declaring constructor is as follows:
name()
{
constructor body
}

name indicates the name of the constructor. The name must be same as the name of the class
in which the constructor is declared.

2/16/2025 14
Program Write a class that displays a message on the screen whenever an object of that
class is created.
#include<iostream.h>
#include<conio.h>
class Hello
{
private:
int n;
public:
Hello() Output:
{
cout<<" object created...."<<endl;
} Object created....
}; Object created....
void main() Object created....
{
Hello x,y,z;
getch();
}

2/16/2025 15
Constructors

USES:
1. To initialize object.
2. To set default value.
3. Automatic memory allocation.

WHY CONSTRUCTORS?
1. Automatic initialization.
2. Code simple.
3. Improve encapsulation.

2/16/2025 16
Constructor uses in real world:
➢ Bank account systems.
When new accounts are created then to set initial balance 0 everytime.

➢ ATM systems.
To create new accounts.

➢ Game development.
To set default power and heart of the character.

➢ Online shopping systems.


When new products are created then their name, price and stock are set automatically.

2/16/2025 17
Passing parameters to constructors
2/16/2025

• The method of passing pararneters to a constructor is same as passing parameters to normal


functions. The only difference is that the parameters are passed to the constructor when the
object is declared. The parameters are written in parenthesis along with the object name in
declaration statement.
Sytnax
The syntax of passing parameters to constructor is as follows:
type object_name(parameters);

type is the name of a class and indicates the type of object to be created.
object_name indicates the name of the object to be created.
Parameter indicates the list of parameters passed to the constructor
18
Program:
#include<iostream> void display()
using namespace std; {
class Student cout << "Name: " << name << ", Age: " << age
{ << endl;

private: }

string name;
};
int age; int main()
public: { Output:
Student student1("Ali", 20);
Student(string n, int a) Student student2("Ayesha", 22); Name: Ali, Age: 20
{ student1.display(); Name: Ayesha, Age: 22
student2.display();
name = n; return 0;
age = a; }
}
2/16/2025 19
Constructor Overloading

The process of declaring multiple constructors with same name but


different parameters is known as constructor overloading.

The constructors with same name must differ in one of the following
ways:
• Number of parameters
• Type of parameter
• Sequence of parameters

2/16/2025 20
Program: Write a class that has num and ch as data members. A constructor with no
parameter initializes num to 0 and ch to 'x'. A constructor with two parameters initializes data
members with the given values and member function show displays the values of data
members.

#include<iostream.h> Over(int n, char c) cout<<"The contents of first:"<<endl;


#include<conio.h> { first.show();
num=n;
class Over cout<<"The contents of
ch=c;
{ second:"<<endl;
}
second.show();
private: void show()
int num; { getch();
char ch; cout<<"num = "<<num<<endl; }
cout<<"ch="<<ch<<endl;
public:
} Output:
Over() The contents of first:
};
{ num =0
Void main()
ch = x
num = 0; {
Th contents of second:
ch = 'x'; Over first, second (100,'p'); num = 100
}
2/16/2025 ch = p 21
Default Copy • Syntax
Constructors The syntax of using default copy constructor is as
follows:
A type of constructor that is used class_name object_name (parameter);
to initialize an object with another OR
object type is known as default class_name object_name = parameter;
copy constructor. It is available by
default in all classes. The user does
class_name is the name of a class and indicates the
not need to write this constructor. type of object to be created.
It accepts a single object of the
object_name indicates the name of the object to be
same type as parameter. The created.
parameter for default copy parameter indicates the name of parameter that is
constructor can be given in passed to default copy constructor. The values of data
parenthesis or using assignment members of parameter object are copied to data
operator. members of the new object. The type of new object and
parameter object must be same.
2/16/2025 22
Program:
#include<iostream.h> void show()
{
#include<conio.h>
cout<<"Title:"<<title<<endl;
#include<stdio.h> cout<<"Pages:"<<pg<<endl;
class Book cout<<"Price:"<<pr<<endl; Output:
{ } Enter title: Visual Programing
Enter pages:637
private: }; Enter price:200
int pg, pr; void man()
The details of b1:
char title [50]; { Title: Visual Programming
public: Book b1; Pages: 637
Price: 200
b1.get();
void get()
Book b2(b1); The details of b2:
{ Book b3 = b1; Title: Visual Programming
Pages: 637
Cout<<"Enter title:"; cout<<"/n The details of b1:"<<endl; Price: 200
gets(title); b1.show(); The details of b3:
cout<<"Enter pages: "; cout<<"/n The details of b2:"<<endl; Title: Visual Programming
cin>>pg; b2.show(); Pages: 637
Price: 200
cout<<"Enter price: "; cout<<"/n The details of b3:"<<endl;
cin>>pr; b3.show();
getch();
}
}

2/16/2025 23
2/16/2025
Destructors

• A type of member function that is automatically executed when an object of that class is destroyed is
known as destructor. The destructor has no return type and its name is same as class name. The
destructor cannot return any value. It also cannot accept any parameters. The destructor name is
preceded by tilde sign ~.
Syntax
The syntax of declaring destructor is as follows:
~name()
{
destructor body
}
name indicates the name of the destructor. The name must be same as the name of the class in which the
constructor is declared.

23
EXAMPLE:
#include<iostream.h> void main()
#include<conio.h> {
class Test clrscr();
{ Test a, b;
Private: }
Int n;
Public:
Test()
{
Cout<<"Object MNMM
created...."<<endl;
} Output:
~Test()
{
Object created.....
Cout<<"Object
destroyed...."<<endl; Object created.....
} Object destroyed....
}; Object destroyed....

2/16/2025 25
Destructor uses:

• To cleanup Dynamic memory.


• To close Database connection.
• To close file, preventing data loss, in file handling.

2/16/2025 26
Static Data Members
A type of data member thatis shared among al objects of clans is known as static data member.
The static data member is defined in the class with static keyword. When a data member is
defined as static, only one variable is created in the memory even if there are many objects of
that class.
The characteristics of a static data member are same as normal static variable. It is visible only
in the class in which it is defined but its lifetime starts when the program starts its execution. Its
lifetime ends when the entire program is terminated. It is normally used to share some data
among all objects of a particular class.
The main difference between normal data member and static data member is that each object
has its own variable of normal data member. On the other hand, static data member is shared
among all objects of the class. Only one memory location is created for static data member that
is shared among all objects.

2/16/2025 27
Program: Write a program that counts the number of objects created of a
particular class.

#include<iostream.h> void main()


#include<conio.h> {
class yahoo clscr();
{
private:
yahoo x, y;
static int n; x.show();
public: yahoo z;
yahoo() x.show();
{ getch();
n++; }
}
void show()
{
cout<<"You have created"<<n<"objects so far."<<endl; Output:
}
}; You have created 2 objects so far.
int yahoo :: n = 0; You have created 3 objects so far.

2/16/2025 28

You might also like