The document discusses object-oriented programming concepts in C#, including classes, constructors, destructors, and access modifiers. It provides examples of how to define classes with properties, methods, and constructors. It also explains the differences between public, private, and protected access modifiers and how they control accessibility of class members both within and outside of a class. The document contains code examples demonstrating the usage of classes, constructors, and access modifiers in C#.
3. 3
Class: is an abstraction model used to define a new Data Type
Which May Contain a Combination of encapsulating Data
(Member Variable) Operation That Can Be Perform On Data
(Member Function) And Accessory to Data (properties).
Member Variable = Field
Member Function = Method
Properties = Attributes
5. 5
Class_name var = new Class_name ();
1. Class can declared before or after main
method
2. Or can be declared before class program.
3. we can change class program to any name
6. 6
// Define Class point
class point
{
public int x;
public int y;
public void setx(int a)
{ x = a; }
public void sety(int b)
{ y = b; }
public int getx()
{ return (x); }
public int gety()
{ return (y); }
}
7. 7
// Calling the Class
private void button1_Click(object sender, EventArgs e)
{
point p = new point();
// p.x = 10;
// p.y = 5;
p.setx(6);
p.sety(7);
textBox1.Text = Convert.ToString(p.getx());
textBox2.Text = Convert.ToString(p.gety());
}
8. 8
// Define Class circle
class circle
{
double radius;
double pi = 3.141;
public double area()
{ return (radius * radius * pi); }
public void set_r(double b)
{ radius = b; }
}
9. 9
// Calling the Class
private void button1_Click(object sender, EventArgs e)
{
circle c = new circle();
double x = Convert.ToDouble(textBox1.Text);
c.set_r(x);
textBox2.Text=Convert.ToString( c.area());
}
10. 10
1. Data Abstraction – it is the act of representing the essential
features without including the background details. Data
Hiding- it is a related concept of data abstraction. Unessential
features are hidden from the world.
2. Data Encapsulation – it is the wrapping of data and associated
functions in one single unit.
3. Inheritance – it is the capability of one class to inherit
properties from other class.
4. Polymorphism – it is the ability for data to be processed in
more than one form.
5. Modularity – it is the concept of breaking down the program
into several module. E.g. : Classes, Structure, etc.
11. 11
// Define Class math
class math
{
public int factorial(int x)
{ int f=1;
for( int i=1; i<=x;i++)
f=f*i; return f;
}
public double pow(double a, double b)
{
double p=Math.pow(a,b);
return p;
}
}
12. 12
// Calling the Class
private void button1_Click(object sender, EventArgs e)
{
math m = new math();
double x = Convert.ToDouble(textBox1.Text);
double y = Convert.ToDouble(textBox2.Text);
int z = Convert.ToInt32(textBox3.Text);
textBox4.Text=Convert.ToString( m.factorial(z));
textBox5.Text=Convert.ToString( m.pow(x,y));
}
13. 13
// Class array Define and Sort // Passing array
class sort
{
int[] ar = new int[10];
public void set_array(int[] a)
{
ar = a;
}
public int[] get_array()
{
Array.Sort(ar); return ((ar));
}
}
14. 14
// Calling the Class
private void button1_Click(object sender, EventArgs e)
{
sort s = new sort();
int[] b = new int[10] { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
s.set_array(b);
b=s.get_array();
for (int i = 0; i < 10; i++)
{
textBox1.Text = textBox1.Text + b[i];
}
}
15. 15class stage2
{
public double rnd( double x)
{
double d=Math.Round(x); // to greater than
return(d);
}
public double flor( double y)
{
double f = Math.Floor(y); // to less than
return (f);
}
public string str_up(string a)
{
return (a.ToUpper());
}
16. 16
public string str_dn(string a)
{
return (a.ToLower());
}
public string get_s;
public string set_s;
public void find_array(string[,] s)
{
for (int j = 0; j < 5; j++)
if (get_s == s[0, j])
{
set_s =s[0,j]+":"+s[1,j]+" = "+ s[2, j];
break;
}
}
}
17. 17
private void button1_Click(object sender, EventArgs e)
{
string[,] st = new string[3, 5] { {ID},{NAME},{AVERAGE}};
stage2 s2 = new stage2();
if (comboBox1.Text == “round")
{
double x = Convert.ToDouble(textBox1.Text);
textBox2.Text = Convert.ToString(s2.rnd(x));
}
else if (comboBox1.Text == “floor")
{
double y = Convert.ToDouble(textBox1.Text);
textBox2.Text = Convert.ToString(s2.flor(y));
}
else if (comboBox1.Text == "upper")
{
string s = textBox1.Text;
textBox2.Text = s2.str_up(s);
}
19. 19
1. Searching According to (ID, Name, Code).
2. List of requests and Total Price.
3. Show Expiration Date of products.
ID Product Price Expired Code
101 Paracetamol 3$ 12/1/2019 0x001
102 Panadol 5$ 20/1/2019 0x002
103 Aspirin 4$ 22/1/2019 0x003
104 Flu Out 2$ 18/1/2019 0x004
20. 20
Suppose that we have a school management system, and
we have to build a system to manage students
information and deal with it. According to the following:
1. The school management system contains 100 students.
2. Suppose, there are only three courses.
Find the average of each student depending on Std_ID.
Hint: Store students info. On one array like a table has shown
below:
Std_ID Std_Name Course1 Course2 Course3
1 AA 67 77 92
2 BB 87 90 55
3 CC 75 57 79
21. 21
Homework :: ??? ???
1. Find summation in array[3,3] using class.
2. Print even numbers and odd numbers in array[3,3].
3. Find summation of even numbers and odd numbers
in array[3,3].
4. Exchange even numbers with odd numbers, keep
using same sequence.
5. Find power value of each elements of array[3,3].
6. Find factorial for each elements of array[10].
Hint: Use only one program to do the above questions.!!!
22. 22
Constructor: Is a special method used when an object of class
is created (used to initialize the data member)
Constructor properties
‽ Constructor has the same name of the class.
‽ Constructor do not return value.
‽ Constructor can be overloaded.
‽ Constructor has public access.
23. 23
➢ Default Construction: has no parameters list.
➢ Parameter Construction: has parameters list.
➢ Copy Construction: has object parameter type.
24. 24
class car
{ string model;
double speed;
public car() // Default Construction
{ model = ""; speed = 0; }
public car(string s, double d) // Parameter Construction
{ model = s; speed = d; }
public car(car k) // Copy Construction
{ model = k.model; speed = k.speed; }
}
private void button1_Click(object sender, EventArgs e)
{ car c1 = new car(); // Default Construction
car c2 = new car("toyota", 220); // Parameter Construction
car c3 = new car(c2); // Copy Construction }
25. 25
class home
{
public string floor;
public double price;
public int garanty;
public home()
{ floor = "One"; price = 20000; }
public home(string s, double d, int g)
{ floor = s; price = d; garanty = g; }
public home(home k)
{ floor = k.floor; price = k.price; garanty = k.garanty; }
}
26. 26
home h1 = new home();
home h2 = new home("Two", 60000, 3);
home h = new home("Three", 100000, 5);
home h3 = new home(h);
textBox1.Text = "Floor: " + h1.floor + " Price:" + h1.price + "
Garanty:" + h1.garanty+" Years";
textBox2.Text = "Floor: " + h2.floor + " Price:" + h2.price + "
Garanty:" + h2.garanty+" Years";
textBox3.Text = "Floor: " + h3.floor + " Price:" + h3.price + "
Garanty:" + h3.garanty+" Years";
30. 30
class cafe
{
public int tea;
public int water;
public int nescafe;
public int pepsi;
public int nargila;
public cafe (int a, int b, int c, int d, int e)
{
tea = a; water = b; nescafe = c; pepsi = d; nargila = e;
}
}
32. 32
➢ Is a method called once an object is disposed, can be used to
cleanup recourse used by the object.
➢ Destructors only used with classes.
➢ A class can only have one destructor.
➢ Destructor cannot be inherited or overloaded.
➢ Destructor cannot be called, They are invoked automatically.
➢ Destructors cannot take a modifiers or have parameters.
~car ()
{
MessageBox.Show("car object is dead");
}
33. 33
All types and members have an accessibility level, which controls
whether they can be used from other code in your assembly or
other assemblies. You can use the following access modifiers to
specify the accessibility of a type or member when you declare it:
Public: The type or member can be accessed by any other code in the
same assembly or another assembly that references it.
Private: The type or member can be accessed only by code in the same
class or structure.
Protected: The type or member can be accessed only by code in the
same class or structure, or in a class that is derived from that class.
Internal: The type or member can be accessed by any code in the same
assembly, but not from another assembly.
34. 34
The public keyword is an access modifier for types and type
members. Public access is the most permissive access level.
There are no restrictions on accessing public members.
Accessibility:
1. Can be accessed by objects of the class
2. Can be accessed by derived classes
Example: In the following example num1 is direct access.
35. 35
Private access is the least permissive access level.
Private members are accessible only within the body of the class or the
structure in which they are declared.
Accessibility:
1. Cannot be accessed by object
2. Cannot be accessed by derived classes
Example: In the following example num2 is not accessible outside the
class.
36. 36
using System;
namespace AccessModifiers
{ class Program
{ class AccessMod
{ public int num1;
int num2; }
private void button1_Click(object sender, EventArgs e)
{ AccessMod ob1 = new AccessMod();
//Direct access to public members
ob1.num1 = 100;
//Access to private member is not permitted
ob1.num2 = 20;
Console.WriteLine("Number one value in main {0}",
ob1.num1);
Console.ReadLine(); } } }
The above program will give compilation error, as access to private is
not permissible. In the below figure you can see the private member
num2 is not available.
38. 38
A protected member is accessible from within the class in which it
is declared, and from within any class derived from the class that
declared this member.
A protected member of a base class is accessible in a derived class
only if the access takes place through the derived class type.
Accessibility:
Cannot be accessed by object
Only by derived classes
39. 39
class Base
{ protected int num1; }
class Derived : Base
{ public int num2; }
private void button1_Click(object sender, EventArgs e)
{
Base ob1 = new Base();
Derived ob2 = new Derived();
ob2.num1 = 20;
// Access to protected member as it is inherited by the Derived class
ob2.num2 = 90;
} } }
In the above program we try to access protected member in main it
is not available as shown in the picture below that num1 is not
listed.