Vous êtes sur la page 1sur 7

7) Program to implement multiple inheritance using interface.

namespace ConsoleApplication8
{
interface calc1
{
int add(int a, int b);
}
interface calc2
{
int sub(int x, int y);
}
interface calc3
{
int mul(int r, int s);
}
interface calc4
{
int div(int c, int d);
}
class Calculation : calc1, calc2, calc3, calc4
{
public int result1;
public int add(int a, int b)
{
return result1 = a + b;
}
public int result2;
public int sub(int x, int y)
{
return result2 = x - y;
}

public int result3;


public int mul(int r, int s)
{
return result3 = r * s;
}
public int result4;
public int div(int c, int d)
{
return result4 = c / d;
}
}
class Program
{
static void Main(string[] args)
{
Calculation c = new Calculation();
c.add(8, 2);
c.sub(20, 10);
c.mul(5, 2);
c.div(20, 10);
Console.WriteLine("Multiple Inheritance concept Using Interfaces :\n ");
Console.WriteLine("Addition: " + c.result1);
Console.WriteLine("Substraction: " + c.result2);
Console.WriteLine("Multiplication :" + c.result3);
Console.WriteLine("Division: " + c.result4);
Console.ReadKey();
}
}
}
8) Program to implement the Multilevel inheritance.

namespace Inherit
{
class Mode
{
public void modesOf()
{
Console.WriteLine("There are Many Modes of Transport !!");
}
}
class vehicle : Mode
{
public void feature()
{
Console.WriteLine("They Mainly Help in Travelling !!");
}
}
class inheri : vehicle
{
public void Noise()
{
Console.WriteLine("All Vehicles Creates Noise !! ");
}
static void Main(string[] args)
{
inheri obj = new inheri();
obj.modesOf();
obj.feature();
obj.Noise();
Console.Read();

}
}
}
9) Program to implement the Hierarchical inheritance.
namespace Inherit
{
class Principal
{
public void Monitor()
{
Console.WriteLine("Monitor");
}
}
class Teacher : Principal
{
public void Teach()
{
Console.WriteLine("Teach");
}
}
class Student : Principal
{
public void Learn()
{
Console.WriteLine("Learn");
}
}
class Program
{
static void Main(string[] args)

{
Principal g = new Principal();
g.Monitor();
Teacher d = new Teacher();
d.Monitor();
d.Teach();
Student s = new Student();
s.Monitor();
s.Learn();
Console.ReadKey();
}
}
}
10) Program to implement event handling using delegates
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public delegate void EventHandler();
class Program
{
public static event EventHandler _show;
static void Main()
{
// Add event handlers to Show event.
_show += new EventHandler(Dog);
_show += new EventHandler(Cat);
_show += new EventHandler(Mouse);
_show += new EventHandler(Mouse);

// Invoke the event.


_show.Invoke();
}
static void Cat()
{
Console.WriteLine("Cat");
}
static void Dog()
{
Console.WriteLine("Dog");
}
static void Mouse()
{
Console.WriteLine("Mouse");
}
}
11) Program to implement exception handling in C#
class Program
{
static void Main(string[] args)
{
int a = 10, b = 5, c = 5;
int x, y;
try
{
x = a / (b - c);
}
catch (Exception e)

{
Console.WriteLine("Divide by Zero " +e.Message);
}
y = a / (b + c);
int[] d = { 5, 10 };
int f = 5;
try
{
int g;
g = d[2] / f - d[1];
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine("Array Index Error");
}
}
}
12) Program to implement user defined exceptions in C#

Vous aimerez peut-être aussi