Describe classes and objects in C#.
Create classes and objects.
Explain constructors and destructors in C#.
Add constructor and destructor in a class.
MODIFIER ACCESSIBILITY CONTROL
Private Member is accessible only within the class containing the
member.
Public Member is accessible from anywhere outside the class as well.
It is also accessible in derived classes.
protected Member is visible only to its own class and its derived classes.
class mul
{
public int n, m; //declaring class variables
public void add() //define add() method
{
int s;
s = n + m;
Console.WriteLine("Sum="+s);
}
public void sub() //define sub() method
{
int d;
d = n - m;
Console.WriteLine("Difference=" + d);
}
}
class Program
{
static void Main(string[] args)
{
mul obj1 = new mul(); //obj1 is the object of mul class
obj1.n = Convert.ToInt32(Console.ReadLine());
obj1.m = Convert.ToInt32(Console.ReadLine());
obj1.add(); //calling add() method
obj1.sub(); //calling sub() method
Console.ReadLine();
}
}
Output
This chapter explains:-
›Creation of classes and objects.
›Adding variables and methods in a class.
›Accessing class members
›Creation of constructors and destructors in a class.