Duhok Polytechnic University
Shekkan Technical Institute
Information Technology Department
Object Oriented Programming (OOP)
Lecture 6
Introduction to OOP
Assistant Lecturer: Dathar A. Hasan
[email protected] زانکۆیا پولیتەکنیکی یا دهۆک 27 - Oct - 2024
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Outlines
• Introduction to OOP in C#.
• Classes and Objects in OOP.
• Class Declaration in C#.
• Access Modifiers.
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Object Oriented Programming (OOP) in C#
• OOP is a programming model where programs are organized around
objects and data rather than action and logic.
• OOP decomposes a problem into many entities called objects and then
builds data and functions around these objects.
• OOP is faster and easier to execute.
• OOP provides a clear structure for the programs.
• OOP makes it possible to create reusable applications with less code and
shorter development time.
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Classes in OOP
• Class is a blueprint that contains a list of variables and functions.
➢ Variables in a class are called (Data Members)
➢ Functions are called (Member Functions).
• A class will not occupy any memory space; it is a logical representation
of data.
• Class is a user-defined data type.
• Each Program may contain one or many classes.
• To use classes in OOP, we need to define objects.
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Classes in OOP
• To create a class, write the keyword "class" followed by the class name.
class Class_Name
{
//field
Data types variable;
//method
}
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Declaring objects (Instance of class)
• The instance of the class is called Object.
• Creating an object will allocate memory and create an instance based on
the class definition.
• The syntax for creating an instance of a class is as follows:
• In this example, S1 is the object’s name, Student is the class from which
the instance is created, and new Student( ) initializes the instance.
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Declaring objects (Instance of class)
• Once a class instance is declared, its fields (data members) can be
accessed and modified, and its methods can be invoked as shown:
Invoking the method
Accessing Data Members Print()
(Name and Age)
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Example 1: Write a C# program that includes a class named (Car), the class has three
data members (NO, Model, and Year). Define a function (set_values) within the class
to set the values for the data numbers. The other function (Print_values) displays the
data members’ values. Declare two objects for the Car class (Toyota and Kia) and
initialize random values for their data members. Then print the objects’ values
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Access Modifiers
• Access modifiers define the accessibility of class data members and methods.
• They determine where the members can be accessed from.
• Here are the primary access modifiers:
• Public: The member is accessible from any other code in the same or another
assembly that references it.
• Private: The member is accessible only within the containing class or struct. It is not
accessible from outside the class.
• Protected: The member is accessible within its class and by derived class instances. It
cannot be accessed from other classes that do not inherit from it.
Note: If we do not assign an access modifier for data members, the default is private.
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
Example 2: Design a class (Customer) that has fields (Name, Address, and Balance).
The name’s value is assigned from outside the class. While the values of Address and
Balance are initialized using the set_value method. Declare three objects then print
their data using print_data method.
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan
1
Thank you
زانکۆیا پولیتەکنیکی یا دهۆک
جامعـة دهــوك التقــنية
Duhok Polytechnic University Prepared By: Dathar A. Hasan