Wednesday, 5 March 2014

Abstract Modifier

  • abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class
ABSTRACT CLASS

Purpose
The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
Characteristics
They are classes that cannot be instantiated.
An abstract class contain either abstract methods or non abstract methods.
A non abstract class derived from abstract class MUST include actual implementation of all inherited members.

ABSTRACT METHOD
Characteristics
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block.
Derived classes of the abstract class must implement all abstract methods.
Implementation is provided by overriding method.
Cannot be private.
Cannot be static.
Abstract method cannot be contained in non abstract class.

Example of Abstract Class and Methods

   abstract class Animal
    {
       internal  void Show()
       {
           Console.WriteLine("Mein Janwar Hun");
       }
       abstract internal void Sound();
       abstract internal void FoodHabit();

    }

  class Dog : Animal
   {

      
     internal override void Sound()
       {
           Console.WriteLine("Bow Bow Bow Bow");
       }

      internal override void FoodHabit()
       {
           Console.WriteLine("Noveggieee");
       }
   }
    class Cat:Animal
    {
        internal override void Sound()
        {
            Console.WriteLine("meow Meow meow");
        }

        internal override void FoodHabit()
        {
            Console.WriteLine("veggieee");
        }
      
      
    }

class CallingClass
{
       public static void Main()
       {
             Dog d = new Dog();
           d.Show();
           d.Sound();
           d.FoodHabit();

           Console.ReadKey();

           Cat c=new Cat();
           c.Show();
           c.Sound();
           c.FoodHabit();
           Console.ReadKey();
       } 
 }

Monday, 3 March 2014

What is Specialisation?

  • When some new sub classes are created from an existing super class to do specific job, then this process  is known as specialization.

What is Generalisation?


  • When we create a base/super class from two or more similar type of objects by extracting their all  common characteristics(attributes and behavior) and combine them, then this process is known as Generalization.

What is Inheritance?

  • Inheritance is the process of creating a new classes from an existing class.
  • The Pre-existing class is called base class or super class and
  • the new class is knows as derived class or subclass.
  • The newly created class have all the characteristics of its existing or base class and it can also add its own additional attributes and behavior too.
  • Inheritance allows the extension and reuse of existing code, without having to repeat or rewrite the code from scratch

Example of Inheritance

//generalization  - extracted common characteristic
class Product
    {
         int productcode;
         string productname;
         int price;
        
        protected void getData()
        {
            Console.WriteLine("Input Product Information");
            Console.WriteLine("Enter Product Code");
            productcode = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter product Name");
            productname = Console.ReadLine();
            Console.WriteLine("Enter Price");
            price = int.Parse(Console.ReadLine());
        }
       protected  void showData()
        {

            Console.WriteLine("Show Product Information");
            Console.WriteLine("Product Code={0} \n Product Name={1} \n Price={2} \n", productcode, productname, price);
        }
 }//class Product Ends
   
//specialization  - to specific job + inherited product
    class Book : Product
    {
        int noPages;
        string Author;
        internal void getBook()
        {

            getData();

            Console.WriteLine("Input Book  specific Information");
            Console.WriteLine("Enter No of Pages");
            noPages = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Name of Author");
            Author = Console.ReadLine();

        }
      internal void showBook()
        {
           showData();

            Console.WriteLine("Show Book specific information");
            Console.WriteLine("Pages={0} \n Author={1}", noPages, Author);

        }
         
          
    }//Book Class Closed

//specialization  - to specific job + inherited product
    class Pen : Product
    {

    
        string b;
        string cink;

        internal void getPen()
        {
            getData();
            cink = Console.ReadLine();
            Console.WriteLine("Enter Brand Name");
            b = Console.ReadLine();

        }
        internal void showPen()
        {   showData();
            Console.WriteLine("Brand={0} \n Ink Color={1}", b,cink);

        }

    }

class CallingClass
{
     public static void Main()
      {
        Console.WriteLine("****BOOK *****"); 
        Book b=new Book();
        b.getBook();
        b.showBook();

        Console.WriteLine("****PEN *****"); 
        Pen p=new Pen();
        p.getPen();
         p.showPen();

        Console.ReadKey();
}

Thursday, 20 February 2014

Stream Read and Write

class StreamReadWrite
    {
        public static void Main()
        {
            int rno;
            string name;

            Console.WriteLine("Enter Roll Number");
            rno = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter Name");
            name = Console.ReadLine();
           

            FileStream fs = new FileStream("student.txt", FileMode.Append , FileAccess.Write);
            StreamWriter sw = new StreamWriter(fs);

           sw.WriteLine("My name is {0} and roll number is {1}", name, rno);
           

            sw.Close();
            fs.Close();


            FileStream fs1 = new FileStream("student.txt", FileMode.OpenOrCreate , FileAccess.Read);
            StreamReader sr = new StreamReader(fs1);

            string s;
            s = sr.ReadLine();
            Console.WriteLine("data in a file is \n {0}", s);
     
            s = sr.ReadLine();
            Console.WriteLine("data in a file is \n {0}", s);

            sr.Close();
            fs.Close();





            Console.WriteLine("Press any key to stop");
            Console.ReadKey();
        }

    }

Binary Reading and Writing in a File

class BinaryReadWrite
    {
       public static void Main()
        {
            int a;
            char b;
            double c;
            bool d = true;

             Console.WriteLine("Enter Int value");
              a = int.Parse(Console.ReadLine());
              Console.WriteLine("Enter Char value");
              b = char.Parse(Console.ReadLine());
              Console.WriteLine("Enter double value");
              c = double.Parse(Console.ReadLine());

              FileStream fs=new FileStream("File11to1.txt",FileMode.OpenOrCreate , FileAccess.Write,FileShare.ReadWrite);

              BinaryWriter bw = new BinaryWriter(fs);

              bw.Write(a);
              bw.Write(b);
              bw.Write(c);
              bw.Write(d);
              bw.Close();
              fs.Close();
             
            FileStream fs = new FileStream("File11to1.txt", FileMode.OpenOrCreate, FileAccess.Read );
            BinaryReader br = new BinaryReader(fs);

            a = br.ReadInt32();
            b = br.ReadChar();
            c = br.ReadDouble();
            d = br.ReadBoolean();


            Console.WriteLine("a={0}\nb={1}\nc={2}\nd={3}", a, b, c, d);


            Console.WriteLine("Press any key to stop");
            Console.ReadKey();

        }
    }

Monday, 17 February 2014

Method Returning More Than One Values using out

