/* * C# Programmers Pocket Consultant * Author: Gregory S. MacBeth * Email: gmacbeth@comporium.net * Create Date: June 27, 2003 * Last Modified Date: */ using System; namespace Client.Chapter_5___Building_Your_Own_Classes { public class OverloadingClasses { static void Main(string[] args) { A MyA = new A(); MyA.Display(); MyA.Display(10); } } class A { public void Display() { Console.WriteLine("No Params Display Method"); } public void Display(int A) { Console.WriteLine("Overloaded Display {0}", A); } } }
Month: February 2011
A simple example of recursion
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple example of recursion.
using System;
class Factorial {
// This is a recursive function.
public int factR(int n) {
int result;
if(n==1) return 1;
result = factR(n-1) * n;
return result;
}
// This is an iterative equivalent.
public int factI(int n) {
int t, result;
result = 1;
for(t=1; t <= n; t++) result *= t;
return result;
}
}
public class Recursion {
public static void Main() {
Factorial f = new Factorial();
Console.WriteLine("Factorials using recursive method.");
Console.WriteLine("Factorial of 3 is " + f.factR(3));
Console.WriteLine("Factorial of 4 is " + f.factR(4));
Console.WriteLine("Factorial of 5 is " + f.factR(5));
Console.WriteLine();
Console.WriteLine("Factorials using iterative method.");
Console.WriteLine("Factorial of 3 is " + f.factI(3));
Console.WriteLine("Factorial of 4 is " + f.factI(4));
Console.WriteLine("Factorial of 5 is " + f.factI(5));
}
}
[/csharp]
Automatic type conversions can affect overloaded method resolution
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ /* Automatic type conversions can affect overloaded method resolution. */ using System; class Overload2 { public void f(int x) { Console.WriteLine("Inside f(int): " + x); } public void f(double x) { Console.WriteLine("Inside f(double): " + x); } } public class TypeConv { public static void Main() { Overload2 ob = new Overload2(); int i = 10; double d = 10.1; byte b = 99; short s = 10; float f = 11.5F; ob.f(i); // calls ob.f(int) ob.f(d); // calls ob.f(double) ob.f(b); // calls ob.f(int) -- type conversion ob.f(s); // calls ob.f(int) -- type conversion ob.f(f); // calls ob.f(double) -- type conversion } }
Demonstrate method overloading
/* C#: The Complete Reference by Herbert Schildt Publisher: Osborne/McGraw-Hill (March 8, 2002) ISBN: 0072134852 */ // Demonstrate method overloading. using System; class Overload { public void ovlDemo() { Console.WriteLine("No parameters"); } // Overload ovlDemo for one integer parameter. public void ovlDemo(int a) { Console.WriteLine("One parameter: " + a); } // Overload ovlDemo for two integer parameters. public int ovlDemo(int a, int b) { Console.WriteLine("Two parameters: " + a + " " + b); return a + b; } // Overload ovlDemo for two double parameters. public double ovlDemo(double a, double b) { Console.WriteLine("Two double parameters: " + a + " "+ b); return a + b; } } public class OverloadDemo { public static void Main() { Overload ob = new Overload(); int resI; double resD; // call all versions of ovlDemo() ob.ovlDemo(); Console.WriteLine(); ob.ovlDemo(2); Console.WriteLine(); resI = ob.ovlDemo(4, 6); Console.WriteLine("Result of ob.ovlDemo(4, 6): " + resI); Console.WriteLine(); resD = ob.ovlDemo(1.1, 2.32); Console.WriteLine("Result of ob.ovlDemo(1.1, 2.2): " + resD); } }
Return an array
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Return an array.
using System;
class Factor {
/* Return an array containing the factors of num.
On return, numfactors will contain the number of
factors found. */
public int[] findfactors(int num, out int numfactors) {
int[] facts = new int[80]; // size of 80 is arbitrary
int i, j;
// find factors and put them in the facts array
for(i=2, j=0; i < num/2 + 1; i++)
if( (num%i)==0 ) {
facts[j] = i;
j++;
}
numfactors = j;
return facts;
}
}
public class FindFactors {
public static void Main() {
Factor f = new Factor();
int numfactors;
int[] factors;
factors = f.findfactors(1000, out numfactors);
Console.WriteLine("Factors for 1000 are: ");
for(int i=0; i < numfactors; i++)
Console.Write(factors[i] + " ");
Console.WriteLine();
}
}
[/csharp]
Use a class factory
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use a class factory.
using System;
class MyClass {
int a, b; // private
// Create a class factory for MyClass.
public MyClass factory(int i, int j) {
MyClass t = new MyClass();
t.a = i;
t.b = j;
return t; // return an object
}
public void show() {
Console.WriteLine(“a and b: ” + a + ” ” + b);
}
}
public class MakeObjects {
public static void Main() {
MyClass ob = new MyClass();
int i, j;
// generate objects using the factory
for(i=0, j=10; i < 10; i++, j--) {
MyClass anotherOb = ob.factory(i, j); // make an object
anotherOb.show();
}
Console.WriteLine();
}
}
[/csharp]
Add a method that takes two arguments
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add a method that takes two arguments.
using System;
class ChkNum {
// Return true if x is prime.
public bool isPrime(int x) {
for(int i=2; i < x/2 + 1; i++)
if((x %i) == 0) return false;
return true;
}
// Return the least common denominator.
public int lcd(int a, int b) {
int max;
if(isPrime(a) | isPrime(b)) return 1;
max = a < b ? a : b;
for(int i=2; i < max/2 + 1; i++)
if(((a%i) == 0) & ((b%i) == 0)) return i;
return 1;
}
}
public class ParmDemo1 {
public static void Main() {
ChkNum ob = new ChkNum();
int a, b;
for(int i=1; i < 10; i++)
if(ob.isPrime(i)) Console.WriteLine(i + " is prime.");
else Console.WriteLine(i + " is not prime.");
a = 7;
b = 8;
Console.WriteLine("Least common denominator for " +
a + " and " + b + " is " +
ob.lcd(a, b));
a = 100;
b = 8;
Console.WriteLine("Least common denominator for " +
a + " and " + b + " is " +
ob.lcd(a, b));
a = 100;
b = 75;
Console.WriteLine("Least common denominator for " +
a + " and " + b + " is " +
ob.lcd(a, b));
}
}
[/csharp]