0% found this document useful (0 votes)
7 views26 pages

session 9

Object-oriented programming (OOP) is a programming paradigm that focuses on the concept of 'objects' which encapsulate data and operations. It contrasts with procedural programming, which follows a top-down approach using a list of instructions. Classes serve as blueprints for creating objects, defining their structure and behavior, while access modifiers help manage data visibility and security.

Uploaded by

amoselgpt241
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)
7 views26 pages

session 9

Object-oriented programming (OOP) is a programming paradigm that focuses on the concept of 'objects' which encapsulate data and operations. It contrasts with procedural programming, which follows a top-down approach using a list of instructions. Classes serve as blueprints for creating objects, defining their structure and behavior, while access modifiers help manage data visibility and security.

Uploaded by

amoselgpt241
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/ 26

CODEX

CODEX
HUB
CODE ACADEMY
What IS Object Oriented Programming ?
Object-oriented programming(OOP) is aprogramming paradigmbased on

the concept of "objects" Aprogramming

paradigm: is a style of programming, a way of thinking about software

construction.

A programming paradigm does not refer to a specific language but rather

to a way to build a program or a methodology to apply.


Some languages make it easy to write in some paradigms but not others.
Some Programming Languages allow the programmer to apply more than
one Paradigm.
Example of Previous Programming Paradigm

Procedural Programming
Procedural programming (PP), also known as inline programming takes a top-down
approach. It is about writing a list of instructions to tell the computer what to do
step by step. It relies on procedures or routines.
Procedural Programming Example : Program to Calculate Average of Array Items

Code To declare and initialize Array


Get Input ( )

Call To function To Accept Array Data

CalcAverage ( ) CalcSum ( )
Call To function To Calculate Average

CalcSum ( )
Object-oriented programming(OOP ) is a programming paradigm based
on the concept of "objects"

Object : is a thing (Tangible –Intangible)


Objects in College Management Program

College Environment

Student Cour se Teacher

Section Hall Of fice


Objects in Super market Program

Super Market Environment

Pr oduct Customer Cashier

Loyalty
Cart Ba ger
Card
Objects in College Management Program

College Environment

Student Cour se Teacher

Section Hall Of fice


Object Is comprised Of ?

Object
Data

Operations ( )
Object Is comprised Of ?
Product
Data
1-Product _name ,
2-Product _code
3-Price
4- Producer
5-Discount

Operations ( )
1-Modify Price () 2-Set
Discount () 3-Get
Product Name () 4-Get
Product Price ()
Object Is comprised Of ?
Student
Data
1- Student_name ,
2-University_Id
3-Birth_Date
4- Address
5-GPA
6- Study_Level

Operations ( )
1-Modify GPA()
2-Change Study level ()
3-Get Student Name ()
4-Get Student Address ()
Object Is comprised Of ?
Car
Data
1-Factory,
2- Model
3- Fuel_Capacity
4- No_of_doors
5-Color
6-Shape

Operations ( )
1-Set Factory Name()
2-Change Color ()
3-Get Car Info ()
4- ………..
What is Class ? Why we need It ?
Student 1 Student 2 Student 3
Data: Data:
Data: 1- Student_name ,
1- Student_name , 1- Student_name , 2- University_Id
2- University_Id 2- University_Id 5-GPA
3- Birth_Date 3- Birth_Date 6- Study_Level
4- Address
5-GPA
4- Address Operations ( )
6- Study_Level
6- Study_Level 1-Modify GPA()
Operations ( ) 2-Change Study level ()
Operations ( ) 1-Modify GPA() 3-Get Student Name ()
1-Modify GPA() 2-Change Study level () 4-Get Student Address ()
2-Change Study level () 4-Get Student Address ()
3-Get Student Name ()
4-Get Student Address ()
What is Class ? Why we need It ?

