/*..................Printing student info ......................*/

using System;

class Student
{

//Member data.
string name;
string regno;
string add;
int age;

//Member function
void Accept()
{
Console.WriteLine("Enter the name:\t");
name=Console.ReadLine();

Console.WriteLine("Enter the regno:\t");
regno=Console.ReadLine();

Console.WriteLine("Enter the address:\t");
add=Console.ReadLine();

Console.WriteLine("Enter the age :\t");
age=Convert.ToInt32(Console.ReadLine());
}
void Display()
{
Console.WriteLine("The name of student is :{0}",name);
Console.WriteLine("The regno of student is :{0}",regno);
Console.WriteLine("The age of student is :{0}",age);
Console.WriteLine("The add of student is :{0}",add);
}
public static void Main(string [] arg)
{
Student S=new Student();
S.Accept();
S.Display();
}
}

/*...................Fibonacci series.................*/

using System;

class Fibonacci
{
int f[] =new int[15];

void Accept()
{
f[0]=0;
f[1]=1;
Console.WriteLine("Enter the num:\t");
int num=Convert.ToInt32(Console.ReadLine());
}

void Display()
{
for(int i=2;i<=num;i++)
{
f[i]=f[i-1]+f[i-2];
Console.WriteLine(f[i]);
}
}

}
class Result
{
public static void Main(string [] arg)
{
Fibonacci F=new Fibonacci();
F.Accept();
F.Display();
}
}

Before coming to methods and there types first we shall know what r the features of Methods....

  • Method is a set of one or more statements or instructions.
  • Methods r mainly used for data hiding (i.e) providing the facility of encapsulation and also abstraction.
  • They allow the user to implement the concept of reusability .
  • They play a key role in modular programming ,when a application is divided into methods then it ill be easy to maintain the code and also to debug.
  • Methods r used for performing repetitive tasks such as fetching specific records and text. They allow user to break an application into discrete logical units, which makes the application more readable.
  • We can reuse the code written in a method and can be executed any number of times by calling methods with little change or else no change .
To use methods, you need to perform 2 things mainly
  1. Define method.
  2. Call method.
Defining methods:

defining a method means declaring the elements of its structure. methods r defined with the following syntax......
(para list)
{
//................Method body;
}

  • Access specifier:
    This explains the extent to which a variable and mtd can be accessed.
  • Return type:
    A method can return any type of value, if method does not return any value then use void as the return type.
  • Method name:
    This plays a prominent role in shaping a class. Method names can not be keywords of the programming language and it is a good practice to follow follow pascal case (eg: Methodone). It can not be same as a variable name also .
  • Parameter list:
    This is used to receive and pass the data from one method to other. We can leave the parameter list empty with out any values.
  • Method body:
    Method body mainly contains the code which is required to perform some specific operation.
example:
class Calculator
{
public int AddNumber(int n1, int n2)
{
int res;
res=n1+n2;
return res;
}
}
Types of Methods
  • calling Methods
    After defining a method , you can use it by its name. The method name is followed by parentheses even if the method call has no parameters.
example:
using System;

class Calculator
{
public int AddNumber(int n1, int n2)
{
int res;
res=n1+n2;
return res;
}
static void Main(string [] arg)
{
Calculator C=new Calculator();
int total=C.AddNumber(10,20);
Console.WriteLine("Te result is :{0}",total);
Console.ReadLine();
}
}
Execution process:
  • In the above program the execution starts from main function . so initially memory is allocated dynamically for the Calculator class and a object is created.
  • Nw by using the object we r trying to access the code defined inside the method. When the method (AddNumber) is accessed the actual arguments(10, 20) supplied in the method is passed to the formal parameters(int n1,int n2) and these values r stored in n1 and n2.
  • since the method uses a return type as int, it is the duty of the user to return a value of type int . so after performing the addition operation the data is returned and the returned value is caught by a variable (total) which is in main function.
  • Last but not the least.............. The value is printed.
  • Here We have to remember one thing that return type of the main method is 'void ' so it doesnt return any value.
example:
/*.................. Recursion method...............*/
using System;

