Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace abstractClassExample
- {
- class Program
- {
- static void Main(string[] args)
- {
- Shape[] shapes = { new Sphere(4), new Cube(3) };
- foreach(Shape shape in shapes)
- {
- shape.GetInfo();
- Console.WriteLine($"{shape.Name} has a volume of {shape.Volume()}\n");
- }
- Console.ReadLine();
- }
- }
- abstract class Shape
- {
- public string Name { get; set; }
- public virtual void GetInfo()
- {
- Console.WriteLine($"This is a {Name}");
- }
- // Only declaring the method, not implementing it
- public abstract double Volume();
- }
- class Cube : Shape
- {
- public double Length { get; set; }
- public Cube(double length)
- {
- Name = "Cube";
- Length = length;
- }
- public override double Volume()
- {
- return Math.Pow(Length, 3);
- }
- public override void GetInfo()
- {
- base.GetInfo();
- Console.WriteLine($"The Cube has a length of {Length}");
- }
- class Sphere : Shape
- {
- public double Radius { get; set; }
- public Sphere(double radius)
- {
- Name = "Sphere";
- Radius = radius;
- }
- public override double Volume()
- {
- return Math.PI * (Math.Pow(Radius, 3)) * 4 / 3;
- }
- public override void GetInfo()
- {
- base.GetInfo();
- Console.WriteLine($"The Cube has a radius of {Radius}");
- }
- }
- }
Add Comment
Please, Sign In to add comment