Wednesday, 28 November 2018

Friend Function in C++


A friend function of a class is defined outside that class scope but it has the right to access all private and protected members of the class. Even though the prototypes for friend functions appear in the class definition, friends are not member functions.

To declare a function as a friend of a class, precede the function prototype in the class definition with keyword friend.

class ABC
{ 
    ….
   public:
    ….
       friend   void   xyz();  
}       




Friend function characteristics

1.It is not in the scope of the class to which it has been declared as friend.

2.Since it is not in the scope of the class, it cannot be called using object of that class.

3.It can be invoked like a normal function without the help of any object.

4.Unlike member functions, it cannot access the member names directly and has to use an object name and dot membership operator with each member name (e.g.  A.x).

5.It can be declare either in the public or the private part of the class without affecting its meaning.

6.Usually, it has the object as arguments.

Example: Friend Function
#include<iostream.h>
#include<conio.h>
class  sample
{
  int a,b;
  public:
     void setvalue()
     {
      a=26;
      b=63;
      }
 friend float mean(sample s);
};
float mean(sample s)
{
  return float(s.a+s.b)/2.0;
}
int main()
{
  sample x;
  x.setvalue();
  cout<<"Mean value is “<<mean(x);
  return 0;
}

For Complete C++ Training in Gwalior

Exception Handling Mechanism in C++


An exception is a problem that arises during the execution of a program. A C++ exception is a response to an exceptional circumstance that arises while a program is running, such as an attempt to divide by zero.

C++ exception handling mechanism provides three keywords.
try
catch
throw

try:
represents a block of code that can throw an exception. It's followed by one or more catch blocks.

catch:
A program catches an exception with an exception handler at the place in a program where you want to handle the problem. The catch keyword indicates the catching of an exception.

try
{
   statement1;
   statement2;
}
catch(argument)
{
   statement3;
}

throw:
  Used to throw an exception.

Syntax of throw statement
  throw (excep);
  throw  excep;
  throw;   //re-throwing of an exception


Example 1:
#include <iostream>
using namespace std;

int main()
{
   int a,b,c;
   float  d;

   cout<<"Enter the value of a:";
   cin>>a;
   cout<<"Enter the value of b:";
   cin>>b;
   cout<<"Enter the value of c:";
   cin>>c;
  
   try
   {
              if((a-b)!=0)
              {
                 d=c/(a-b);
                 cout<<"Result is:"<<d;
              }
              else
              {
                 throw(a-b);
              }
   }
   catch(int i)
   {
              cout<<"Answer is infinite because a-b is:"<<i;
   }
   
    system("PAUSE");
    return 0;
}

Catch All Exception
There is a special catch block called “catch all’ catch(…) that can be used to catch all types of exceptions.

#include<iostream>
using namespace std;

void test(int x)
{
   try
   {
        if(x==1)
           throw x;     //int
        else if(x==0)
           throw 'x';    //char
        else if(x==-1)
           throw 1.0;   //double
        cout<<"End of try block \n";
   }
  catch(...)
  {
            cout<<"Caught an exception\n";
  }
}
int main()
{
   cout<<"Testing Generic Catch\n";
   test(1);
   test(0);
   test(-1);
   return 0;
}

For Complete C++ Training in Gwalior
Website :http://www.affyinformatics.com/cpp-training.html

Graphics in C Language

1.Applying the visual properties to a dos application is called Graphics.

2.Whenever we work with graphics application, always we require including "graphics.h"

3.Whenever we work with graphics application then we require finding out "EGAVGA.BGI" file location. It will install all the resources of graphics to the application.

4.Generally the file is available in "C:\TC\BGI" directory.

5.Whenever we work with graphics application then we require initializing graphics properties properly. 

6.By using "initgraph()" function we can initialize graphics property.

7.At the time of initializing the graphics due to graphics resource we can get initialization related errors also. 

8.Initialization related errors can be found by using "graphicresult()" function.

9."graphicresult()" function returns error code, if the graphics are not initialized properly, if it is initialized properly then we will get "grOK".

10.By using "grapherrormsg()" function we can display error message.

11.By using "closegraph()" function we can close all properties of the graphics.


Example 1:

#include<graphics.h>
#include<conio.h>
void main()
{
    int gd = DETECT, gm;
    /* initialization of graphic mode */
    initgraph(&gd, &gm, "");
    line(100,100,200,200);
    getch();
    closegraph();

}

Example 2:

#include<graphics.h>
#include<conio.h>
void main()
{
            int gd = DETECT, gm;
            initgraph(&gd, &gm, "");
            circle(300, 300, 50);
            getch();
            closegraph();
}

