OOPS
Concepts in C#:-
- Calling the method:
Sys10.ShutDown();
- defining the method:
class System
{
void ShutDown()
{
//some
code
}
}
OOPS:-
- It is a programming style / programming paradigm
- All modern programming languages (such as c++, java, c#.net, vb.net, vc++.net etc.) follows OOPS.
- OOPS = Object Oriented Programming Synopsys
- In OOPS, classes and objects must be used
Why
OOPS?
- OOPS has two major benefits:
a)
Re-usability
b)
Security
a) Re-usability: The classes, data members and methods that
are defined once can be used many times in the program.
b) Security: The class can give permissions to the
required other classes only, to access its data members & methods.
Class
- A class is a collection of (set of) data members & methods.
- Syntax:
class ClassName
{
//data members
//methods
}
a)
Data members:
- Are the variables that are declared within the class.
- Used to store the data.
b)
Methods:
- Are the functions defined the class.
- Used to manipulate the data members
example:
class car
{
//data
members
string
CarModel, CarNumber, CarColor;
//methods
void
Start()
{
//some
code
}
void
ChangeGear()
{
//some code
}
void
Brake()
{
//some
code
}
}
Note:
- The methods of a class can access any data member of the same class.
- System doesn't allocate any memory for the class.
- So, we can't call the methods of the class, directly.
- We must declare atleast one object to access data members & methods of the class
Object:
- It is a real example of the class
- It is an instance of the class
- System allocates necessary memory for the object only
Steps
for working with objects:
i) Declare
/ define / construct the object
classname
objectname = new classname( );
(or)
classname
objectname;
objectname
= new classname( );
ii) Call the methods
objectname.method();
Ex:-
Develop
a console app, for user login.
*
read username & password from console and validate the login.
using
System;
namespace
FirstOOPDemo
{
//user-defined class
class Login
{
//data members
public string Username, Password;
//methods
public bool Validate()
{
if (Username == "system" && Password == "manager")
return true;
else
return false;
}
}
//startup class
class Program
{
static void Main(string[] args)
{
//create an object for Login class
Login lg = new Login();
//read username & password from
console
Console.Write("Enter
Username: ");
lg.Username = Console.ReadLine();
Console.Write("Enter
Password: ");
lg.Password = Console.ReadLine();
//call the method
bool b = lg.Validate();
if (b == true)
Console.WriteLine("Successful Login");
else
Console.WriteLine("Invalid login");
Console.ReadKey();
}
}
}
"this"
keyword:-
- It is used to represent "current object", based on which the method is called.
o
Note: you must use an object, to call a method.
- It is a contextual keyword, which can be used only within the methods.
Ex:
"Person" is a class; "ram" and "krishna"
are objects.
ram.Walk();
so
in this case, "ram" is the "current object".
"this" keyword is used to access the
members of current working object.
Ex:-
Develop
a c# console app, to increment salary of an employee.
using
System;
namespace
ThisKeywordDemo
{
//startup class
class Program
{
static void Main(string[] args)
{
//create an object for Employee class
Employee emp;
emp = new Employee();
//read salary from console
Console.Write("Enter
salary: ");
emp.Salary = Convert.ToDecimal(Console.ReadLine());
Console.Write("Enter
increment amount: ");
decimal s = Convert.ToDecimal(Console.ReadLine());
//call the method
emp.Increment(s);
//show result
Console.Write("Salary
is " + emp.Salary);
Console.ReadKey();
}
}
//user-defined class
class Employee
{
//data members
public decimal Salary;
//methods
public void Increment(decimal
sal)
{
this.Salary = this.Salary + sal;
}
}
No comments:
Post a Comment