Encapsulation in c# - C# Tutorials

Latest

For Video tutorials Visit my Channel https://www.youtube.com/channel/UCVWpZVbMqLHuQBigIfc9I8Q

Monday, 13 February 2017

Encapsulation in c#

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

namespace Encapsulation
{
    class Program
    {
        class Rectangle
        {
            //member variables
            public double length;
            public double width;

            public double GetArea()
            {
                return length * width;
            }
            public void Display()
            {
                Console.WriteLine("Length: {0}", length);
                Console.WriteLine("Width: {0}", width);
                Console.WriteLine("Area: {0}", GetArea());
            }
        }//end class Rectangle

        class Rectangle1
        {
            //member variables
            private double length;
            private double width;

            public void Acceptdetails()
            {
                Console.WriteLine("Enter Length in inches: ");
                length = Convert.ToDouble(Console.ReadLine());
                Console.WriteLine("Enter Width in inches: ");
                width = Convert.ToDouble(Console.ReadLine());
            }
            public double GetArea()
            {
                return length * width;
            }
            public void Display()
            {
                Console.WriteLine("Length: {0}", length);
                Console.WriteLine("Width: {0}", width);
                Console.WriteLine("Area: {0}", GetArea());
            }
        }//end class Rectangle1

        class Rectangle2
        {
            //member variables
            internal double length;
            internal double width;

            double GetArea()
            {
                return length * width;
            }
            public void Display()
            {
                Console.WriteLine("Length: {0}", length);
                Console.WriteLine("Width: {0}", width);
                Console.WriteLine("Area: {0}", GetArea());
            }
        }//end class Rectangle2

        static void Main(string[] args)
        {
            Console.WriteLine("Enter 1 for Public Access Specifier");
            Console.WriteLine("Enter 2 for Private Access Specifier");
            Console.WriteLine("Enter 3 for Internal Access Specifier ");
            Console.WriteLine("Enter 4 for Exit");
            Console.WriteLine("Enter your Option");

            int op = Convert.ToInt16(Console.ReadLine());
            Console.Clear();
            switch (op)
            {
                case 1:
                    {
                        Rectangle r = new Rectangle();
                        r.length = 4.5;
                        r.width = 3.5;
                        r.Display();
                        Console.ReadKey();
                        break;
                    }
                case 2:
                    {
                        Rectangle1 r = new Rectangle1();
                        r.Acceptdetails();
                        r.Display();
                        Console.ReadKey();
                        break;
                    }
                case 3:
                    {
                        Rectangle r = new Rectangle();
                        r.length = 4.5;
                        r.width = 3.5;
                        r.Display();
                        Console.ReadKey();
                        break;
                    }
                case 4:
                    {
                        System.Environment.Exit(1);
                        break;
                    }

            }
        }
    }
}


No comments:

Post a Comment