Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program.cs
- ______________
- using System;
- namespace test
- {
- class Program
- {
- static void Main ()
- {
- Rectangle firstRectangle = new Rectangle (-1,2);
- firstRectangle.Show ();
- Console.WriteLine ();
- firstRectangle.A = 4;
- firstRectangle.B = 4;
- firstRectangle.Show ();
- if (firstRectangle)
- Console.WriteLine ("Is Square");
- int x = firstRectangle.GetPerimetr();
- int y = firstRectangle.GetSquare();
- Console.WriteLine ("Perimetr = {0}; Square = {1} ",
- x, y);
- Console.WriteLine ();
- firstRectangle [0] = 9;
- firstRectangle [1] = 9;
- firstRectangle [2] = 0;
- firstRectangle++;
- firstRectangle.Show ();
- firstRectangle *= 4;
- firstRectangle.Show ();
- int s = firstRectangle.isSquare;
- }
- }
- }
- ____________________
- Rectangle.cs
- ____________________
- using System;
- namespace test
- {
- public class Rectangle
- {
- private int a;
- private int b;
- //конструкторы
- public Rectangle ()
- {
- }
- public Rectangle (int a, int b)
- {
- if (a > 0 && b > 0)
- {
- this.a = a;
- this.b = b;
- }
- else
- Console.WriteLine ("Incorrect a or b");
- }
- //методы: вывод на экран, подсчет периметра и площади
- public void Show()
- {
- Console.WriteLine ("Rectangle with side a = {0} and side b = {1}", a, b);
- }
- public int GetPerimetr()
- {
- return 2*(a+b);
- }
- public int GetSquare()
- {
- return a*b;
- }
- //свойства
- public int A
- {
- get
- {
- return a;
- }
- set
- {
- if (value > 0)
- a = value;
- else
- {
- a = 0;
- Console.WriteLine ("Incorrect a");
- }
- }
- }
- public int B
- {
- get
- {
- return b;
- }
- set
- {
- if (value > 0)
- b = value;
- else
- {
- b = 0;
- Console.WriteLine ("Incorret b");
- }
- }
- }
- public int isSquare
- {
- get
- {
- if (a == b)
- {
- Console.WriteLine("Rectangle is Square");
- return 0;
- }
- else return 0;
- }
- }
- //индексатор
- public int this [int i]
- {
- get
- {
- if (i == 0)
- return a;
- else if (i == 1)
- return b;
- else
- {
- Console.WriteLine ("Error: index");
- return 0;
- }
- }
- set
- {
- if (i == 0 && value > 0)
- a = value;
- else if (i == 1 && value > 0)
- b = value;
- else
- Console.WriteLine ("Error: index or value");
- }
- }
- //перегрузка операций
- public static Rectangle operator ++ (Rectangle x)
- {
- x.A += 1;
- x.B += 1;
- return x;
- }
- public static Rectangle operator -- (Rectangle x)
- {
- x.A -= 1;
- x.B -= 1;
- return x;
- }
- public static Rectangle operator * (Rectangle x, int y)
- {
- x.A *= y;
- x.B *= y;
- return x;
- }
- public static bool operator true (Rectangle x)
- {
- if (x.a == x.b)
- return true;
- else
- return false;
- }
- public static bool operator false (Rectangle x)
- {
- if (x.a == x.b)
- return true;
- else
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment