using System;
using System.Collections;
abstract class Employee
{
protected Employee(int employeeId, int hoursWorked)
{
this.employeeId = employeeId;
HoursWorked = hoursWorked;
}
protected int employeeId;
public int EmployeeId
{
get { return employeeId; }
}
protected int HoursWorked;
protected double hourlyCost = -1; // dummy init value
public abstract double HourlyCost
{
get;
}
}
class ContractEmployee : Employee
{
public ContractEmployee(int employeeId, double hourlyWage,
int hoursWorked)
: base(employeeId, hoursWorked)
{
HourlyWage = hourlyWage;
}
protected double HourlyWage;
public override double HourlyCost
{
get
{ return HourlyWage; }
}
}
class SalariedEmployee : Employee
{
public SalariedEmployee(int employeeId, double salary,
int hoursWorked)
: base(employeeId, hoursWorked)
{
Salary = salary;
}
protected double Salary;
public override double HourlyCost
{
get
{
return (Salary / 52) / HoursWorked;
}
}
}
class OverrideProperties
{
public static ArrayList employees = new ArrayList();
public static void PrintEmployeesHourlyCostToCompany()
{
foreach (Employee employee in employees)
{
Console.WriteLine("{0} employee (id={1}) costs {2}" +
" per hour", employee, employee.EmployeeId,
employee.HourlyCost);
}
}
public static void Main()
{
ContractEmployee c = new ContractEmployee(1, 50, 40);
employees.Add(c);
SalariedEmployee s =
new SalariedEmployee(2, 100000, 65);
employees.Add(s);
PrintEmployeesHourlyCostToCompany();
Console.ReadLine();
}
}
Month: February 2011
MyClass class that contains a private courseName instance variable, and a property to get and set its value.
using System;
public class MyClass
{
private string courseName;
public string CourseName
{
get
{
return courseName;
}
set
{
courseName = value;
}
}
public void DisplayMessage()
{
Console.WriteLine( CourseName );
}
}
public class MyClassTest
{
public static void Main( string[] args )
{
MyClass myMyClass = new MyClass();
Console.WriteLine( "Initial course name is: '{0}'
",myMyClass.CourseName );
Console.WriteLine( "Please enter the course name:" );
string theName = Console.ReadLine();
myMyClass.CourseName = theName;
Console.WriteLine();
myMyClass.DisplayMessage();
}
}
Using variable-length argument lists.
using System;
public class VarargsTest
{
public static double Average( params double[] numbers )
{
double total = 0.0; // initialize total
foreach ( double d in numbers )
total += d;
return total / numbers.Length;
}
public static void Main( string[] args )
{
double d1 = 10.0;
double d2 = 20.0;
double d3 = 30.0;
double d4 = 40.0;
Console.WriteLine("d1 = {0:F1}
d2 = {1:F1}
d3 = {2:F1}
d4 = {3:F1}
",d1, d2, d3, d4 );
Console.WriteLine( "Average of d1 and d2 is {0:F1}",Average( d1, d2 ) );
Console.WriteLine( "Average of d1, d2 and d3 is {0:F1}",Average( d1, d2, d3 ) );
Console.WriteLine( "Average of d1, d2, d3 and d4 is {0:F1}",Average( d1, d2, d3, d4 ) );
}
}
Test Polymorphism Virtual Functions
/*
A Programmer's Introduction to C# (Second Edition)
by Eric Gunnerson
Publisher: Apress L.P.
ISBN: 1-893115-62-3
*/
// 01 - Object-Oriented BasicsPolymorphism and Virtual Functions
// copyright 2000 Eric Gunnerson
using System;
public PolymorphismVirtualFunctions
{
public static void CallPlay(MusicServer ms)
{
ms.Play();
}
public static void Main()
{
MusicServer ms = new WinAmpServer();
CallPlay(ms);
ms = new MediaServer();
CallPlay(ms);
}
}
public abstract class MusicServer
{
public abstract void Play();
}
public class WinAmpServer: MusicServer
{
public override void Play()
{
Console.WriteLine("WinAmpServer.Play()");
}
}
public class MediaServer: MusicServer
{
public override void Play()
{
Console.WriteLine("MediaServer.Play()");
}
}
Method override 3
/*
Learning C#
by Jesse Liberty
Publisher: O'Reilly
ISBN: 0596003765
*/
using System;
class Dog
{
private int weight;
// constructor
public Dog(int weight)
{
this.weight = weight;
}
// override Object.ToString
public override string ToString()
{
return weight.ToString();
}
}
public class TesterOverride
{
static void Main()
{
int i = 5;
Console.WriteLine("The value of i is: {0}", i.ToString());
Dog milo = new Dog(62);
Console.WriteLine("My dog Milo weighs {0} pounds", milo.ToString());
}
}
Demonstrates the use of a virtual property to override a base class property
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// VProp.cs -- Demonstrates the use of a virtual method to override
// a base class method.
//
// Compile this program with the following command line:
// C:>csc VProp.cs
namespace nsVirtual
{
using System;
public class VPropclsMain
{
static public void Main ()
{
clsBase Base = new clsBase();
clsFirst First = new clsFirst();
Base.SetString ("This should set the base class property");
First.SetString ("This should set the derived class property");
Console.WriteLine();
Console.WriteLine (Base.GetString());
Console.WriteLine (First.GetString());
}
}
class clsBase
{
public void SetString (string str)
{
StrProp = str;
}
public string GetString ()
{
return (StrProp);
}
virtual protected string StrProp
{
get
{
Console.WriteLine ("Getting Base string");
return (m_BaseString);
}
set
{
Console.WriteLine ("Setting Base string");
m_BaseString = value;
}
}
private string m_BaseString = "";
}
class clsFirst : clsBase
{
override protected string StrProp
{
get
{
Console.WriteLine ("Getting derived string");
return (m_DerivedString);
}
set
{
Console.WriteLine ("Setting derived string");
m_DerivedString = value;
}
}
private string m_DerivedString = "";
}
}
Demonstrates the use of a virtual method to override a base class method
/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa
Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
//
// Virtual.cs -- Demonstrates the use of a virtual method to override
// a base class method.
//
// Compile this program with the following command line:
// C:>csc Virtual.cs
namespace nsVirtual
{
using System;
public class VirtualclsMain
{
static public void Main ()
{
clsBase Base = new clsBase();
clsFirst First = new clsFirst();
clsSecond Second = new clsSecond();
Base.Show();
First.Show();
Second.Show ();
}
}
class clsBase
{
public void Show ()
{
Describe ();
}
virtual protected void Describe ()
{
Console.WriteLine ("Called the base class Describe() method");
}
}
class clsFirst : clsBase
{
override protected void Describe ()
{
Console.WriteLine ("Called the derived class Describe() method");
}
}
class clsSecond : clsBase
{
}
}

