| |
|
|
| |
|
|
| |
| |
|
class Point
{
public int x_coordinate;
public int y_coordinate;
}
class TopLevel
{
static void Main()
{
Point[] Points = new Point[100]; // give me an array of (unallocated) points; i.e.,
// an array of point references
for (int i = 0; i < 100; i++) // now allocate 100 points in the array Points
Points[i] = new Point();
Points[0].x_coordinate = 100;
}
}
class Point
{
private int x_coordinate;
private int y_coordinate;
public int GetX()
{
return(x_coordinate);
}
public int GetY()
{
return(y_coordinate);
}
public void SetX(int X)
{
x_coordinate = X;
}
public void SetY(int Y)
{
y_coordinate = Y;
}
} // end Point class
class TopLevel
{
static void Main()
{
Point[] Points = new Point[100];
for (int i = 0; i < 100; i++)
Points[i] = new Point();
Points[0].SetX(100);
}
} // end TopLevel class
class XYZPoint
{
public int x_coordinate;
public int y_coordinate;
public int z_coordinate;
}
class XYZPoint : Point
{
public int z_coordinate;
}
class Point
{
public int x_coordinate = -99;
public int y_coordinate = -99;
}
class TopLevel
{
static void Main()
{
Point MyPoint = new Point();
System.Console.WriteLine(System.Convert.ToString(MyPoint.x_coordinate));
System.Console.WriteLine();
System.Console.WriteLine("Hit Return to Exit...");
System.Console.ReadLine();
System.Console.WriteLine();
}
}
This is exactly what happens when you create a new object, namely, a function with the same name as the class is called; this function is a so-called 'constructor' function.
Notice, btw, that you do not yourself have to write the code for this function; in VC#.NET, each class automatically gets a constructor function: here Point().
However, sometimes, you want to write your own constructor function, for instance, if you want the construction of an object to have some side-effects. An example would be that when a new 'customer' object gets created, a corresponding record is created in a database.
Since constructors are functions, we can pass them parameters. In the folowing example we use this mechanism to initialize the x_coordinate and y_coordinate of MyPoint
class Point
{
public int x_coordinate;
public int y_coordinate;
public Point(int value)
{
x_coordinate = y_coordinate = value;
}
}
class TopLevel
{
static void Main()
{
Point MyPoint = new Point(-99);
System.Console.WriteLine(System.Convert.ToString(MyPoint.x_coordinate));
System.Console.WriteLine();
System.Console.WriteLine("Hit Return to Exit...");
System.Console.ReadLine();
System.Console.WriteLine();
}
}
Answer: Yes, indeed! The destructor for the Point class would be written as ~Point(); i.e., same name as the constructor preceeded by a ~. And just as in the case of a constructor, you could pass arguments to the destructor.
First, the rules of a firebrigae:
class FireBrigade
{
static void Main()
{
bool[] buckets = new bool[10];
int water_dumped = 0;
int i, j;
//initialize all buckets to be absent; i.e., nobody has a bucket (not really needed)
for (i = 0; i < 10; i++)
buckets[i] = false;
for (i = 1; i < 21; i++) //simulate 20 buckets to be processed
//update the bucket line
//do all buckets except the last
{
for (j = 8; j >= 0; j--) //work backwards!
if (buckets[j] == true)
{
buckets[j + 1] = true;
buckets[j] = false;
}
//now do the last bucket
if (buckets[9] == true)
{
water_dumped = water_dumped + 1;
buckets[9] = false;
}
//do the first bucket
buckets[0] = true;
//print out the bucket line
for (j = 0; j < 10; j++)
if (buckets[j] == true)
System.Console.Write("X");
else
System.Console.Write(".");
System.Console.Write(" " + System.Convert.ToString(water_dumped));
System.Console.WriteLine();
} // end for (i)
} // end Main()
} // end class
class FireBrigade
{
public static int water_dumped = 0;
public static FireFighter[] FFers;
static void Main()
{
FFers = new FireFighter[10];
int i;
//initialize all firefighters (from end to front!!!)
for (i = 9; i >= 0; i--)
FFers[i] = new FireFighter(i);
//ask the first(!!) FFer to process 20 buckets
for (i = 0; i < 20; i++)
{
FFers[0].process_bucket();
DisplayLine();
}
}
static void DisplayLine()
{
int i;
for (i = 0; i < 10; i++)
if (FFers[i].bucket == true)
System.Console.Write("X");
else
System.Console.Write(".");
System.Console.Write(" " + System.Convert.ToString(water_dumped));
System.Console.WriteLine();
}
} // end class FireBrigade
class FireFighter
{
private FireFighter next_neighbor;
public bool bucket;
//class constructor
public FireFighter(int number_in_line)
{
if (number_in_line < 9) //not last-in-line firefighter
next_neighbor = Fireline.FFers[number_in_line + 1];
else
next_neighbor = null;
bucket = false; //not really needed but always nice
}
public void process_bucket()
{
if (bucket == true) //passing a buck(et)
if (next_neighbor == null) // end-of-the-line FFer
FireBrigade.water_dumped = FireBrigade.water_dumped + 1;
else //recursively call the next neighbor
next_neighbor.process_bucket();
else //receiving a bucket
bucket = true;
}
} // end FireFighter class
//initialize all firefighters (from end to front!!!)
for (i = 9; i >= 0; i--)
FFers[i] = new FireFighter(i);
//class constructor
public FireFighter(int number_in_line)
{
if (number_in_line < 9) //not last-in-line firefighter
next_neighbor = Fireline.FFers[number_in_line + 1];
else
next_neighbor = null;
bucket = false; //not really needed but always nice
}
class Hello
{
static void Main()
{
System.Console.WriteLine("Hello World...\n");
}
}