Practical : 8
Subject : .Net
Output :
Subject : .Net
Aim : Program in C# to create a base class shape and derived classes i.e., Rectangle, Circle, and Triangle. Invoke the method from base class shape using polymorphism.
Source Code :
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
using
System.Threading.Tasks;
namespace
dotnet008
{
class shape
{
int x;
double y;
}
class rectangle : shape
{
public void area(float a, float b)
{
Console.Write(" Area is :
" + (a * b));
}
}
class circle : shape
{
public void area(float a)
{
Console.Write(" Area is :
" + (3.14 * a * a));
}
}
class triangle : shape
{
public void area(float a, float b)
{
Console.Write(" Area is :
" + ((a * b) / 2));
}
}
class pra8
{
static void Main(string[] args)
{
rectangle re = new rectangle();
circle c = new circle();
triangle t = new triangle();
Console.WriteLine("1.Ractangle
\n2.Circle \n3.Triangle ");
Console.Write("Enter your
Choice: ");
int a =
Convert.ToInt32(Console.ReadLine());
switch (a)
{
case 1:
Console.Write("Enter l and
w: ");
int l =
Convert.ToInt32(Console.ReadLine());
int w =
Convert.ToInt32(Console.ReadLine());
re.area(l, w);
break;
case 2:
Console.Write("Enter r
: ");
int r =
Convert.ToInt32(Console.ReadLine());
c.area(r);
break;
case 3:
Console.Write("Enter a
and b: ");
a =
Convert.ToInt32(Console.ReadLine());
int b =
Convert.ToInt32(Console.ReadLine());
t.area(a, b);
break;
}
Console.ReadKey();
}
}
}
Output :
0 comments:
Post a Comment