Example:3

#include<graphics.h>
#include<stdio.h>
void main()
{
   int gdriver = DETECT, gmode;
   int x = 200, y = 200;
   initgraph(&gdriver, &gmode, "");
   outtextxy(x, y, "Hello World");
   closegraph();
}

 Example 4:

#include<graphics.h>
#include<conio.h>
void main()
{
            int gd = DETECT, gm;
            initgraph(&gd, &gm, "");
            ellipse(100, 100, 0, 360, 50, 25);
            getch();
            closegraph();
}

For Complete C Language Training

Website: http://www.affyinformatics.com/c-language-training.html 



Command Line Arguments in C Language


Parameters or Values can be passed to program from the command line which are received and processed in main function. Since the arguments are passed from the command line hence they are called as command line arguments. All commands on Unix Operating System use this concept.

Two built in formal parameters are used to accept parameters in main.

  argc : Contains number of command line arguments. It is type int.

  argv : A pointer to an array of strings where each string represents a token of the arguments passed. It is a character array of pointers.

  main(int argc, char *argv[])



Example:

     #include <stdio.h>
int main( int argc, char *argv[] )
{
   if( argc == 2 )
   { 
       printf("The argument supplied is %s\n", argv[1]);
   }
   else if( argc > 2 )
   {
        printf("Too many arguments supplied.\n");
    }
   else
   {
         printf("One argument expected.\n");
    }
}

For Complete C Language Training in Gwalior

Web Services in ASP.NET


Web Service represent the business logic encapsulated in a component. Since these services are hosted on a http server these are referred as web services.

Web Services are stateless in nature. i.e. Every request treated as a new client.
The request and response data is transmitted using XML. Hence, they are architecture neutral. i.e. A web service developed using JAVA(JSP) may be consumed by an ASP.NET web page.

The underlying protocol used is SOAP(Simple object access protocol) SOAP envelope consist of SOAP Header and SOAP Body.

Note:
A WebService class is derived from System.Web.Services.WebService
The attribute [WebService]  marked for the class indicates that the class will act as a web service.

The web service class may consist of several methods but only those methods which are marked by the attribute [WebMethod] will be exposed to the web service consumer.
A Consumer is the web application that will send request to the web service and obtain a response.

The attribute [WebService] has a property Namespace whose ideal value may be URL of the web service where it is hosted conventionally, otherwise it may be set to any non-existent URL.



Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public int Addition(int x,int y)
    {
        return x + y;
    }  
}

Consume a Web Service into ASP.NET Web Application
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        int a = Convert.ToInt32(TextBox1.Text);
        int b = Convert.ToInt32(TextBox2.Text);
        MyWebservice.Service affy = new MyWebservice.Service();
        int c = affy.Addition(a, b);
        TextBox3.Text = c.ToString();
    }
}

For Complete ASP.NET Training in Gwalior

Lambda Function Python

Function which doesn’t contain any name explicitly is known as lambda functions or anonymous functions.

syntax:
       lambda argument:expression

lambda function can contain 'n' number of arguments.
lambda function can contain only one expression.
After executing lambda function it will returns the expression value.

Ex:
double=lambda x:x*x
print(double(5))

Generally we use the lambda functions when ever we want to pass one function as parameters to another function

Ex:
my_list=[1,2,3,5,6,8]
even_list=filter(lambda x:(x%2==0),my_list)
odd_list=filter(lambda x:(x%2!=0),my_list)
print(even_list)
print(odd_list)

For Complete Python Training in Gwalior 

NuGet Packages in ASP.NET MVC

The ASP.NET Web Forms Application template includes a set of NuGet packages. These packages provide componentized functionality in the form of open source libraries and tools. There is a wide variety of packages to help you create and test your applications. Visual Studio makes it easy to add, remove, and update NuGet packages. Developers can create and add packages to NuGet as well. 

When you install a package, NuGet copies files to your solution and automatically makes whatever changes are needed, such as adding references and changing you’re the configuration associated with your Web application. If you decide to remove the library, NuGet removes files and reverses whatever changes it made in your project so that no clutter is left. NuGet is available from the Tools menu in Visual Studio.


For additional information about the installed libraries and packages included with the ASP.NET Web Forms template, see the list of installed NuGet packages.

To do this, In Visual Studio
1. Create a new Web Forms project,
2. select Tools ->
3. Library Package Manager ->
4. Manage NuGet Packages for Solution, and
5. Select  Installed packages in the Manage NuGet Packages dialog box.

For ASP.NET MVC Training in Gwalior
Website: