/*
* 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 MyMainClass13
{
static void Main(string[] args)
{
//The function called is based
//upon the type called by new.
B MyB = new C();
MyB.Display(); //Calls C's Display
}
}
abstract class A
{
public abstract void Display();
}
class B: A
{
public override void Display()
{
Console.WriteLine("Class B's Display Method");
}
}
class C: B
{
public override void Display()
{
Console.WriteLine("Class C's Display Method");
}
}
class D: C
{
public override void Display()
{
Console.WriteLine("Class D's Display Method");
}
}
}
Virtual and overload
/*
* 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 MyMainClass12 {
static void Main(string[] args)
{
//The function called is based
//upon the type called by new.
B MyB = new C();
D MyD = new D();
MyB.Display(); //Calls C Display
MyD.Display(); //Calls D Display
}
}
public interface A
{
void Display();
}
class B: A
{
public virtual void Display()
{
Console.WriteLine("Class B's Display Method");
}
}
class C: B
{
public override void Display()
{
Console.WriteLine("Class C's Display Method");
}
}
class D: C
{
public override void Display()
{
Console.WriteLine("Class D's Display Method");
}
}
}
Virtual keyword can be used to start a new inheritance ladder
using System;
public class Class1 {
public static void Main(string[] strings) {
BankAccount ba = new BankAccount();
Test1(ba);
SavingsAccount sa = new SavingsAccount();
Test1(sa);
Test2(sa);
SpecialSaleAccount ssa = new SpecialSaleAccount();
Test1(ssa);
Test2(ssa);
Test3(ssa);
SaleSpecialCustomer ssc = new SaleSpecialCustomer();
Test1(ssc);
Test2(ssc);
Test3(ssc);
Test4(ssc);
}
public static void Test1(BankAccount account) {
Console.Write("to Test(BankAccount)");
account.Withdrawal(100);
}
public static void Test2(SavingsAccount account) {
Console.Write("to Test(SavingsAccount)");
account.Withdrawal(100);
}
public static void Test3(SpecialSaleAccount account) {
Console.Write("to Test(SpecialSaleAccount)");
account.Withdrawal(100);
}
public static void Test4(SaleSpecialCustomer account) {
Console.Write("to Test(SaleSpecialCustomer)");
account.Withdrawal(100);
}
}
public class BankAccount {
virtual public void Withdrawal(double dWithdrawal) {
Console.WriteLine(" invokes BankAccount.Withdrawal()");
}
}
public class SavingsAccount : BankAccount {
override public void Withdrawal(double dWithdrawal) {
Console.WriteLine(" invokes SavingsAccount.Withdrawal()");
}
}
public class SpecialSaleAccount : SavingsAccount {
new virtual public void Withdrawal(double dWithdrawal) {
Console.WriteLine(" invokes SpecialSaleAccount.Withdrawal()");
}
}
public class SaleSpecialCustomer : SpecialSaleAccount {
override public void Withdrawal(double dWithdrawal) {
Console.WriteLine
(" invokes SaleSpecialCustomer.Withdrawal()");
}
}
Polymorphism
using System;
public class MotorVehicle {
public string make;
public string model;
public MotorVehicle(string make, string model) {
this.make = make;
this.model = model;
}
public virtual void Accelerate() {
Console.WriteLine(model + " accelerating");
}
}
public class Product : MotorVehicle {
public Product(string make, string model) :
base(make, model) {
}
public override void Accelerate() {
Console.WriteLine("Pushing gas pedal of " + model);
base.Accelerate();
}
}
public class Motorcycle : MotorVehicle {
public Motorcycle(string make, string model) :
base(make, model) {
// do nothing
}
public override void Accelerate() {
Console.WriteLine("Twisting throttle of " + model);
base.Accelerate();
}
}
class MainClass {
public static void Main() {
Product myProduct = new Product("Toyota", "MR2");
myProduct.Accelerate();
Motorcycle myMotorcycle =
new Motorcycle("Harley-Davidson", "V-Rod");
myMotorcycle.Accelerate();
}
}
Illustrates method overloading
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example5_9.cs illustrates method overloading
*/
// declare the Swapper class
class Swapper
{
// this Swap() method swaps two int parameters
public void Swap(ref int x, ref int y)
{
int temp = x;
x = y;
y = temp;
}
// this Swap() method swaps two float parameters
public void Swap(ref float x, ref float y)
{
float temp = x;
x = y;
y = temp;
}
}
public class Example5_9
{
public static void Main()
{
// create a Swapper object
Swapper mySwapper = new Swapper();
// declare two int variables
int intValue1 = 2;
int intValue2 = 5;
System.Console.WriteLine("initial intValue1 = " + intValue1 +
", intValue2 = " + intValue2);
// swap the two float variables
// (uses the Swap() method that accepts int parameters)
mySwapper.Swap(ref intValue1, ref intValue2);
// display the final values
System.Console.WriteLine("final intValue1 = " + intValue1 +
", intValue2 = " + intValue2);
// declare two float variables
float floatValue1 = 2f;
float floatValue2 = 5f;
System.Console.WriteLine("initial floatValue1 = " + floatValue1 +
", floatValue2 = " + floatValue2);
// swap the two float variables
// (uses the Swap() method that accepts float parameters)
mySwapper.Swap(ref floatValue1, ref floatValue2);
// display the final values
System.Console.WriteLine("final floatValue1 = " + floatValue1 +
", floatValue2 = " + floatValue2);
mySwapper.Swap(ref floatValue1, ref floatValue2);
}
}
Operator Overloading
using System;
public class Rectangle {
public int width;
public int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public override string ToString() {
return "width = " + width + ", height = " + height;
}
public static bool operator ==(Rectangle lhs, Rectangle rhs) {
Console.WriteLine("In operator ==");
if (lhs.width == rhs.width && lhs.height == rhs.height) {
return true;
} else {
return false;
}
}
public static bool operator !=(Rectangle lhs, Rectangle rhs) {
Console.WriteLine("In operator !=");
return !(lhs == rhs);
}
public override bool Equals(object obj) {
Console.WriteLine("In Equals()");
if (!(obj is Rectangle)) {
return false;
} else {
return this == (Rectangle)obj;
}
}
public static Rectangle operator +(Rectangle lhs, Rectangle rhs) {
Console.WriteLine("In operator +");
return new Rectangle(
lhs.width + rhs.width, lhs.height + rhs.height);
}
}
class MainClass {
public static void Main() {
Rectangle myRectangle = new Rectangle(1, 4);
Console.WriteLine("myRectangle: " + myRectangle);
Rectangle myRectangle2 = new Rectangle(1, 4);
Console.WriteLine("myRectangle2: " + myRectangle2);
if (myRectangle == myRectangle2) {
Console.WriteLine(
"myRectangle is equal to myRectangle2");
} else {
Console.WriteLine(
"myRectangle is not equal to myRectangle2");
}
Rectangle myRectangle3 = myRectangle + myRectangle2;
Console.WriteLine("myRectangle3: " + myRectangle3);
}
}
Overloaded methods with identical signatures cause compilation errors, even if return types are different.
public class MethodOverloadError
{
public int Square( int x )
{
return x * x;
}
public double Square( int y )
{
return y * y;
}
}

