Q:

C# program to demonstrate the class and object creation

0

C# program to demonstrate the class and object creation

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Sample
    {
        private int X;
        private int Y;

        public Sample()
        { 
            X = 5;
            Y = 10;
        }

        public void read()
        {
            Console.Write("Enter value of X: ");
            X = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter value of Y: ");
            Y = Convert.ToInt32(Console.ReadLine());
        }

        public void print()
        {
            Console.WriteLine("Value of X: " + X);
            Console.WriteLine("Value of Y: " + Y);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            Sample S1 = new Sample();

            S1.print();

            Sample S2 = new Sample();

            S2.read();
            S2.print();


        }
    }
}

Output

Value of X: 5
Value of Y: 10
Enter value of X: 12
Enter value of Y: 15
Value of X: 12
Value of Y: 15

In this program, there is a class named "Sample", it contains default constructor and two private data members and two public methods that are operated on data members.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | Class, Object, Methods

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
\'this\' reference in C#.Net with Exampl... >>