/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Return an object. using System; class Rect { int width; int height; public Rect(int w, int h) { width = w; height = h; } public int area() { return width * height; } public void show() { Console.WriteLine(width + " " + height); } /* Return a rectangle that is a specified factor larger than the invoking rectangle. */ public Rect enlarge(int factor) { return new Rect(width * factor, height * factor); } } public class RetObj { public static void Main() { Rect r1 = new Rect(4, 5); Console.Write("Dimensions of r1: "); r1.show(); Console.WriteLine("Area of r1: " + r1.area()); Console.WriteLine(); // create a rectange that is twice as big as r1 Rect r2 = r1.enlarge(2); Console.Write("Dimensions of r2: "); r2.show(); Console.WriteLine("Area of r2 " + r2.area()); } }
Month: February 2011
This program creates two Building objects
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // This program creates two Building objects. using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants } // This class declares two objects of type Building. public class BuildingDemo1 { public static void Main() { Building house = new Building(); Building office = new Building(); int areaPP; // area per person // assign values to fields in house house.occupants = 4; house.area = 2500; house.floors = 2; // assign values to fields in office office.occupants = 25; office.area = 4200; office.floors = 3; // compute the area per person in house areaPP = house.area / house.occupants; Console.WriteLine("house has: " + house.floors + " floors " + house.occupants + " occupants " + house.area + " total area " + areaPP + " area per person"); Console.WriteLine(); // compute the area per person in office areaPP = office.area / office.occupants; Console.WriteLine("office has: " + office.floors + " floors " + office.occupants + " occupants " + office.area + " total area " + areaPP + " area per person"); } }
A program that uses the Building class
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // A program that uses the Building class. using System; class Building { public int floors; // number of floors public int area; // total square footage of building public int occupants; // number of occupants } // This class declares an object of type Building. public class BuildingDemo { public static void Main() { Building house = new Building(); // create a Building object int areaPP; // area per person // assign values to fields in house house.occupants = 4; house.area = 2500; house.floors = 2; // compute the area per person areaPP = house.area / house.occupants; Console.WriteLine("house has: " + house.floors + " floors " + house.occupants + " occupants " + house.area + " total area " + areaPP + " area per person"); } }
Create class
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; public class MyTime { // private variables private int year; private int month; private int date; private int hour; private int minute; private int second; // public methods public void DisplayCurrentMyTime() { Console.WriteLine( "stub for DisplayCurrentMyTime"); } } public class Tester { static void Main() { MyTime timeObject = new MyTime(); timeObject.DisplayCurrentMyTime(); } }
Variable in and out a class
/* Learning C# by Jesse Liberty Publisher: O'Reilly ISBN: 0596003765 */ using System; namespace heap { public class Dog { public int weight; } public class TesterClass { public void Run() { // create an integer int firstInt = 5; // create a second integer int secondInt = firstInt; // display the two integers Console.WriteLine("firstInt: {0} secondInt: {1}", firstInt, secondInt); // modify the second integer secondInt = 7; // display the two integers Console.WriteLine("firstInt: {0} secondInt: {1}", firstInt, secondInt); // create a dog Dog milo = new Dog(); // assign a value to weight milo.weight = 5; // create a second reference to the dog Dog fido = milo; // display their values Console.WriteLine("Milo: {0}, fido: {1}", milo.weight, fido.weight); // assign a new weight to the second reference fido.weight = 7; // display the two values Console.WriteLine("Milo: {0}, fido: {1}", milo.weight, fido.weight); } static void Main() { TesterClass t = new TesterClass(); t.Run(); } } }
Illustrates hiding
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example5_4.cs illustrates hiding */ // declare the Car class class Car { public int yearBuilt; public double maximumSpeed; public int Age(int currentYear) { int maximumSpeed = 100; // hides the field System.Console.WriteLine("In Age(): maximumSpeed = " + maximumSpeed); int age = currentYear - yearBuilt; return age; } public double Distance(double initialSpeed, double time) { System.Console.WriteLine("In Distance(): maximumSpeed = " + maximumSpeed); return (initialSpeed + maximumSpeed) / 2 * time; } } public class Example5_4 { public static void Main() { // create a Car object Car redPorsche = new Car(); redPorsche.yearBuilt = 2000; redPorsche.maximumSpeed = 150; int age = redPorsche.Age(2001); System.Console.WriteLine("redPorsche is " + age + " year old."); System.Console.WriteLine("redPorsche travels " + redPorsche.Distance(31, .25) + " miles."); } }
Show name hiding in a derived class
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ // // Hide.cs -- Show name hiding in a derived class // // Compile this program with the following command line: // C:>csc Hide.cs // namespace nsHide { using System; using System.Reflection; public class Hide { static public void Main () { clsBase Base = new clsBase(); clsDerived Derived = new clsDerived (); Base.x = 42; Derived.x = 42; Console.WriteLine ("For the base class:"); Console.WriteLine (" The type stored in clsBase is " + Base.TypeOf()); Console.WriteLine (" MathOp () returns {0,0:F3} for {1}", Base.MathOp(42), 42); Console.WriteLine (" For the derived class:"); Console.WriteLine (" The type stored in clsDerived is " + Derived.TypeOf()); Console.WriteLine (" MathOp () returns {0,0:F3} for {1}", Derived.MathOp(42), 42); } } class clsBase { protected int m_x; public int x { get {return (x);} set {m_x = value;} } public double MathOp (int val) { return (Math.Sqrt ((double) val)); } public string TypeOf () { return ("integer"); } } class clsDerived : clsBase { new protected double m_x; new public double x { get {return (x);} set {m_x = value;} } new public double MathOp (int val) { return ((double) (val * val)); } new public string TypeOf () { return ("long"); } } }