namespace Introduction_To_Inheritance
{
public class Circle
{
private double _radius;
public Circle(double radius)
{
_radius = radius;
}
//default constructor
public Circle()
{
_radius = 1;
}
public double Radius { get { return _radius; } }
public double Area()
{
return Math.PI * _radius * _radius;
}
public double Perimeter()
{
return 2 * Math.PI * _radius;
}
}
}
//========================================================
namespace Introduction_To_Inheritance
{
public class Sphere : Circle
{
public Sphere(double radius):base(radius)
{
}
new public double Area()
{
return 4 * Math.PI * Radius * Radius;
}
}
}
//========================================================
namespace Introduction_To_Inheritance
{
//define a class Cylinder that inherits from Circle
public class Cylinder : Circle
{
private double _height;
public Cylinder(double radius, double height) : base (radius)
{
_height = height;
}
public double Height
{
get { return _height; }
}
new public double Area()
{
return 2 * Math.PI * Radius * Height;
}
public double Volume()
{
return Math.PI * Radius * Radius * Height;
}
}
}
//========================================================
namespace Introduction_To_Inheritance
{
public class Cone : Cylinder
{
//Cylinder Constructor
public Cone(double radius, double height): base(radius, height)
{
//initialize any fields defined in this class, if any
}
//hide both area and volume
new public double Area()
{
return Math.PI * Radius * (Radius + Math.Sqrt(Height * Height + Radius * Radius));
}
new public double Volume()
{
return Math.PI * Radius * Radius * Height / 3;
}
}
}
//========================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Introduction_To_Inheritance
{
public partial class Form1 : Form
{
List<Circle> shapeList = new List<Circle>();
//Because of the "is a" property, you can use the shapeList to save
//any object of type Circle as well as Cylinder, Cone and Sphere.
public Form1()
{
InitializeComponent();
Circle c1 = new Circle(5.6);
Sphere sp1 = new Sphere(9.3);
}
Random rand = new Random();
private void btnCreateCircleSphere_Click(object sender, EventArgs e)
{
double radius = rand.Next(10, 20);
double height = rand.Next(100, 150);
//create a Circle object
Circle c = new Circle(radius);
shapeList.Add(c);
//Compute perimeter and area
double circle_peri = c.Perimeter();
double circle_area = c.Area();
//create a Sphere object (using same radius)
Sphere sp = new Sphere(radius);
shapeList.Add(sp);
double sphere_peri = sp.Perimeter();
double sphere_area = sp.Area();
//Create a cylinder object
Cylinder cylinder = new Cylinder(radius, height);
shapeList.Add(cylinder);
double cy_area = cylinder.Area();
double cy_vol = cylinder.Volume();
//create a cone object
Cone cone = new Cone(radius, height);
shapeList.Add(cone);
double cn_area = cone.Area();
double cn_vol = cone.Volume();
//display both
richTextBox1.Text = String.Format(
"{0} Radius: {1} peri: {2:f} area: {3:f}\n\n",
c.GetType().Name,radius, circle_peri, circle_area);
richTextBox1.AppendText(String.Format(
"{0} Radius: {1} peri: {2:f} area: {3:f}\n\n",
sp.GetType().Name,radius, sphere_peri, sphere_area));
richTextBox1.AppendText(String.Format(
"{0 } Radius: {1} Height: {2:f} Area: {3:f} Volume: {4:f} \n\n",
cylinder.GetType().Name, radius, height, cy_area, cy_vol));
richTextBox1.AppendText(String.Format(
"{0 } Radius: {1} Height: {2:f} Area: {3:f} Volume: {4:f} \n\n",
cone.GetType().Name, radius, height, cn_area, cn_vol));
}
private void Display()
{
foreach (Circle c in shapeList)
{
//richTextBox1.AppendText(String.Format(
// "type: {0} r = {1} h = {2} p = {3} a = {4} v = {5}",
if (c is Cone)
{
//cast c to a Cone object
Cone co = (Cone)c;
//use co to display information
}
else if (c is Cylinder)
{
Cylinder cy = (Cylinder)c;
}
else if (c is Sphere)
{
Sphere spr = (Sphere)c;
}
else //implied Circle
{
Circle cir = (Circle)c;
}
}
}
}
}

No comments:
Post a Comment