Day Code Interfaces
Day Code Interfaces
listing 1
public interface ISeries {
int GetNext(); // return next number in series
void Reset(); // restart
void SetStart(int x); // set starting value
}
listing 2
// Implement ISeries.
class ByTwos : ISeries {
int start;
int val;
public ByTwos() {
start = 0;
val = 0;
}
listing 3
// Demonstrate the ByTwos interface.
using System;
class ISeriesDemo {
static void Main() {
ByTwos ob = new ByTwos();
Console.WriteLine("\nResetting");
ob.Reset();
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " +
ob.GetNext());
Console.WriteLine("\nStarting at 100");
ob.SetStart(100);
for(int i=0; i < 5; i++)
Page 1 of 14
C# Programming Purvis Samsoodeen
listing 4
// Implement ISeries and add GetPrevious().
class ByTwos : ISeries {
int start;
int val;
int prev;
public ByTwos() {
start = 0;
val = 0;
prev = -2;
}
listing 5
// Implement ISeries.
class ByThrees : ISeries {
int start;
int val;
public ByThrees() {
start = 0;
val = 0;
}
Page 2 of 14
C# Programming Purvis Samsoodeen
return val;
}
listing 6
// Demonstrate interface references.
using System;
public ByTwos() {
start = 0;
val = 0;
}
Page 3 of 14
C# Programming Purvis Samsoodeen
public ByThrees() {
start = 0;
val = 0;
}
class ISeriesDemo2 {
static void Main() {
ByTwos twoOb = new ByTwos();
ByThrees threeOb = new ByThrees();
ISeries ob;
listing 7
// A character queue interface.
public interface ICharQ {
// Put a character into the queue.
void Put(char ch);
listing 8
// Demonstrate the ICharQ interface.
using System;
Page 4 of 14
C# Programming Purvis Samsoodeen
putloc++;
q[putloc] = ch;
}
getloc++;
return q[getloc];
}
}
listing 9
// A circular queue.
class CircularQueue : ICharQ {
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
Page 5 of 14
C# Programming Purvis Samsoodeen
putloc++;
if(putloc==q.Length) putloc = 0; // loop back
q[putloc] = ch;
}
getloc++;
if(getloc==q.Length) getloc = 0; // loop back
return q[getloc];
}
}
listing 10
// A dynamic circular queue.
// This implementation automatically doubles the
// size of the queue when it is full.
class DynQueue : ICharQ {
char[] q; // this array holds the queue
int putloc, getloc; // the put and get indices
Page 6 of 14
C# Programming Purvis Samsoodeen
putloc++;
if(putloc==q.Length) putloc = 0; // loop back
q[putloc] = ch;
}
getloc++;
if(getloc==q.Length) getloc = 0; // loop back
return q[getloc];
}
}
listing 11
// Demonstrate the queues.
class IQDemo {
static void Main() {
SimpleQueue q1 = new SimpleQueue(10);
DynQueue q2 = new DynQueue(5);
CircularQueue q3 = new CircularQueue(10);
ICharQ iQ;
char ch;
int i;
Page 7 of 14
C# Programming Purvis Samsoodeen
Console.WriteLine();
Console.WriteLine();
}
}
listing 12
// Use a property in an interface.
using System;
Page 8 of 14
C# Programming Purvis Samsoodeen
int Next {
get; // return the next number in series
set; // set next number
}
}
// Implement ISeries.
class ByTwos : ISeries {
int val;
public ByTwos() {
val = 0;
}
Console.WriteLine("\nStarting at 21");
ob.Next = 21;
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " + ob.Next);
}
}
listing 13
// Add an indexer in an interface.
using System;
// An interface indexer.
int this[int index] {
Page 9 of 14
C# Programming Purvis Samsoodeen
// Implement ISeries.
class ByTwos : ISeries {
int val;
public ByTwos() {
val = 0;
}
Console.WriteLine("\nStarting at 21");
ob.Next = 21;
for(int i=0; i < 5; i++)
Console.WriteLine("Next value is " +
ob.Next);
Console.WriteLine("\nResetting to 0");
ob.Next = 0;
Page 10 of 14
C# Programming Purvis Samsoodeen
}
}
listing 14
// One interface can inherit another.
using System;
public interface IA {
void Meth1();
void Meth2();
}
class IFExtend {
static void Main() {
MyClass ob = new MyClass();
ob.Meth1();
ob.Meth2();
ob.Meth3();
}
}
listing 15
interface IMyIF {
int MyMeth(int x);
}
listing 16
class MyClass : IMyIF {
int IMyIF.MyMeth(int x) {
return x / 3;
}
}
listing 17
Page 11 of 14
C# Programming Purvis Samsoodeen
interface IEven {
bool IsOdd(int x);
bool IsEven(int x);
}
// Normal implementation.
public bool IsEven(int x) {
IEven o = this; // reference to invoking object
return !o.IsOdd(x);
}
}
class Demo {
static void Main() {
MyClass ob = new MyClass();
bool result;
result = ob.IsEven(4);
if(result) Console.WriteLine("4 is even.");
else Console.WriteLine("3 is odd.");
listing 18
// Use explicit implementation to remove ambiguity.
using System;
interface IMyIF_A {
int Meth(int x);
}
interface IMyIF_B {
int Meth(int x);
}
Page 12 of 14
C# Programming Purvis Samsoodeen
return x + x;
}
int IMyIF_B.Meth(int x) {
return x * x;
}
class FQIFNames {
static void Main() {
MyClass ob = new MyClass();
listing 19
// Demonstrate a structure.
using System;
// Define a structure.
struct Account {
public string name;
public double balance;
Page 13 of 14
C# Programming Purvis Samsoodeen
listing 20
// Demonstrate an enumeration.
using System;
class EnumDemo {
enum Coin { Penny, Nickel, Dime, Quarter, HalfDollar, Dollar };
Page 14 of 14