Friday, 26 August 2011

Operator Overloading


                 By the using Operator overloading we can do the arithmetic operation on the user define data types as class, structures. Do any operation symbolizing the operator in your class.
You can add/subtract/multiply/divide or do any other operation for  + operator() the choice is entirely yours.

The following Program describes about Operator Overloading...



public class Complex
    {
        public int real;
        public int imaginary;

        public Complex(int real, int imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }

        // Declare which operator to overload (+), the types
        // that can be added (two Complex objects), and the
        // return type (Complex):
        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.real + c2.real, c1.imaginary + c2.imaginary);
        }
        // Override the ToString method to display an complex number in the suitable format:
        public override string ToString()
        {
            return (String.Format("{0} + {1}i", real, imaginary));
        }

        public static void Main()
        {
            Complex num1 = new Complex(2, 3);
            Complex num2 = new Complex(3, 4);

            // Add two Complex objects (num1 and num2) through the
            // overloaded plus operator:
            Complex sum = num1 + num2;

            // Print the numbers and the sum using the overriden ToString method:
            Console.WriteLine("First complex number:  {0}", num1);
            Console.WriteLine("Second complex number: {0}", num2);
            Console.WriteLine("The sum of the two numbers: {0}", sum);
            Console.Read();

        }
    }

Output:

First complex number:  2 + 3i
Second complex number: 3 + 4i
The sum of the two numbers: 5 + 7i






Private Constructor


When you declare a Constructor with Private access modifier then it is called Private Constructor. We can use the private constructor in singleton pattern.

If you declare a Constructor as private then it doesn't allow to create object for its
derived class, i.e you loose inherent facility for that class.

In the following program I'm trying to create a derived class  for the Base class having Private constructor it shows an error like .
   Error: parent()  is inaccessible due to its protection level   
Use public instead of private then  the following program works...



class parent
    {
       public  string name;
       private   parent()
       //use public instead of private then this program works...
        {
            this.name = "Radha";
        }
    }

    class child : parent
    {
        public void show()
        {
            Console.WriteLine(name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            child obj = new child();
            obj.show();
            Console.Read();

        }
    }
}


// Error :: parent()' is inaccessible due to its protection level  

Order of Constructor execution in Inheritance

  •  constructors are called in the order from the top to the bottom (parent to child class) in inheritance hierarchy.
See the below program and check the output ...




class parent
    {
        public parent()
        {
            Console.WriteLine("This is Paarent");
        }
    }
    class child : parent
    {
        public child()
        {
            Console.WriteLine("This is child");
        }
    }

    class Program : child
    {
        static void Main(string[] args)
        {
            Program obj = new Program();
            Console.Read();

        }

        public Program()
        {
            Console.WriteLine("This is program");
        }
    }


Output:

This is Paarent
This is child
This is program

Shadowing and Overriding differences using Static keywords...

 In this blog i  already specified about Shadowing concept . if you don't know then check it once.
  • Here the following program describes the differences between shadowing and overriding what i observe are written below the program





 class A
    {
        public virtual  void methodsample()
        {
            Console.WriteLine("methodsample in A");
        }
        public void methodOne()
        {
            Console.WriteLine("methodOne in A");
        }
        public  void methodTwo()
        {
            Console.WriteLine("methodTwo in A ");
        }
        public  static  void methodThree()
        {
            Console.WriteLine("methodThree in A");
        }
        public static void methodFour()
        {
            Console.WriteLine("methodFour in A");
        }
    }
    class B : A
    {
        public override void methodsample()
        {
            Console.WriteLine("methodsample in A overridding...This is methodsample in class B");
        }

        public new void methodOne()
        {
            Console.WriteLine("methodOne in B ");
        }
        public new static void methodTwo()
        {
            Console.WriteLine("methodTwo in B");
        }
        public new static  void methodThree()
        {
            Console.WriteLine("methodThree in B");
        }
        public new void methodFour()
        {
            Console.WriteLine("methodFour in B");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            A obj = new A();
            obj.methodsample();
            A objA = new B();  // observe in creating instance....
            objA.methodsample();
            objA.methodOne();
            objA.methodTwo();
            A.methodThree();
            A.methodFour();
            //objB.methodFour(); bcz static methods cannot acess with object name
            B objB = new B();
            objB.methodOne();
            B.methodTwo();
            B.methodThree();
            objB .methodFour();
            objB.methodsample();
            //objB.methodTwo(); static method ...
            Console.ReadLine();
        }
    }

