Classes and Objects
Class is real time entity allow you to control all the
functions that can be applied to given set of data as well as access data.
Syntax:
Class <Classname>
{
<access-modifier> [static] <returntype>
Methodname(<signature>);
<access-modifier> [static] <variable type>
variablename < = variable-value>
}
A class is similar to container. It contains data members,
member functions and also other classes.
Let’s take an example of class
namespace ConsoleApplication3
{
class Employee
{
int EmployeeId = 10;
public void
GetEmployee()
{
}
}
}
In the above code, in class “Employee” we can add all
functionalities related to employee object means its leave details, salary
details, Documents etc.
Let’s first understand what is mean by object:
Object is instance of a class. Let’s take example of “Car”,
Car is a class and BMW is a object of class.
By using object of a class we can access the functionalities
defined inside the class.
In below code, there is statement for How to create object
of a class:
Employee Emp=new Employee();
By using new keyword we create the object of class.
“One more thing about
class, class has only public access modifier it cannot be private, protected
and protected internal , It gives error “Error : Elements defined in a
namespace cannot be explicitly declared as private, protected, or protected
internal”
“Another thing is by default all members of a class are
private”
0 Comments