Answer to Question #178957 in C# for muhammad arfan

Question #178957

Create a class Point, which defines a point on the coordinate plane. Implement a count of the number of created instances of type Point


1
Expert's answer
2021-04-10T01:08:05-0400
using System;

namespace Test
{
    public class Point
    {
        private static int _counter = 0;

        private readonly int _x;
        private readonly int _y;

        public Point(int x, int y)
        {
            _x = x;
            _y = y;

            ++_counter;
        }

        public int GetX() { return _x; }
        public int GetY() { return _y; }

        public static int GetCounter() { return _counter; }
    }

    class PointTest
    {
        static void Main()
        {
            Point p1 = new Point(0, 0);
            Console.WriteLine("Counter value: {0}", Point.GetCounter());

            Point p2 = new Point(1, 1);
            Console.WriteLine("Counter value: {0}", Point.GetCounter());
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS