Naresh I Technologies C#.
Net
- 1 -
Abstract Class Example
Class Diagram
Form Design Screen
Naresh I Technologies C#.Net
- 2 -
// [Link] (This is Abstract Class)
using System;
namespace AbstractExample
{
public abstract class Shape
{
private float _area;
private float _perimeter;
public float Area
{
get{ return _area; }
set{ _area = value; }
}
public float Perimeter
{
get{ return _perimeter; }
set{ _perimeter = value; }
}
public abstract void CalculateArea();
public abstract void CalculatePerimeter();
}
}
// [Link] (This is Drived Class from Shape Base Class)
using System;
namespace AbstractExample
{
class Circle : Shape
{
private float _radius;
public Circle() { }
public Circle(float radius)
{
_radius = radius;
}
public float Radius
{
get{ return _radius; }
set{ _radius = value; }
}
public override void CalculateArea()
{
[Link] = (float)[Link] * _radius * _radius;
}
public override void CalculatePerimeter()
{
[Link] = (float)[Link] * (_radius *2);
}
}
}
Naresh I Technologies C#.Net
- 3 -
// [Link] (This is Drived Class from Shape Base Class)
using System;
namespace AbstractExample
{
class Rectangle : Shape
{
private float _height;
private float _width;
public Rectangle() { }
public Rectangle(float height, float width)
{
_height = height;
_width = width;
}
public float Height
{
get{ return _height; }
set{ _height = value; }
}
public float Width
{
get{ return _width; }
set{ _width = value; }
}
public override void CalculateArea()
{
[Link] = _height * _width;
}
public override void CalculatePerimeter()
{
[Link] = (_height * 2) + (_width * 2);
}
}
}
// [Link] (This is Drived Class from Shape Base Class)
using System;
namespace AbstractExample
{
class Square : Shape
{
private float _side;
public Square() { }
public Square(float side)
{
_side = side;
}
public float Side
{
get{ return _side; }
set{ _side = value; }
}
Naresh I Technologies C#.Net
- 4 -
public override void CalculateArea()
{
[Link] = _side * _side;
}
public override void CalculatePerimeter()
{
[Link] = _side * 4;
}
}
}
// Form Code
using System;
using [Link];
namespace AbstractExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnCalculateCircleArea_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Circle Class
Circle objCircle = new Circle();
// set the radius value to Circle Class through property
[Link] = [Link]([Link]);
//calculate The Area Of Circle
[Link]();
//Display the Value of Calculated Area of Circle using Area Property
[Link] = [Link]([Link]);
}
catch (Exception ee)
{
[Link]([Link]);
}
}
private void btnCalculateCirclePerimeter_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Circle Class with passing Parameterized constructor
Circle objCircle = new Circle([Link]([Link]));
//calculate The Perimeter Of Circle
[Link]();
//Display the Value of Calculated Perimeter of Circle using Perimeter Property
[Link] = [Link]([Link]);
}
catch (Exception ee)
{
[Link]([Link]);
}
}
private void btnCalculateRectangleArea_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Circle Class
Rectangle objRect = new Rectangle();
Naresh I Technologies C#.Net
- 5 -
// set the height, width value to Rectangle Class through property
[Link] = [Link]([Link]);
[Link] = [Link]([Link]);
//calculate The Area Of Rectangle
[Link]();
//Display the Value of Calculated Area of Rectangle using Area Property
[Link] = [Link]([Link]);
}
catch (Exception ee)
{
[Link]([Link]);
}
}
private void btnCalculateRectanglePerimeter_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Circle Class with passing Parameterized constructor
Rectangle objRect = new Rectangle([Link]([Link]), [Link]([Link]));
//calculate The Perimeter Of Rectangle
[Link]();
//Display the Value of Calculated Perimeter of Rectangle using Perimeter Property
[Link] = [Link]([Link]);
}
catch (Exception ee)
{
[Link]([Link]);
}
}
private void btnCalculateSquareArea_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Square Class
Square objSquare = new Square();
// set the side value to Square Class through property
[Link] = [Link]([Link]);
//calculate The Area Of Square
[Link]();
//Display the Value of Calculated Area of Square using Area Property
[Link] = [Link]([Link]);
}
catch (Exception ee)
{
[Link]([Link]);
}
}
private void btnCalculateSquarePerimeter_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Square Class with passing parameterized constructor
Square objSquare = new Square([Link]([Link]));
//calculate The Perimeter Of Square
[Link]();
//Display the Value of Calculated Perimeter of Square using Perimeter Property
[Link] = [Link]([Link]);
}
catch (Exception ee)
{
[Link]([Link]);
}
}
}
}