 struct Calculator
        {

            void calculate(int a, int b, out int p, out int q, out int r, out int s)
            {


               p = a + b;
                q = a - b;
                r = a * b;
                s = a / b;

                Console.WriteLine("All operations PERFORMED successfully");


            }
            static void Main()
            {
                int a, b,c,d,e,f;

                int[] r = new int[5];
                Console.WriteLine("Enter First No");
                a = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter Second No");
                b = int.Parse(Console.ReadLine());

                Calculator o = new Calculator();
                o.calculate(a, b, out c, out d, out e, out f);


                Console.WriteLine("Sum={0}", c);
                Console.WriteLine("Subtraction={0}", d);
                Console.WriteLine("Multiplication={0}", e);
                Console.WriteLine("Division={0}", f);

                Console.WriteLine("Press Any Key To Exit");
                Console.ReadLine();
            }
        }

Method Returning Multiple Values

        struct Calculator
        {

            int[] calculate(int a, int b)
        {
            int[] result = new int[5];
          
            result[0] = a + b;
            result[1] = a - b;
            result[2] = a * b;
            result[3] = a / b;

            Console.WriteLine("All operations PERFORMED successfully");
            return result;
              
        }
            static void Main()
            {
                int a, b;

                int[] r = new int[5];
                Console.WriteLine("Enter First No");
                a = int.Parse(Console.ReadLine());

                Console.WriteLine("Enter Second No");
                b = int.Parse(Console.ReadLine());

                Calculator o = new Calculator();
               r= o.calculate(a, b);


                Console.WriteLine("Sum={0}",r[0]);
                Console.WriteLine("Subtraction={0}",r[1]);
                Console.WriteLine("Multiplication={0}",r[2]);
                Console.WriteLine("Division={0}",r[3]);

                Console.WriteLine("Press Any Key To Exit");
                Console.ReadLine();
            }
        }

Function Having return type

   struct Square
        {
            int SquareMe(int x)
            {
       
                int s;
            
                Console.WriteLine ("I am Squaring You");
                s = x * x;
                return s;
            }


           static void Main()
            {
                int n, s1=0;
                Square o = new Square();

                for (int i = 1; i <= 5; i++)
                {
                    Console.WriteLine("Enter Any Number");
                    n = int.Parse(Console.ReadLine());
                    s1 = o.SquareMe(n);
                    Console.WriteLine("Square of {0} is {1}", n, s1);
                   
                }
               
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }

Pass by Reference using out

   struct Increment
        {
            void increment(out int n)
            {
                n = 2;
                n = n + 1;
                Console.WriteLine("\n****in method my value is {0}", n);
            }

        static void Main()
            {
                int n ;
                Console.WriteLine("Enter any no");
               //  n = int.Parse(Console.ReadLine());
                //Console.WriteLine("\nBefore Calling method...My Value is {0}", n);
                Increment o = new Increment();
                o.increment(out n);
                Console.WriteLine("\nAfter Calling method....My Value is {0}", n);
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }

Pass by reference

 struct Increment
        {
            void increment(ref int n)
            {
                n = n + 1;
                Console.WriteLine("\n****in method my value is {0}", n);
            }
            static void Main()
            {
                int n;
                Console.WriteLine("Enter any no");
               n = int.Parse(Console.ReadLine());
                Console.WriteLine("\nBefore Calling method...My Value is {0}", n);
                Increment o = new Increment();
                o.increment(ref n);
                Console.WriteLine("\nAfter Calling method....My Value is {0}", n);
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }

Pass By Value


Passing a value-type data to a method by value means passing a copy of the variable to the method.

Any changes to the parameter that take place inside the method have no affect on the original data in the calling method.


  struct Square
        {
            void SquareMe(int x)
            {
                int s;
                s = x * x;
                Console.WriteLine("Square of {0} is {1}", x, s);
            }

            void increment(int n)
            {
                n = n + 1;
                Console.WriteLine("\n****in method my value is {0}", n);
            }

            static void Main()
            {
                int n;
                Console.WriteLine("Enter Any Number");
                n = int.Parse(Console.ReadLine());
                Console.WriteLine("\nBefore Calling method...My Value is {0}", n);
                Square o = new Square();
                o.increment(n);
                Console.WriteLine("\nAfter Calling method....My Value is {0}", n);
                Console.WriteLine("Press any key to Exit");
                Console.ReadKey();
            }
        }