0% found this document useful (0 votes)
88 views

Abstract Class Example Class Diagram

The document discusses an abstract class example in C# for calculating the areas and perimeters of shapes. It defines an abstract Shape class with abstract CalculateArea and CalculatePerimeter methods. Concrete Circle, Rectangle, and Square classes inherit from Shape and override the abstract methods to calculate areas and perimeters specific to each shape type. A Windows form allows the user to input values, create shape objects, and display the results of calling the calculation methods.

Uploaded by

emraan khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
88 views

Abstract Class Example Class Diagram

The document discusses an abstract class example in C# for calculating the areas and perimeters of shapes. It defines an abstract Shape class with abstract CalculateArea and CalculatePerimeter methods. Concrete Circle, Rectangle, and Square classes inherit from Shape and override the abstract methods to calculate areas and perimeters specific to each shape type. A Windows form allows the user to input values, create shape objects, and display the results of calling the calculation methods.

Uploaded by

emraan khan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Naresh I Technologies C#.

Net

- 1 -
Abstract Class Example

Class Diagram



Form Design Screen



Naresh I Technologies C#.Net

- 2 -
// Shape.cs (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();
}
}


// Circle.cs (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()
{
this.Area = (float)System.Math.PI * _radius * _radius;
}

public override void CalculatePerimeter()
{
this.Perimeter = (float)System.Math.PI * (_radius *2);
}
}
}





Naresh I Technologies C#.Net

- 3 -
// Rectangle.cs (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()
{
this.Area = _height * _width;
}
public override void CalculatePerimeter()
{
this.Perimeter = (_height * 2) + (_width * 2);
}
}
}

// Square.cs (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()
{
this.Area = _side * _side;
}

public override void CalculatePerimeter()
{
this.Perimeter = _side * 4;
}
}
}


// Form Code

using System;
using System.Windows.Forms;

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
objCircle.Radius = float.Parse(txtRadius.Text);
//calculate The Area Of Circle
objCircle.CalculateArea();
//Display the Value of Calculated Area of Circle using Area Property
txtCircleArea.Text = Convert.ToString(objCircle.Area);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}

private void btnCalculateCirclePerimeter_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Circle Class with passing Parameterized constructor
Circle objCircle = new Circle(float.Parse(txtRadius.Text));
//calculate The Perimeter Of Circle
objCircle.CalculatePerimeter();
//Display the Value of Calculated Perimeter of Circle using Perimeter Property
txtCirclePerimeter.Text = Convert.ToString(objCircle.Perimeter);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}

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
objRect.Height = float.Parse(txtHeight.Text);
objRect.Width = float.Parse(txtWidth.Text);
//calculate The Area Of Rectangle
objRect.CalculateArea();
//Display the Value of Calculated Area of Rectangle using Area Property
txtRectangleArea.Text = Convert.ToString(objRect.Area);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}

private void btnCalculateRectanglePerimeter_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Circle Class with passing Parameterized constructor
Rectangle objRect = new Rectangle(float.Parse(txtHeight.Text), float.Parse(txtWidth.Text));
//calculate The Perimeter Of Rectangle
objRect.CalculatePerimeter();
//Display the Value of Calculated Perimeter of Rectangle using Perimeter Property
txtRectanglePerimeter.Text = Convert.ToString(objRect.Perimeter);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}

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
objSquare.Side = float.Parse(txtSide.Text);
//calculate The Area Of Square
objSquare.CalculateArea();
//Display the Value of Calculated Area of Square using Area Property
txtSquareArea.Text = Convert.ToString(objSquare.Area);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}

private void btnCalculateSquarePerimeter_Click(object sender, EventArgs e)
{
try
{
// create a new instance of Square Class with passing parameterized constructor
Square objSquare = new Square(float.Parse(txtSide.Text));
//calculate The Perimeter Of Square
objSquare.CalculatePerimeter();
//Display the Value of Calculated Perimeter of Square using Perimeter Property
txtSquarePerimeter.Text = Convert.ToString(objSquare.Perimeter);
}
catch (Exception ee)
{
MessageBox.Show(ee.Message);
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy