Sample Coding Day 4 - Evening Session

2 views
Skip to first unread message

Nuwan

unread,
Dec 31, 2005, 5:19:11 AM12/31/05
to sliit-mcsd
Here is the rest of the coding we looked at

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

using System;

class Item {
public Item() : this (1,1,10) {}

public Item(int itemno, float price, int qty) {

Add(itemno, price, qty);
}

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

public float Amt() {

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

protected int itemno;
protected float price;
protected int qty;
}


class Book : Item {

public Book(int bookno, string title, string author,
int pages, float price, int qty)
: base(bookno,price,qty) {

this.title = title;
this.author = author;
this.pages = pages;
}

public override void Print() {
Console.WriteLine("Book {0} ", itemno);
Console.WriteLine("Title {0} {1} {2}", title, pages, Amt());

}

private string title;
private int pages;
private string author;

}


class Sample {
public static void Main() {
Item i = new Item(10, 50, 6);
Item m = new Book(10, "Programmming C#", "Microsoft",
300, 1000, 10);
m.Print();
i.Print();
// polymorphism

}
}


// ===================================================

interface IPrint {
void Print();
}

interface IInput {
void Input();
}

class Bird : IPrint, IInput {
private string name;

public void Print() {
System.Console.WriteLine("{0} cheep chep", name);
}

public void Input() {
name = System.Console.ReadLine();
}
}

class Dog : IPrint, IInput {
private string name;

public void Print() {
System.Console.WriteLine("{0} Bow wow", name);
}

public void Input() {
name = System.Console.ReadLine();
}
}

class sample {
public static void Main() {


IPrint ip;
Dog a = new Dog();
a.Input();


Bird b = new Bird();
b.Input();

print(a);
print(b);

}

static void print(object o) {
IPrint ip = (IPrint) o;
ip.Print();
}


}


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

using System;

abstract 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 Animal();
a.Speak();

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



}
}

Reply all
Reply to author
Forward
0 new messages