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]

A simple example that uses a parameter

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A simple example that uses a parameter.

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; } } public class ParmDemo { public static void Main() { ChkNum ob = new ChkNum(); for(int i=1; i < 10; i++) if(ob.isPrime(i)) Console.WriteLine(i + " is prime."); else Console.WriteLine(i + " is not prime."); } } [/csharp]

Add a method to Building


   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Add a method to Building. 
  
using System;  
  
class Building {   
  public int floors;    // number of floors 
  public int area;      // total square footage of building 
  public int occupants; // number of occupants 
 
  // Display the area per person.  
  public void areaPerPerson() {  
    Console.WriteLine("  " + area / occupants + 
                      " area per person"); 
  }  
}   
 
// Use the areaPerPerson() method. 
public class BuildingDemo2 {   
  public static void Main() {   
    Building house = new Building();   
    Building office = new Building(); 
 
 
    // 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;  
   
 
    Console.WriteLine("house has:
  " + 
                      house.floors + " floors
  " + 
                      house.occupants + " occupants
  " + 
                      house.area + " total area"); 
    house.areaPerPerson(); 
 
    Console.WriteLine(); 
 
    Console.WriteLine("office has:
  " + 
                      office.floors + " floors
  " + 
                      office.occupants + " occupants
  " + 
                      office.area + " total area"); 
    office.areaPerPerson(); 
  }   
}


           
          


Method overloading test


   

/*
Learning C# 
by Jesse Liberty

Publisher: O&#039;Reilly 
ISBN: 0596003765
*/
 using System;

 namespace MethodOverloading
 {
     public class Time1
     {
         // private member variables
         private int Year;
         private int Month;
         private int Date;
         private int Hour;
         private int Minute;
         private int Second;

         // public accessor methods
         public void DisplayCurrentTime()
         {
             System.Console.WriteLine("{0}/{1}/{2} {3}:{4}:{5}",
                 Month, Date, Year, Hour, Minute, Second);
         }

         // constructors
         public Time1(System.DateTime dt)
         {
             Year =      dt.Year;
             Month =     dt.Month;
             Date =      dt.Day;
             Hour =      dt.Hour;
             Minute =    dt.Minute;
             Second =    dt.Second;
         }

         public Time1(int Year, int Month, int Date,
             int Hour, int Minute, int Second)
         {
             this.Year =     Year;
             this.Month =    Month;
             this.Date =     Date;
             this.Hour =     Hour;
             this.Minute =   Minute;
             this.Second =   Second;
         }
     }

    public class MethodOverloadingTester
    {
       public void Run()
       {
           System.DateTime currentTime = System.DateTime.Now;

           Time1 time1 = new Time1(currentTime);
           time1.DisplayCurrentTime();

           Time1 time2 = new Time1(2000,11,18,11,03,30);
           time2.DisplayCurrentTime();
       }

       static void Main()
       {
          MethodOverloadingTester t = new MethodOverloadingTester();
          t.Run();
       }
    }
 }