class Number
{
public int Factorial(int n)
{
int res;
if(n==1)
return 1;
else
{
res=factorial(n-1)*n;
return res;
}

}
static void Main(string [] arg)
{
Number obj=new Number();
Console.WriteLine("Factorial of 3 is "+obj.factorial(3));
Console.WriteLine("Factorial of 4 is "+obj.factorial(4));
Console.WriteLine("Factorial of 5 is "+obj.factorial(5));
Console.ReadLine();
}
}

In above program, the factorial() method is recursive method. If the value entered by the used is not 1, this method will call itself.

/*.............. Progr@m to calculate area of rectangle and square.........*/

using System;

class Area
{
static int res;//.............declaring static variable.

public static void RecArea()//........... defining a static func.
{
int l, b;

Console.WriteLine("Enter the length :\t");
l=Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Enter the breadth :\t");
b=Convert.ToInt32(Console.ReadLine());

res=l*b;
Console.WriteLine("The area of a rectangle is :{0}",res);

// (or) Console.WriteLine("The area of rectangle is :\t"+res);
}
public static void SqrArea()//........... defining a static func.
{
int side;

Console.WriteLine("Enter the side of the square:\t");
side=Convert.ToInt32(Console.ReadLine());

res=side*side;
Console.WriteLine("The area of a square is :{0}",res);
}
}
class Result
{
public static void Main(string [] arg)
{
int option;
Area a=new Area();

Console.WriteLine("Main Menu");
Console.WriteLine("1. Area of Rectangle ");
Console.WriteLine("2. Area of Square ");
Console.WriteLine("Enter the option :\t");
option=Convert.ToInt32(Console.ReadLine());

switch(option)
{
case 1:

a.RecArea();
break;
case 2:
a.SqrArea();
break;
default:
Console.WriteLine("oops..!.............Wrong choice buddy, u can leave ");
break;
}
Console.ReadLine();
}
}

Note:

steps to compile and execute
  1. save teh file with .cs extension (i.e) Area.cs
  2. compilation:
    csc Area.cs
  3. execution:
    Area.cs (or) Area.exe

Access specifier defines the scope of a class member. We generally use these access specifiers to give complete security to the data . We use different type of access specifiers to specify the extent of visibility of a class member. In C# we use totally 5 types of access specifiers

  1. public
  2. private
  3. protected
  4. internal
  5. protected internal
  • public access specifier
    1. This access specifier allows the member data and functions of a class to be exposed to other class functions and objects .
    2. The member which is declared as public can be accessed from outside the class.
example:

using System;

class Car
{
private string Car_color;
}
class Bike
{
public string bike_color;
}
class Result
{
static void Main(string [] arg)
{
Car Camry=new Car();
Bike unicorn=new Bike():

Camry.Car_color="white"; //.................................. Error..! cant access private var outside class.
unicorn.bike_color="black"; //................................assigns black to variable.
Console.ReadLine();
}
}

example 2:
using System;
class Car
{
public string color;
public void Honk()
{
Console.WriteLine(" Member function invoked");
}
}
class Result
{
static void Main(string [] arg)
{
car Camry =new Car();
Camry.Honk();//.............. displays the msg.........................Member function invoked.
Console.ReadLine();
}
}

  • private access specifier
    1. This access specifier allows the private data of a class to be hidden from out side classes.
    2. Only the members of the same class r provided permissions to access the private members.
example:

using System;

class Car
{
private string Model;

void Honk()
{
Console.WriteLine("beep beep....!");
}
public void setModel
{
Console.WriteLine("Enter the model num:\t");
Model=Console.ReadLine();
}
public void Display()
{
Console.WriteLine("The model is :\t");
}
}
class Result
{
static int Main(string [] arg)
{
Car Camry=new Car();

Camry.setModel(); //..................Prompts user to enter the model..
Camry.Display();//...............Displays the model.
Camry.Honk();// ................. can not access private methods out side class.
Console.WriteLine(Camry.Model);//...............can not access private mem outside class.

return 0;
Console.ReadLine();
}
}


Note:
When u do not specify any data members as either public, private, protected then the default access specifier is private.
  • Protected access specifier:
    1. This specifier allows a class to expose its member data and functions only to its child classes .
    2. It hides the data to be accessed from other class objects and functions.
