0% found this document useful (0 votes)
5 views

Week 02

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Week 02

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Mansoura University

Faculty of Computers and Information Sciences


First Semester 2023-2024

Object Oriented Programming


(OOP)
Week: 02
Grade: 1st
Eng. Amany Sherif | Computer Science Department
Table of contents
01 02 03
What is What is What is
OOP! Class! objects!

04 05 06
OOP Practical Problem
Vocabulary Example! Solving
1 What’s OOP:

Object-Oriented Programming (OOP) is a programming


paradigm that organizes code into objects, which are
instances of classes. These objects contain data (attributes)
and behavior (methods) related to a specific concept or
entity.
2 What is Class:
• A class is a user-defined data type.

• It consists of data members and member functions, which can


be accessed and used by creating an instance of that class.

• It represents the set of properties or methods that are common to


all objects of one type.

• A class is like a blueprint for an object.


3 What’s Object!
• Object is an instance of a class.
• All data members and member functions of the class can be
accessed with the help of objects.
• When a class is defined, no memory is allocated, but memory
is allocated when it is instantiated (i.e. an object is created).
4 OOP Vocabulary:
• Class: a blueprint consisting of methods and attributes.
• Object: an instance of a class.
• Attribute/ field/ member: a descriptor or characteristic. Examples
would be color, length, size, etc. These attributes can take on
specific values like blue, 3 inches, large, etc.
• Method: an action that a class or object could take.
• Modifiers: Access Modifiers or Access Specifiers in a class are
used to assign the accessibility to the class members, i.e., they set
some restrictions on the class members so that they can’t be
directly accessed by the outside functions.
Class

Attributes: Methods:
• Color. • ChangePrice()
• Size. • ChangeSize()
• Style.
• Price.
Class Object

Attributes: Attributes: Attributes: Attributes:


• Color: Pink. • Color: Green. • Color: Yellow. • Color: Black.
• Size: Small. • Size: Medium. • Size: Large. • Size: x-Large.
• Style: T-Shirt. • Style: T-Shirt. • Style: T-Shirt. • Style: T-Shirt.
Attributes: Methods: • Price: 16$. • Price: 30$. • Price: 22$.
• Color. • Price: 20$.
• Size. • ChangePrice()
• Style. • ChangeSize()
Methods: Methods: Methods: Methods:
• Price.
• ChangePrice() • ChangePrice() • ChangePrice() • ChangePrice()
• ChangeSize() • ChangeSize() • ChangeSize() • ChangeSize()
5 Practical Example using VS

practice to declare class, object,


attributes, methods and modifires!
6 Problem Solving (1/2):
• Write a program that defines a class named Battery. The Battery
class should contains a public field of type int named energy and
a public static method named Charge with the following three
parameters:
• source: a battery object.
• target: a battery object.
• amount: an integer variable.
• The Charge method should remove the energy specified by the
amount variable from the source battery and add it to the target
battery.
6 Problem Solving (2/2):

• In the Main method, create two Battery objects and set the
energy field for each of them to 100.

• Then print the energy value for each battery.

• Afterward, call the Charge method to move an energy amount of


50 from one battery to the other. Then again print the energy
value for each battery.
7 Problem Solving (1/2):
• Write a program that defines a class named Employee. The
Employee class should contains a public field of type int named
id and a public field of type string named name and a public field
of type decimal named salary static method named Bonus with
the following three parameters:
• salary: a decimal variable.
• amount: an integer variable.
• The Bonus method should add the amount of bonus to the basic
salary.
7 Problem Solving (2/2):
• In the Main method, create two Employee objects and set the
name field for each of them to (“Ahmed”, “Sara”) respectively,
set the id field for each of them to (1254, 1789) and set the salary
field for each of them to (5000, 4000) respectively.

• Then print the name, id, salary value for each Employee.

• Afterward, call the Bonus method to add amount 1000 for each
of them as bonus. Then again print the name, id, salary value for
each Employee.
Thanks!
Do you have any questions?
[email protected]

You might also like