Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- Figure[] figures = new Figure[2];
- figures[0] = new Rectangle(2, 3, new Color(2,3,4));
- figures[1] = new Circle(100, new Color(255,0,0));
- foreach(Figure item in figures)
- {
- Console.WriteLine(item.ToString() + $" and area: {item.Area()}");
- }
- }
- }
- abstract class Figure
- {
- public Color _color { get; set; }
- public abstract double Area();
- }
- class Rectangle : Figure
- {
- private int _height;
- private int _width;
- public int Height
- {
- get => _height;
- set => _height = value >= 0 ? value : 0;
- }
- public int Width
- {
- get => _width;
- set => _width = value >= 0 ? value : 0;
- }
- public override double Area()
- {
- return Height * Width;
- }
- public Rectangle(int height, int width, Color color)
- {
- Height = height;
- Width = width;
- _color = color;
- }
- public override string ToString()
- {
- return $"Rectangle: height = {Height}, width = {Width}, color: {_color.ToString()}";
- }
- }
- class Circle : Figure
- {
- private int _radius;
- public int Radius
- {
- get => _radius;
- set => _radius = value >= 0 ? value : 0;
- }
- public override double Area()
- {
- return 3.1415 * Radius * Radius;
- }
- public override string ToString()
- {
- return $"Circle: radius = {Radius}, color: {_color.ToString()}";
- }
- public Circle(int radius, Color color)
- {
- Radius = radius;
- _color = color;
- }
- }
Add Comment
Please, Sign In to add comment