Week-3
Week-3
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.).
4
Designing the Interface to Add two numbers
➢ Controls to add:
➢ Customization:
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...
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:
12
OOP
➢ Object-Oriented Programming (OOP) is a programming paradigm based on Classes and objects.
13
Why Use Classes?
14
Classes and Objects in C#
Class
Object
15
Classes and Objects in C#
16
Classes and Objects in C#
17
Classes and Objects in C#
18
Principles of OOP
3. Polymorphism - The ability of different classes to be treated as instances of the same class.
19
Encapsulation
➢ It refers to the practice of restricting direct access to certain details of an object and only exposing necessary
functionality.
20
Why Encapsulation?
➢ 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:
26
Answer
27
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