Day 4 - Sample Coding

1 view
Skip to first unread message

Nuwan

unread,
Dec 31, 2005, 12:37:55 AM12/31/05
to sliit-mcsd
Here are some of the sample programs we discused

//----------------------------------------------------------------
using System;

class Animal {


public virtual void Speak() {
Console.WriteLine("I can't speak");
}
}

class Mammal : Animal {
public override void Speak() {
Console.WriteLine("Mammal");
}
}

class Cat : Mammal {
public override void Speak() {
Console.WriteLine("Meow");
}
}

class Dog : Mammal {
public override void Speak() {
Console.WriteLine("Bow Wow");
}
}
class Sample {
public static void Main() {

Cat c = new Cat();
c.Speak();

Dog d = new Dog();
d.Speak();

Mammal m;
m = c;

m.Speak();

Animal a = new Dog();
a.Speak();

Dog dg = new Mammal();

// Animal a= new Animal();
// Dog d;
// d = a as Animal;
// d.Speak();


}
}


//------------------------------------------------------------------------------------------
using System;

class Cat {
public void Meaow() {
Console.WriteLine("Meow");
}
}

class Dog {
public void BowWow() {
Console.WriteLine("Bow Wow");
}
}

class Sample {
public static void Main() {
Dog k = new Dog();
Cat p = new Cat();
object op = k;

// Dog temp = (Dog)op;
// temp.BowWow();


print(k);
print(p);
}
public static void print(object o) {

if (o is Dog) {
Dog d = o;
d.BowWow();
}
if (o is Cat) {
Cat c = (Cat) o;
Cat c = o as Cat;
c.Meaow();
}


}
}

//-------------------------------------------------------------------------------

using System;

class Item {
// constructor

static Item() {
Console.WriteLine("static cons called");
}

public Item(int itemno, float price, int qty) {
this.itemno = itemno;
Add(price, qty);
}

public void Add( float price, int qty) {

this.price = price;
this.qty = qty;
}

public float Amt() {

return price*qty;
}
public void Print() {
Console.WriteLine("Item No : {0} ", itemno);
Console.WriteLine("Amount : {0} ", Amt());
}
private readonly int itemno;
private float price;
private int qty;
}

class Sample {
public static void Main() {
Item a = new Item(7, 8, 9);
Item b = new Item(5, 4, 20);
// a.Add(4, 6, 8);
a.Print();
b.Print();
}
}


//-----------------------------------------------------------------

class util {

public static int min(int a, int b) {

if (a > b)
return b;
else
return a;
}
}

class sample {

public static void Main() {

System.Console.WriteLine("min is {0} ", util.min(45, 34));
}
}

Reply all
Reply to author
Forward
0 new messages