0% found this document useful (0 votes)
8 views43 pages

Chapter 1. Overview

This document provides an overview of Object-Oriented Programming (OOP), outlining its main concepts, characteristics, and advantages. It covers programming paradigms, basic concepts such as classes and objects, and the four main pillars of OOP: abstraction, encapsulation, inheritance, and polymorphism. Additionally, it highlights popular OOP languages and their industry adoption.
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)
8 views43 pages

Chapter 1. Overview

This document provides an overview of Object-Oriented Programming (OOP), outlining its main concepts, characteristics, and advantages. It covers programming paradigms, basic concepts such as classes and objects, and the four main pillars of OOP: abstraction, encapsulation, inheritance, and polymorphism. Additionally, it highlights popular OOP languages and their industry adoption.
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

Chapter 1 – Overview

(Object-Oriented Programming)

1
Outline
1. Programming paradigms
2. Basic concepts
3. Main pillars of OOP
4. OOP languages

2
1.1 Programming paradigms
a. Sequential Programming
b. Procedural Programming
c. Modular Programming
d. Object-Oriented Programming (OOP)
e. Functional Programming
f. Declarative Programming

3
Sequential Programming
• Solves small, simple problems.
• Consists only of a main program.
• The program is a sequential sequence of statements.
• Disadvantages:
• Repeat code segments
• All data is global.
→ uncontroll data access scope.

4
Procedural Programming
• The program is organized into subprograms.
• Subprograms include functions and procedures.
• These are independent and perform specific tasks.

5
Modular Programming
• Procedures/functions are grouped into separate modules.
• The program consists of many small parts.
• Parts interact by calling procedures.

6
Procedural and Modular Programming
Disadvantages:
• Changing shared data requires changing all related functions/procedures.
• The program is difficult to control and expand
Object-Oriented Programming (OOP)
• Focuses on objects and does not
allow data to move freely.
• Data is closely tied to functions.
• Main Characteristics:
• The program is divided into
objects.
• Data is encapsulated, hidden,
and secured.
• Objects interact through
messages,
• The program is designed using a
bottom-up approach.
8
Why is OOP so popular?
• Real-World Modeling
• – OOP mirrors real-world objects, making programming intuitive.
• Code Reusability
• – Features like inheritance and polymorphism reduce redundancy.
• Scalability & Maintainability
• – Modular design makes large applications easier to manage.
• Industry Adoption
• – Widely used in Java, Python, C++, C#, and major frameworks.
• Team Collaboration
• – Encourages structured code, making teamwork efficient.
• Hybrid Flexibility
• – Integrates with functional programming for modern development.

9
1.2 Basic concepts
• Abstract Data Type (ADT)
• Object
• Attributes & Methods
• Class
• Message

10
Abstract Data Type (ADT)
• Data type
oData
oOperations on Data
• Separate the logical characteristics and the implementation

11
• Số nguyên: int, short, long, long long,…
• Số thực: float, double
• Ký tự: char
Khai báo
int a = 1;
string name = “nguyen van a”;
char name[21]; //name chứa choỗi có tối đa 20 ký tự, \0
char *name = new char[21];

12
Abstract Data Type (ADT)
Abstract
Data
Type

Abstract Data Structure


Interface
Operations

13
ADT Example
• Stack ("last in first out" or LIFO).
❑Push
❑Pop
❑Initialize
❑Empty

14
ADT Example
• Stack Interface

15
Stack Implementation

16
Stack Client

17
Object
• An object is an abstract concept that represents real-world entities.
• Object = data (attributes) + methods (behavior).
• An object is an instance of a class.
• Objects that are instances of the same class share the same structure but
may have different states (i.e., different attribute values).

18
Attributes & Methods
Attributes:
• Characteristics that distinguish objects
• Data stored within an object
• Constants, variables, etc.
• Can be of primitive data types or user-defined types
Methods:
• Member functions defined inside the class and associated with the object
• Operate on the data stored within the object
• Also called operations, behaviors, or member functions
19
Class
• A collection of objects of the same type
• A representation of an Abstract Data Type (ADT)
• Used as a user-defined data type
• A blueprint (template) for creating object instances
• Memory is allocated for objects, not for the class itself

20
Example 1: Car

Object: Car01021

Attributes Behavior
Color: Yellow Start, Accelerate,
Reverse, Stop
Class: Car Type: Coupe
Model: Mustang
Attributes Cylinder: 6

Color, Type, Model, Cylinder


Behavior
Start, Accelerate, Reverse, Stop
Object: Car01022

Attributes Behavior
Color: Orange Start, Accelerate,
Type: Coupe Reverse, Stop

Model: Focus
Cylinder: 4
Example 2: Person
Object: Person1

Class: Person Attributes Behavior


Name: Ann Speak,
Attributes Listen, Eat,
Height: 5’ 4”
Run, Walk
Name, Height, Age Age: 21

