| using System;
class SetOfAccounts{
public static void Main(){
// Construct accounts:
BankAccount ba1 = new BankAccount("John", 0.02),
ba2 = new BankAccount("Anne", 0.02),
ba3 = new BankAccount("Frank", 0.02);
CheckAccount ca1 = new CheckAccount("Mike", 0.03),
ca2 = new CheckAccount("Lene", 0.03),
ca3 = new CheckAccount("Joan", 0.03);
// Constructs empty sets of accounts:
Set<BankAccount> s1 = new Set<BankAccount>();
Set<CheckAccount> s2 = new Set<CheckAccount>();
// Insert elements in the sets:
s1.Insert(ba1); s1.Insert(ba2);
s2.Insert(ca1); s2.Insert(ca2);
// Establish s1 as an alias to s2
s1 = s2; // Compile-time error:
// Cannot implicitly convert type 'Set<CheckAccount>'
// to 'Set<BankAccount>'
// Insert a BankAccount object into s1,
// and via the alias also in s2
s1.Insert(new BankAccount("Bodil", 0.02));
// Activates some CheckAccount operation on a BankAccount object
foreach(CheckAccount ca in s2)
ca.SomeCheckAccountOperation();
Console.WriteLine("Set of BankAccount: {0}", s1);
Console.WriteLine("Set of CheckAccount: {0}", s2);
}
} |