What is an object?
In simple words we can say an Object as an "Instance of a Class." Without Objects we cannot say a Class is Existing.
An object is a combination of messages and data. Objects can receive and send messages and use messages to interact with each other. The messages contain information that is to be passed to the recipient object.
Look at the program given below:
using System;
---------------------------------------
namespace prime_no
{
class prime
{
int num;
public void acceptNo()
{
Console.WriteLine("Enter a number:");
num = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("\n");
}
public void primeNosTill()
{
int i, j;
bool answer = false;
Console.WriteLine("The prime numbers till entered number are: ");
for (i = 2; i <= num; i++)
{
for (j = 2; j <>
{
if (i % j == 0)
{
answer = true;
break;
}
}
if (answer == false)
Console.WriteLine(i);
else
answer = false;
}
}
class execute
{
public static void Main(string[] a)
{
prime primeNo = new prime();
primeNo.acceptNo();
primeNo. primeNosTill();
Console.Read();
}
}
}
}
---------------------------------------
The prerequisite of object creation is a class i.e. you can only create an object of a class. For the declaration of a class, the class keyword is used. In the above code, class keyword defines the class prime. The braces know as delimiters, are used to indicate the start and end of a class.
The functions of a class are called member functions. A function is a set of statements that performs a specific task in response to a message. Member functions are declared inside the class. The function declaration introduces the function in the class and function definition contains the function code. The two member functions in the above code are acceptNo and primeNosTill.
The execute class is declared with the Main() method. The execute is used as a class from where the prime class can be instantiated. To use the members of the class, the object of that must be created.
The above code contain class prime and the object has been named primeno.
prime primeno = new prime();
The member functions (acceptNo and primeNosTill), can be accessed through the object (primeno) of the class prime using “.” operator.
primeno.acceptNo();
primeno. primeNosTill();
The acceptNo function accepts the number from the user and primeNosTill function displays all the prime numbers till the number entered by the user.
Thursday, November 6, 2008
Subscribe to:
Post Comments (Atom)
1 comment:
its nice one
Post a Comment