Behavior
Speak, Listen, Eat, Run, Walk Object: Person2

Attributes Behavior
Name: Adam Speak,
Listen, Eat,
Height: 5’ 9”
Run, Walk
Age: 24

22
Example 3: Shipping Box

sender_name : string
receiver_name : string
cost_per_pound : int int shipping_cost() {
weight : int return cost_per_pound*weight;
}

shipping_cost() : int
Objects of ShippingBox class

Object BoxA
sender_name = Julie
receiver_name = Jill
cost_per_pound = 2
weight = 5
shipping_cost()

Class
ShippingBox
Object BoxB

sender_name = Jim
receiver_name = John
cost_per_pound = 5
weight = 10
shipping_cost()
Class
Scenario:
A customer makes an online purchase on Lazada. After selecting the desired
products and adding them to the shopping cart, the customer proceeds to the
checkout step. At this stage, the customer provides personal information and a
shipping address. The system then verifies and confirms the order and
provides the customer with an order ID to track the order status. Finally, the
system sends the customer a detailed copy of the order via email.
Requirement:
Identify the necessary classes for the above scenario.

25
Class Relationships

Problem: Write a program to draw the objects


points, circles, rectangles, triangles, etc.
• is – a (kind of)
Animal, Cat
Cat is a animal
• has–a (part of)
Circle is part of Logo
Logo has a Circle

27
Message
• Objects interact and communicate with one another by sending messages.

28
Message
• Paradigm for problem solving by interaction among objects
• It follows a natural way of solving problems

Ex. Ann wants to start her car


(1) Ann walks to her car
(2) Ann sends a message to the car to start by turning on the ignition
(3)The car starts

29
Message
• Problem: Ann wants to start her car

Object Ann Object Ann’s car


Name = Ann Color = Yellow
Age = 21 Type = Coupe
Speak() Model = Mustang
Run() Cylinder = 6
Walk() Start()
Accelerate()
Stop()

30
Message
A message consists of:
• The target object
• The name of the method to be invoked
• The required parameters
class Integer { Integer obj1;
attributes: [Link](1);
int i
methods:
setValue(int n)
Integer addValue(Integer j)
}

31
1.3 Four main pillars of OOP

32
Abstraction
• Abstraction is the process of showing only essential/necessary features of an
entity/object to the outside world and hide the other irrelevant information.

Simpler Interface
Reduce the impact
of change

33
Abstraction - Example

Shipping Box Abstraction Class


Attributes sender_name : string
Sender’s name, receiver_name : string
Receiver’s name, cost_per_pound : int
Cost of shipping per pound, weight : int
Weight
Behavior shipping_cost() : int
Calculate shipping cost

34
Encapsulation
• This problem of interdependency of functions in procedural programing is
now solved in OOPs
f() x

Property
Method

35
Procedural programming example
float wage(float bS, int ot, float rate)
{
return bS + ot * rate;
}
int main()
{
float baseSalary = 20000, rate = 20;
int overtime = 10;
float netSalary;
netSalary = wage(baseSalary, overtime, rate);
return 0;
}
36
Solve the same problem using OOP
class employee { Encapsulation
private:
float baseSalary = 10 - Binding data members and
int overTime = 20; member functions together.
float rate = 30; - Providing different levels of
public:
float wage() { scope on how classes, methods,
return (baseSalary + overTime * rate); and attributes can be accessed
} and only allowing access on a
}; need-to-know basis.
int main(int argc, char** argv) { - Using private, public and
employee e1;
cout << [Link](); protected keywords to
return 0; implement encapsulation on
} attributes, methods and classes.

37
Data hiding vs Encapsulation
• Data Hiding: ensures the security of members of a class from illegal access
• protects the data members from being changed or hacked
• Example:
Class BankAccount has a data member AcBalance that is a sensitive
information. We may allow an external application to check the account
balance, but we would not allow the application to change the account
balance. Hence, in this case, we restrict the alternation in account balance
by declaring the account balance attribute private.
• Both work together for a common purpose

38
Inheritance
Base class
• A derived class inherits the SuperClass
attributes and methods of its parent Parent Class
class.
• It provides a powerful mechanism
for organizing and structuring
Derived class
programs. SubClass
Child Class

39
Polymorphism
• Poly means Many and Morph means form
• Methods with the same name may exhibit different behaviors when applied
to different objects or classes.
• It is realized through:
oOverloading
oOverriding

40
Polymorphism

Which draw() method is invoked?


Advantages of OOP
• Eliminates code duplication
• Enables the development of secure and robust programs
• Facilitates extensibility and system upgrades
• Reduces system development time
• The use of messages simplifies interaction with external systems
• Helps manage the complexity of software products

42
1.4 OOP languages
• C++
• C#
• JAVA
• Ruby
• Python
• JavaScript
• And many more
• …

43

You might also like