Problem Statement
John, a software developer in Zed Axis Technologies, needs to track the number of objects of a class that are created. For the same, he has been asked to create a class named ObjectCount. Help John to create a program containing the ObjectCount class. Solution To create the required program, John needs to perform the following steps: 1 Open Notepad.Write the following code in Notepad
:
using System;
public class ObjectCount
{
public static int count;
public ObjectCount()
{ count++;
}
public int display()
{
return count;
}
}
class Static
{
static int Main(string[] args)
{
ObjectCount obj1 = new ObjectCount();
ObjectCount obj2 = new ObjectCount();
ObjectCount obj3 = new ObjectCount();
Console.WriteLine("Number of objects created are {0}", obj3.display());
Console.ReadLine();
return 0;
} }
No comments:
Post a Comment