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

Week-3

The document covers Object-Oriented Programming (OOP) principles and practical application in C#, specifically focusing on creating a Windows application using Visual Studio. It details the design of a user interface for adding two numbers, event handling, and the importance of classes and encapsulation in programming. The document also includes examples and exercises related to encapsulation through classes like BankAccount and Rectangle.

Uploaded by

esra güngören
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)
2 views

Week-3

The document covers Object-Oriented Programming (OOP) principles and practical application in C#, specifically focusing on creating a Windows application using Visual Studio. It details the design of a user interface for adding two numbers, event handling, and the importance of classes and encapsulation in programming. The document also includes examples and exercises related to encapsulation through classes like BankAccount and Rectangle.

Uploaded by

esra güngören
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/ 32

Object Oriented Programming (OOP)

Dr. Niayesh Gharaei


Week 6:

Windows Application in Visual Studio

2
3
Windows Application
Form Designer: This is the graphical interface where you design the user interface of your application.

• Drag and Drop controls from the Toolbox (TextBoxes, Buttons, Labels, etc.).

• Customize properties of controls (e.g., changing names, text, size).

4
Designing the Interface to Add two numbers
➢ Controls to add:

1. TextBox (txtNumber1) for the first number.

2. TextBox (txtNumber2) for the second number.

3. Button (btnAdd) to trigger the addition action.

4. Label (lblResult) to display the result.

➢ Customization:

1. Set the button text to "Add".

2. Clear the label's default text.

5
6
Writing the Code (Event Handling)
➢ Double-click on the Add button to open the Click Event Handler.

➢ Event-driven programming: Handling user actions with button clicks and text input.

7
Lets Write Together...

8
Lets Write Together...

private void button1_Click(object sender, EventArgs e)


{
if (txtNumber1.Text == "hello123")
{
l.Text = "Welcome!!!";
}
else
{
l.Text = "Password is not correct!";
}
}

9
Lets Write Together...

10
double num1 = double.Parse(txtNumber1.Text);
double num2 = double.Parse(txtNumber2.Text);
double result = 0;
if (rbSum.Checked)
{ result = num1 + num2;
}
else if (rbMultiply.Checked)
{ result = num1 * num2;
}
else if (rbSubtract.Checked)
{ result = num1 - num2;
}
else if (rbDivision.Checked)
{
if (num2 != 0)
result = num1 / num2;
else
MessageBox.Show("Cannot divide by zero!", "Error", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Please select an operation.", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
txtResult.Text = result.ToString(); 11
Week 6:

Object Oriented Programming

12
OOP
➢ Object-Oriented Programming (OOP) is a programming paradigm based on Classes and objects.

➢ OOP allows for better code organization, reusability, and scalability.

➢ C# is a strongly-typed, OOP-based language.

13
Why Use Classes?

➢ Code Reusability: Write code once and reuse it multiple times.

➢ Modularity: Break down a program into smaller, manageable pieces.

➢ Encapsulation: Protect data and control access through methods.

➢ Abstraction: Hide complex details while exposing only relevant functionality.

➢ Scalability: Make programs easier to maintain and extend.

14
Classes and Objects in C#

➢ A class is a blueprint for creating objects.

➢ An object is an instance of a class.

Class

Object

15
Classes and Objects in C#

➢ A class is a blueprint for creating objects.

➢ An object is an instance of a class.


Attributes or the variables belongs to a Class

16
Classes and Objects in C#

➢ A class is a blueprint for creating objects.

➢ An object is an instance of a class.

Method or the Functions belongs to a Cla

17
Classes and Objects in C#

➢ A class is a blueprint for creating objects.

➢ An object is an instance of a class.

Instances from Class (myCar)

18
Principles of OOP

1. Encapsulation - Hiding implementation details and exposing only necessary components.

2. Inheritance - Reusing and extending existing classes.

3. Polymorphism - The ability of different classes to be treated as instances of the same class.

4. Abstraction - Hiding complex implementation and exposing essential details.

19
Encapsulation

➢ Encapsulation is one of the four fundamental principles of Object-Oriented Programming (OOP).

➢ It refers to the practice of restricting direct access to certain details of an object and only exposing necessary

functionality.

20
Why Encapsulation?

➢ Data Hiding: Prevents unintended modifications of data.

➢ Improved Security: Restricts access to sensitive information.

➢ Modularity: Makes code easier to manage and maintain.

➢ Flexibility: Allows controlled data access through getter and setter methods.

21
Encapsulation in C# (Example)

22
Encapsulation in C# (Example)

23
Encapsulation in C# (Another Example)

24
Question

Write a C# program that demonstrates encapsulation by creating a BankAccount class. The class should have:
1.Private fields:

•accountNumber (string) – Stores the account number.

•balance (double) – Stores the account balance.


2.Public methods:
•SetAccountNumber(string accNum): Sets the account number (ensure it's not empty).
•GetAccountNumber(): Returns the account number.
•Deposit(double amount): Adds money to the balance (ensure the amount is positive).
•Withdraw(double amount): Deducts money from the balance (ensure sufficient funds).
•GetBalance(): Returns the account balance.
25
Answer

26
Answer

27
Question

Write a C# program that demonstrates encapsulation by creating a Rectangle class.


The class should have:
➢ Private fields:
1. length (double) – Stores the length of the rectangle.
2. width (double) – Stores the width of the rectangle.
➢ Public methods:
1. SetLength(double len): Sets the length (ensure it's positive).
2. SetWidth(double wid): Sets the width (ensure it's positive).
3. GetLength(): Returns the length.
4. GetWidth(): Returns the width.
5. CalculateArea(): Returns the area of the rectangle.
28
Question

29
Question

30
Question
Create a C++ class Circle that encapsulates the radius of a circle and provides the following
functionalities:
1. Private data member radius (to store the radius).
2. Public setter and getter methods:
• setRadius(double r): Sets the radius (ensure the radius is positive).
• getRadius(): Returns the radius.
3. Public methods:
• calculateArea(): Returns the area of the circle.
• calculateCircumference(): Returns the circumference of the circle.

31
Encapsulation Using Properties
C# provides properties to enforce encapsulation more cleanly:

32

You might also like