Class Student
Student 1 Student 2 Student 3
Data: Data: Data:
Data: 1- Student_name 1- Student_name
2- University_Id 1- Student_name
1- Student_name , 2- University_Id
3- Birth_Date 2- University_Id
3- Birth_Date
2- University_Id 4- Address 3- Birth_Date
4- Address
3- Birth_Date 5-GPA 4- Address
5-GPA
4- Address 6- Study_Level 5-GPA
6- Study_Level
6- Study_Level
5-GPA Operations ( ) Operations ( )
6- Study_Level Operations ( )
1-Modify GPA() 1-Modify GPA()
1-Modify GPA()
Operations ( ) 2-Change Study level () 2-Change Study level ()
2-Change Study level ()
3-Get Student Name () 3-Get Student Name ()
1-Modify GPA() 3-Get Student Name ()
4-Get Student Address () 4-Get Student Address ()
2-Change Study level () 4-Get Student Address ()
3-Get Student Name ()
4-Get Student Address ()
What is Class ? Why we need It ?
Student 1 Student 3
Data: Data:
Class Student 1- Student_name
2- University_Id 1- Student_name
3- Birth_Date 2- University_Id
Data: 4- Address
5-GPA
3- Birth_Date
4- Address
6- Study_Level 5-GPA
1- Student_name , 6- Study_Level
7- Email
2- University_Id 7- Email
Operations ( ) Operations ( )
3- Birth_Date 1-Modify GPA()
1-Modify GPA()
4- Address 2-Change Study level ()
2-Change Study level ()
3-Get Student Name ()
3-Get Student Name ()
5-GPA 4-Get Student Address ()
4-Get Student Address ()
5-Print Student Info ()
6- Study_Level 5-Print Student Info ()

7- Email Student 2
Operations ( ) Data:
1- Student_name
2- University_Id
1-Modify GPA() 3- Birth_Date
4- Address
2-Change Study level () 5-GPA
3-Get Student Name () 6- Study_Level
7- Email
4-Get Student Address ()
5-Print Student Info () Operations ( )
1-Modify GPA()
2-Change Study level ()
3-Get Student Name ()
4-Get Student Address ()
5-Print Student Info ()
What is Class ? Why we need It ?

Student 1 Data: = Ahmed


1- Student_name , = 1050
2- University_Id
5-GPA =3.75
6- Study_Level =5
Operations ( )
1-Modify GPA()
2-Change Study level ()
3-Get Student Name ()
4-Get Student GPA ()
Objects and Classes
Classes: The Blueprint for Objects
A class defines the structure and behavior of a particular type of
object. It specifies:
Attributes (Fields): The data an object can hold.
Methods (Functions): The actions an object can perform.
Think of a class as a blueprint—just like an architectural plan
defines the structure of a house, a class provides the template
for creating objects with shared characteristics and behaviors.
Objects and Classes
During program execution, a class serves as a template
to create multiple objects in memory as needed.
Each object created from a class is known as an instance
of that class.
Instances have unique data but share the same
structure and behavior defined by the class.
Writing a Class, Step by Step
•A Rectangle object will have the following fields:

Rectangle

length
width

setLength()
setWidth()
getLength()
getWidth()
getArea()
Writing the Code
Rectangle

length
public class Rectangle width
{ setLength()
private: setWidth()
getLength()
float length; getWidth()
getArea()
float width;
}
Data Hiding

Access Modifiers
• An access modifier is a C++ keyword that indicates how a field or method
can be accessed.
• public
When the public access modifier is applied to a class member, the
member can be accessed by code inside the class or outside.
• private
When the private access modifier is applied to a class member, the member
cannot be accessed by code outside the class. The member can be accessed
only by methods that are members of the same class.
Data Hiding
An object hides its private data from external code.
Only the class’s own methods can directly access and modify
its private data.
External code must use public methods to interact with private
data.
This improves security, data integrity, and maintainability,
especially in large projects with multiple developers.
Return
Type Rectangle
Access Method
specifier Name -width : float
-length : float

+ setWidth(w : float) : void


Public: + setLength(len: float): void
+ getWidth() : float
void setLength(float len)
+ getLength() : float
+ getArea() : float
Parameter variable declaration
public class Rectangle
{ Rectangle
private: -width : float
float length; -length : float
float width; + setWidth(w : float) : void
+ setLength(len: float): void
Public:
+ getWidth() : float
void setLength(float len) + getLength() : float
{ + getArea() : float
If (len>=0)
length = len;
Else cout<<“Error , Please Enter positive value”;
}
}
Creating a rectangle object

Rectangle box; Int X; String name;

A Rectangle object
The box
variable holds length: 0.0
the address of address
the Rectangle width: 0.0
object.
Thank you

You might also like