example:

using System;
class Car
{
protected string model;
void Method()
{
Console.WriteLine("Member function invoked");
}
public void setModel()
{
Console.WriteLine("Enter the model name :\t");
model=Console.ReadLine();
}
void Display()
{
Console.WriteLine("The model is :\t");
}
}
class Result
{
static int Main(string [] arg)
{
Car civic =new Car();
civic.Method();//......... Error!...can not access this member as it is a protected member.
civic.setModel();//....... accepts the input.
civic.Display();//.......Error!...private member can not be accessed.
Console.WriteLine(civic.model);//.....protected members can not be accessed.

return 0;
Console.ReadLine();
}
}
  • internal access specifier:
    1. Any member that is declared internal can be accessed from any class or method defined within an application in which the member is defined.
    2. When u do not specify any class class as either public, private, protected then the default access specifier for a class is internal.
example:

using System;
class Bike
{
private string col;

internal void Method()
{
Console.WriteLine("Method invoked");
}
}
class Result
{
static void Main(string[] arg)
{
Bike Fz=new Bike();
Console.WriteLine(Fz.col);//...........Error!...can not access private data.
Fz.Method();//............Displays method invoked.
Console.ReadLine();
}
}

Note:
the main difference between public and internal is
  1. public is visible to objects of other class outside the namespace collection and also to the child classes outside the namespace collection.
  2. But internal access specifier is not visible to objects of other class outside the namespace collection and also to the child classes outside the namespace collection.
  • protected internal access specifier:
    1. This specifier allows a class to hide its mem var, mem funct, to be accessed from other class objects and functions, except the child class ,within the application.
    2. This access specifier ecomes important while implementing inheritance.
example:

using System;

class Car
{
protected internal string col;
void Method()
{
Console.WriteLine(" Method invoked");
}
}
class Result
{
static int Main(string [] arg)
{
Car audi =mew Car();
audi.Method();//........Error!... private function.
Console.WriteLine(audi.col);//...... cannot access protected internal members outside the class .

return 0;
Console.ReadLine();
}
}
  1. this Acc.Spec has the similar properties as that of internal and the only dfference is that it can not be visible to objects of other class within the same namespace.






Object Oriented Programming mainly deals with concepts such as Abstraction, Encapsulation, Polymorphism, Inheritance.


Abstraction:

  • Abstraction mainly involves extraction of only relevant information.
  • The concepts of abstraction and encapsulation mainly deals with the member functions of the class.
  • abstraction can be explained in other words as ' Looking for what u want ' in an object or a class.
  • after reading this summary of abstratcion one gets a doubt " does abstraction mean that information is unavailable" .....! No, It means that all the information exists, but only the relevant information is provided to the user.
  • we shall learn more abt abstraction by considering the below given example...
example:

Anautomobile salesperson is aware that different people have different preferences. Some r intrested in the mileage of the car, some in its price, some in its engine, and some in its style.

So every individual have diffferent views and wants . Although every individual is intrested in diff aspects of the car finally tey all come to the category of buying a car. The sales man knows the details of the car , but he presents only the relevant information to potential to the customer. As a result, the salesman practices abstraction and presents only relevant details to customer.


Encapsulation:

  • Encapsulation means packing of one or more components together.
  • It short encapsulation means ' to enclose it or as if in a capsule' .
  • Encapsulation can be defined as " the process of enclosing one or more items within a physical or logical package ", it involves preventing access to non-essential data.
example:

When you plug in the cord of a vacuum cleaner and turn on the switch, the vacuum cleaner starts. You do not see the complex process needed to actually convert electricity into suction power. In other words , the exact working of the cleaner has been encapsulated. Therefore, encapsulation is also explained as information hiding or data hiding because it involves hiding many of the important details of an object from the user.

Abstraction and encapsulation are realated features. abstraction enables you to make relevant information visible. Encapsulation enables you to package information to impent the desired level of abstaraction. Therefore, encapsulation assists abstraction by providing a means of suppressing the non essential details.
the feature of encapsulation is achieved by access specifiers............

Newer Posts Older Posts Home