Lecture 2
Lecture 2
Lecture 02
CONTENT
Object Oriented Programming (OOP)
Objects
Properties of Object
Functions of Object
Classes
Declaring a Class
Access Specifiers
Creating Objects
Examples
3
Objects can represent entities from the real world, such as a person, place, or thing.
In OOP, both the data and the functions that operate on the data are encapsulated within the object.
OOP enhances code readability and simplifies the learning and modification of programs.
It is a robust technique used to develop software by analyzing and designing applications in terms of objects.
It focuses on organizing data and the functions that operate on that data into a single entity, known as an object. [i.e. instead of
handling data and its related operations separately, OOP combines them, allowing the data and its associated procedures to be treated as a cohesive unit. This
approach simplifies how data is managed and manipulated within a program, ensuring that all operations related to the data are directly tied to the object itself.]
Some of the key object-oriented programming languages that have been developed include: C++, Python, Ruby, PHP, Java.
4
CLASSES
Classes are templates (framework) for creating objects in OOP. They enable the design of various objects by defining their properties and functions.
REAL-WORLD MODELING
OOP is established on real-world modeling, where entities possess properties and functionalities. In this context, objects have data representing their attributes
and functions that define their behaviors.
REUSABILITY
OOP facilitates data and code reusability. Inheritance allows programmers to utilize existing code to develop new programs efficiently.
INFORMATION HIDING
OOP enables programmers to conceal critical data from users through encapsulation, ensuring data protection and integrity.
POLYMORPHISM
The capability of an object to exhibit different behaviors in various contexts (e.g. distinct situations or environments).
5
Objects
An object represents a real-world entity, such as a person, thing, or concept, and is identified by its name.
Examples:
1. Physical Objects
Objects
2. Elements of Computer-User Environment
Structures: Represents a collection of
File Management (e.g., File explorers, Folders, Shortcuts) different data types grouped together.
Classes: Encapsulates data and methods,
Application Elements (e.g., Dialog boxes, Context menus, Status bars) allowing for OOP.
Enumerations: Defines a variable that can
Web Elements (e.g., Hyperlinks, Web forms, Search bars) hold a set of predefined constants.
Unions: Similar to structures but allows
3. User-defined Data Types storing different data types in the same
memory location.
Arrays: A collection of elements of the same
Structures (e.g., a Student structure containing name, age, and grades).
data type, defined by the user
Records: A collection of fields, possibly of
Classes (e.g., a Car class with properties like make, model, and methods like start and stop).
different data types, typically used in
databases.
Enumerations (e.g., Days representing Sunday, Monday, Tuesday, etc.).
Typedefs: Creates an alias for an existing
data type for ease of use.
Unions (e.g., a Data union that can hold either an integer or a float).
Arrays (e.g., an array of Integers or an array of Strings) NOTE: Some examples (like classes and enumerations) directly
relate to OOP, others (like structures and unions) can be used in
Records (e.g., a Book record with title, author, and ISBN). an OOP context but are not as integral to its principles. Classes
are the primary means of defining objects in OOP, while the
Typedefs (e.g., typedef int Integer; for defining Integer as an alias for int). others can support or complement object-oriented design.
7
Properties of Object
The characteristics of an object are referred to as its properties or attributes, with each object
possessing its unique set of properties. These properties serve to describe the object.
For instance: The properties of a Person object may include the following:
Name, Age, Gender, Height, Weight, Address, Phone Number, Email, Occupation,
Nationality.
Manufacturer, Model, Year, Color, Engine Size, Fuel Type, Mileage, Transmission (e.g.,
automatic, manual), VIN (Vehicle Identification Number, a unique code for each car.),
Owner.
8
Properties of Object
The collection of attribute values for a specific object is referred to as its state. This indicates that the state of an
object can be defined by the values assigned to its attributes.
The table below illustrates the attribute values for a Car object.
Car
Manufacturer Toyota
Model Camry
Year 2021
Color Blue
Engine Size 2.5L
Fuel Type Gasoline
Mileage 30,000 miles
Transmission Automatic
VIN 1HGBH41JXMN109186
Owner John Doe
9
Functions of Object
An object is capable of executing various tasks and actions. The actions that an object can carry out are referred to
as functions or methods.
For instance, the Car object can perform the following functions:
Start Engine, Stop Engine, Accelerate, Brake, Turn, Refuel, Play Music, Lock Doors, Turn On Headlights,
Check Mileage.
The collection of all functions associated with an object defines its behavior. This implies that the complete
behavior of an object can be assessed based on the list of its functions.
Classes
A collection of objects that share the same properties and functions is referred to as a class.
A class serves to define the characteristics of the objects and acts as a template (framework) for creating distinct
objects of the same type.
For Instance: A Person class can be utilized to outline the attributes (properties) and methods (functions)
associated with a person. This class can generate various objects of the Person type, e.g., Waleed, Junaid, and
Faraz.
While all objects within the Person class will possess identical characteristics (properties) and functions, the
specific values for each object may differ. These values are assigned after the object has been created.
For Example: Waleed, Junaid, and Faraz represent three instances of the Person class. Likewise, myJournal
and yourJournal can serve as two instances of the Journal class.
11
Declaring a Class
A class is declared in a manner similar to that of a structure, using the keyword
class, but with additional features like inheritance and polymorphism.
A class declaration defines the variables and functions that are shared among all
objects of that class.
The variables defined within a class are referred to as member variables or data
members, while the functions declared in a class are known as member
functions.
12
Declaring a Class
Syntax
Identifier: This refers to the name of the class, which must adhere to the same naming conventions as variable
declarations.
The class declaration always ends (concludes) with a semicolon. All data members and member functions are
defined within the braces, referred to as the body of the class.
13
Declaring a Class
Example:
class Test
{
int n;
char c;
float x;
};
NOTE: The Test class mentioned in example above includes only data members. However, a class can encompass
both data members and member functions.
14
Access Specifiers
The commands used to define the access level of class members are referred to as access specifiers. The two
primary access specifiers are as follows:
The private access specifier restricts class members to be accessed only within the class, preventing any
external access.
Members declared with the private access specifier can only be accessed internally and are not accessible from
outside the class.
This specifier serves as the default access level in many programming languages.
Data members are typically declared as private due to their sensitive nature.
The private access specifier safeguards data members from direct external access.
Only the functions defined within the class can access and utilize these private data members.
15
Access Specifiers
2. PUBLIC Access Specifier
The public access specifier allows class members to be accessed both within and outside the
class.
Class members declared as public can be accessed from anywhere in the program.
Member functions are typically declared as public, enabling external access to an object’s
functions.
If both data members and member functions are declared as private, the class cannot be
used directly from outside the class.
16
Access Specifiers
Test
Private:
int a;
char c;
float x;
Public:
show() Outside the class
input()
Access Specifiers
In FIGURE 1:
The data members a, c, and x of the Test class are inaccessible from outside
the class due to being declared as private.
The member functions show() and input() are accessible externally as they are
declared public.
Data members are accessed and modified indirectly through these public
member functions.
18
Creating Objects
A class serves as a model or prototype for creating objects, functioning like a new
data type that includes both data and functions.
When an object is instantiated, memory is allocated for all data members defined
in the class, based on their data types.
Creating Objects
Syntax
The syntax for instantiating an object from a class is as follows:
class_name object_name;
class_name: It refers to the class name from which the object is being instantiated.
object_name: This is the name assigned to the object being created. The naming rules for objects follow the
same conventions as those for declaring variables.
Example:
Test obj;
The example above declares an object named obj of type Test. This object includes all the data members
defined within the Test class.
20
Member functions can only be executed after the object has been created.
21
object_name.function();
object_name: It refers to the name of the object for which the member function is to be executed.
function: It denotes the name of the member function to be executed, with any necessary parameters passed
within parentheses.
Dot operator: The object name and member function are distinguished by the dot operator.
Example:
Test obj;
obj.input();
22
Examples
Example 1: Create a class called "MyClass":
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
Code Explanation:
The class keyword is used to create a class called MyClass.
The public keyword is an access specifier, which specifies that members (attributes and
methods) of the class are accessible from outside the class.
Inside the class, there is an integer variable myNum and a string variable myString. When
variables are declared within a class, they are called attributes.
At last, end the class definition with a semicolon ;.
23
Examples
Example 2: Create an object called "myObj" and access the attributes:
int main() {
MyClass myObj; // Create an object of MyClass
Examples
Example 3: Multiple Objects: create multiple objects of one class.
// Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};
int main() {
// Create an object of Car
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;