Friday, 5 December 2014

Friend Function in ASP .Net

Friend Function:-

 If  a function is defined as friend function then it can access all private and protected members of the class.Friend fuction is the friend of the class so it can access all the memeber of the class .it can define outside of the class.

EXAMPLE:-

#include <iostream>
 
using namespace std;
 
class Box
{
   double width;
public:
   friend void printWidth( Box box );
   void setWidth( double wid );
};

// Member function definition
void Box::setWidth( double wid )
{
    width = wid;
}

// Note: printWidth() is not a member function of any class.
void printWidth( Box box )
{
   /* Because printWidth() is a friend of Box, it can
    directly access any member of this class */
   cout << "Width of box : " << box.width <<endl;
}
 
// Main function for the program
int main( )
{
   Box box;
 
   // set box width without member function
   box.setWidth(10.0);
   
   // Use friend function to print the wdith.
   printWidth( box );
 
   return 0;
}

Difference between Virtual Method and Abstract Method in ASP .Net


Virtual methods have an implementation and provide the derived classes with the option of overriding it. Abstract methods do not provide an implementation and forces the derived classes to override the method.
So, abstract methods have no actual code in them, and subclasses HAVE TO override the method. Virtual methods can have code, which is usually a default implementation of something, and any subclasses CAN override the method using the override modifier and provide a custom implementation.
public abstract class E
{
    public abstract void AbstractMethod(int i);

    public virtual void VirtualMethod(int i)
    {
        // Default implementation which can be overridden by subclasses.
    }
}

public class D : E
{
    public override void AbstractMethod(int i)
    {
        // You HAVE to override this method
    }
    public override void VirtualMethod(int i)
    {
        // You are allowed to override this method.
    }
}

Abstract Method in Asp .Net

Abstract Method :-

If method is define as abstract method then it can not contain a implementation but it can forcely allow to derived class to override that method.


 EXAMPLE:-


public abstract class Shape
{
   public abstract void Paint(Graphics g, Rectangle r);
}
public class Ellipse: Shape
{
   public override void Paint(Graphics g, Rectangle r) {
      g.DrawEllipse(r);
   }
}
public class Box: Shape
{
   public override void Paint(Graphics g, Rectangle r) {
      g.DrawRect(r);
   }
}

Virtual function in Asp.Net

Virtual function in Asp.Net :-

 If method is define as a virtual method in base class then we can allow the derived class to override that method.


Example of Virtual Method in .Net:


Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}

Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}

static void main()
{
parent objParent = new child();
objParent.hello();
}

//Output
Hello from Child.



Difference between Function and Store Procedure in Sql Server

Basic Difference:-

  1. Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).
  2. Functions can have only input parameters for it whereas Procedures can have input/output parameters .
  3. Functions can be called from Procedure whereas Procedures cannot be called from Function.

Constructor and Destructor:-

1)Constructor:-

It is a member function having same name as it’s class and which is used to initialize the objects of that class type with a legel initial value. Constructor is automatically called when object is created.

 

Types of Constructor

Default Constructor-: A constructor that accepts no parameters is known as default constructor. If no constructor is defined then the compiler supplies a default constructor.
Circle :: Circle()
{
    radius = 0;
}
Parameterized Constructor -: A constructor that receives arguments/parameters, is called parameterized constructor.
Circle :: Circle(double r)
{
    radius = r; 
}
Copy Constructor-: A constructor that initializes an object using values of another object passed to it as parameter, is called copy constructor. It creates the copy of the passed object.

Circle :: Circle(Circle &t)
{
    radius = t.radius; 
}

There can be multiple constructors of the same class, provided they have different signatures.

2)Destructor

A destructor is a member function having sane name as that of its class preceded by ~(tilde) sign and which is used to destroy the objects that have been created by a constructor. It gets invoked when an object’s scope is over.
~Circle() {}

Example : In the following program constructors, destructor and other member functions are defined inside class definitions. Since we are using multiple constructor in class so this example also illustrates the concept of constructor overloading
#include<iostream>
using namespace std;
 
class Circle //specify a class
{
    private :
        double radius; //class data members
    public:
        Circle() //default constructor
        {
            radius = 0;
        }
        Circle(double r) //parameterized constructor
        {
            radius = r;
        }
        Circle(Circle &t) //copy constructor
        {
            radius = t.radius;
        }
        void setRadius(double r) //function to set data
        {
            radius = r;
        }
        double getArea()
        {
            return 3.14 * radius * radius;
        }
        ~Circle() //destructor
        {} 
};
 
int main()
{
    Circle c1; //defalut constructor invoked
    Circle c2(2.5); //parmeterized constructor invoked
    Circle c3(c2); //copy constructor invoked
    cout << c1.getArea()<<endl;
    cout << c2.getArea()<<endl;
    cout << c3.getArea()<<endl;
    return 0;
}
 

Difference between convert.tostring() and .tostring() in Asp.Net

Convert.tostring() :-  

                                   It can handle null values.


.tostring():-  


                                   It cannot handle null values.