Points I observed:
/* for this program no method can be overriden . Here Just Shadowing concept will be takes place only.
   the flow is like : for which class you create object those methods only will be executed .
   Try to comment  methodOne and methodFour in class B and create object for B and Execte that...
   for methodFour it shows an error...bcz we cannot shadow static methods...
  comment methodTwo in classB and observe...
  static methods can only call with class names .with what class name you are calling that method will be        executed.
  A obj = new B(); is possible.
  B obj = new A(); Not possible. bcz A is parent class....
  If you use new keyword in derived class then it doesn't show any warning means it hides the base class method and this concept is known as shadowing ...
 Here u observe the difference between overriding and shadowing...
 Observe commenting ovverride methodsample in class B and execute...*/

Output:

methodsample in A
methodsample in A overridding...This is methodsample in class B
methodOne in A
methodTwo in A
methodThree in A
methodFour in A
methodOne in B
methodTwo in B
methodThree in B
methodFour in B
methodsample in A overridding...This is methodsample in class B


Copy Constructor in C#

         Unlike some languages, C# does not provide a copy constructor. If you create a new object and want to copy the values from an existing object, you have to write the appropriate method yourself.

The following program describes copy constructor...

 class sample
    {
        string nme;
        int number;

        public sample()
        {
       
        }
       
        // Parameterised Constructor.............
        public sample(string name, int no)
        {
            this.nme = name;
            this.number = no;
        }
        // Copy Constructor -------------------------
        public sample (sample object11)
        {
            nme=object11.nme;
            number = object11.number;
           
   
        }

        public void show()
        {
            Console.WriteLine("Name is " + nme);
            Console.WriteLine("Number is " + number);
        }
    }
   
   
   
    class Program
    {
        static void Main(string[] args)
        {
            sample obj = new sample("radha",101);
            sample obj1 = new sample(obj);
            obj.show();
            obj1.show();
            Console.Read();


        }
    }


Output:
Name is radha
Number is 101
Name is radha
Number is 101


Thursday, 25 August 2011

Static Constructors

    A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only. It is called automatically before the first instance is created or any static members are referenced.

Static constructors have the following properties:
  • A static constructor does not take access modifiers or have parameters.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  • A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  • A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  • Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

    The following Program describes about the static constructor...



class sample
    {
         public static int i;
         public int j;
         static sample()
        {
            i = 10;
            //j = 11; Error:: An object reference is required for the non-static field, method, or property .

            Console.WriteLine("static constructor is invoked");
        }

        public sample()
        {
            int j = 22;
            Console.WriteLine("Non Static Constructor is invoked");
        }
           
    }

                                                     

    class Program
    {
      
        static void Main(string[] args)

        {
            Console.WriteLine("i value is " + sample.i);
         // Static constructor is called before object creation.
            sample obj = new sample();
            Console.WriteLine("J value is " + obj.j);
            Console .Read();

        }
    }

Output:
static constructor is invoked
i value is 10
Non Static Constructor is invoked
J value is 0





Message Passing in Object Oriented Programming


              Message passing is a method by which an object sends data to another object or requests other object to invoke method. This is also known as interfacing. It acts like a messenger from one object to other object to convey specific instructions.

The following Program explains about the message passing...




class Base
    {
        public Base()
        { }
        public Base(string s)
        {
            Console.WriteLine("hi " +s);
        }
    }
    class Derived : Base
    {
       
        public Derived(string s)
        {
            Base b = new Base(s);
            Console.WriteLine(s);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Derived d = new Derived("Krishna");
            Console.ReadLine();
        }
    }

Output:

hi Krishna
Krishna

// Here Object d invokes the Constructor of base class that means object interaction will be takes place between Object d and Object b.