Tuesday, 13 March 2018

IMP Question C++


QUESTION BANK
Classes and objects
Q1.What are the various characteristics of Object oriented programming techniques ?
Q2.What is a class ? How does it accomplish data hiding?
Q3.How do structure of C and C++ differ ?
Q4.Diifferentiate between private,public and protected?
Q5.Discuss the significance of scope resolution operator(:: ) in C++.
Q6.Compare object oriented  programming  with procedural programming?
Q7.What are the differences between objects and classes ?Discuss instantiation of  
      objects?
Q8.Distinguish between Object and Class ?
Q9.Explain  with the help of suitable example how data abstraction and data
     encapsulation takes place?
Q10.  What is difference between  a pointer and  a reference?              
Constructor and Destructor
Q11What is the difference between a constructor and a simple member function of a class? When is a constructor member function invoked in a class?
Q12. What is constructor overloading explain with example?
Q13.What is default Constructor  ? How is it different from constructor with default values?
Q14.Create a class ,which keeps track of number of instances of this class.Using Static data  members ,constructor and destructor to maintain updating information about active objects.
Q15. .Give an Illustration of copy constructor , multiple  constructs ,dynamic constructor 
        in   a class.
Q16.What is a destructor function  ? What is its use ? How many times is the destructor invoked?What happens if a destrucor is not given  in a class?
Q17.Write a program to read five real numbers and print average using a static member class .Use parameterized constructor for entering the numbers.
Q18.Distinguish between the following two statements:
            Item I1 (I2);
            Item I2 = I1;
Where I1 and I2 are objects of class Item.
Q19.Create a class for manipulating coordinates in Polar coordinate System .Represent points as objects .The class Polar must include data members  radius and theta .Member functions to add ,subtract and to find angle are to be included in the class.
Q20.What is an empty class? Can instances of empty class be created?
Friend Function
Q21. What  is a friend? Do friends violate encapsulation?
Q22 .Give some advantages and disadvantages of using Friend function?   
Q23.State True or false:Friend functions have access to  only  public members  of a class?
Q24. Illustrate with an example how can a common friend function to two different classes be declared?
Q25.Discuss the relation of a friend function  with public and protected data members of a class?
Pointers and Virtual Functions
Q25.What is Dangling pointer?
Q26. Explain ,with an example how an array of objects is created using pointers?
Q27.What is  significance of THIS pointer and how is it used?
Q28.What is Auto pointer?
Q29.What are pure virtual functions? How do they differ from normal virtual function?
Q30.What are virtual destrucors? How do they differ from normal destructors?
Q31.What is a smart pointer?
Q32.What is the difference between  const  char  *myPointer and char *const myPointer?
Q33.What is Overriding?
Q34.How are virtual functions implemented?

Inheritance
Q35.What is inheritance in C++?
Q36.Explain the need of inheritance in C++ using suitable examples?
Q37.What is visibility mode? What are the different inheritance visibility modes supported by C++.
Q38.With an example explin what are the different types of inheritance?
Q39.In what order are the class constructors called when a derived class object is created?
Q40.What is containership or delegation? How does it differ from inheritance?
Q41.Describe how an object of a class that contains objects of other classes is created?
Q42. What is an abstract class?
Polymorphism and Operator overloading
Q43.What is Polymorphism?
Q44.What is overloading?
Q45.Name the operators which cannot be overloaded?
Q46.What are the benefits of operator overloading?
Q47.Design a program to input two complex numbers and add them with + using operator overloading?
Q48.What is the difference between assignment operator and arithmetic assignment operator overloading?
Q49.Discuss  the benefits of constructor overloading? What are the similarities and differences between construcot and function overloading?
Q50.What are the limitations of overloading unary increment/decrement operator?How are they overcome?
Q51.Why is friend function not allowed to access members of a class directly although its body can appear within the class body?
Q52. Define two classes Polar and Rectangle to represent points in the polar and rectangle systems. Use conversion routines to convert from one system to the other.


Exception Handling
Q63.What is exception handling ? Explain try, throw and catch using an example?
Q64.What happens when a function throws an exception that was not specified by an exception specification for this function?
Q65.What are the advantages of using exception handling mechanism in a program?



Monday, 12 March 2018

Function





                                                Functions

Before reading this tutorial you should have knowledge of pointers.

Functions are building blocks of the programs. They make the programs more modular and easy to read and manage. All C++ programs must contain the function main( ). The execution of the program starts from the function main( ). A C++ program can contain any number of functions according to the needs. The general form of the function is: -

return_type  function_name(parameter list)
{
            body of the function

}

The function of consists of two parts function header and function body. The function header is:-

            return_type   function_name(parameter list)

The return_type specifies the type of the data the function returns. The return_type can be void which means function does not return any data type. The function_name is the name of the function. The name of the function should begin with the alphabet or underscore. The parameter list consists of variables separated with comma along with their data types. The parameter list could be empty which means the function do not contain any parameters. The parameter list should contain both data type and name of the variable. For example,

            int factorial(int n, float j)

is the function header of the function factorial. The return type is of integer which means function should return data of type integer. The parameter list contains two variables n and j of type integer and float respectively. The body of the function performs the computations.




Function Declaration

A function declaration is made by declaring the return type of the function, name of the function and the data types of the parameters of the function.  A function declaration is same as the declaration of the variable. The function declaration is always terminated by the semicolon. A call to the function cannot be made unless it is declared. The general form of the declaration is:-

return_type function_name(parameter list);

For example function declaration can be
            int factorial(int n1,float j1);

The variables name need not be same as the variables of parameter list of the function. Another method can be
           
int factorial(int , float);

The variables in the function declaration can be optional but data types are necessary.

Function Arguments

The information is transferred to the function by the means of arguments when a call to a function is made.  Arguments contain the actual value which is to be passed to the function when it is called.  The sequence of the arguments in the call of the function should be same as the sequence of the parameters in the parameter list of the declaration of the function. The data types of the arguments should correspond with the data types of the parameters. When a function call is made arguments replace the parameters of the function.

The Return Statement and Return values

A return statement is used to exit from the function where it is. It returns the execution of the program to the point where the function call was made. It returns a value to the calling code. The general form of the return statement is:-

return expression;

The expression evaluates to a value which has type same as the return type specified in the function declaration. For example the statement,

            return(n);

is the return statement of the factorial function. The type of variable n should be integer as specified in the declaration of the factorial function. If a function has return type as void then return statement does not contain any expression. It is written as:-

            return; 

The function with return type as void can ignore the return statement. The closing braces at the end indicate the exit of the function. Here is a program which illustrates the working of functions.

#include<iostream>
using namespace std;
int factorial(int n);

int main ()
{
            int n1,fact;
            cout <<"Enter the number whose factorial has to be calculated" <<  endl;
            cin >> n1;
            fact=factorial(n1);
            cout << "The factorial of " << n1 << "  is : " << fact << endl;
            return(0);
           
}
int factorial(int n)
{
            int i=0,fact=1;
            if(n<=1)
            {
                        return(1);
            }
            else
            {
                        for(i=1;i<=n;i++)
                        {
                                    fact=fact*i;
                        }
                        return(fact);
            }
}

The result of the program is:-

http://cpp-tutorial.cpp4u.com/images/image002_0037.jpg

The function factorial calculates the factorial of the number entered by the user. If the number is less than or equal to 1 then function returns 1 else it returns the factorial of the number. The statement

            int factorial(int n);

is a declaration of the function. The return type is of integer. The parameter list consists of one data type which is integer. The statement

            cout <<"Enter the number whose factorial has to be calculated" <<  endl;
            cin >> n1;
makes the user enter the number whose factorial is to be calculated. The variable n1 stores the number entered by the user. The user has entered number 5. The statement
           
            fact=factorial(n1);

makes a call to the function. The variable n1 is now argument to the function factorial. The argument is mapped to the parameters in the parameter list of the function. The function header is
           
            int factorial(int n)

The body of the function contains two return statements. If the value entered by the user is less than and equal to 1 then value 1 is returned else computed factorial is returned. The type of the expression returned is integer.

Parameter passing mechanism

There are two parameter passing mechanisms for passing arguments to functions such as pass by value and pass by reference.

Pass by value

In pass be value mechanism copies of the arguments are created and which are stored in the temporary locations of the memory. The parameters are mapped to the copies of the arguments created. The changes made to the parameter do not affect the arguments. Pass by value mechanism provides security to the calling program. Here is a program which illustrates the working of pass by value mechanism.

#include<iostream>
using namespace std;
int add(int n);

int main()
{
            int number,result;
            number=5;
            cout << " The initial value of number : " << number << endl;
            result=add(number);
            cout << " The final value of number : " << number << endl;
            cout << " The result is : " << result << endl;
            return(0);
}

int add(int number)
{
            number=number+100;
            return(number);
}

The result of the program is:-

http://cpp-tutorial.cpp4u.com/images/image004_0013.jpg

The value of the variable number before calling the function is 5. The function call is made and function adds 100 to the parameter number. When the function is returned the result contains the added value. The final value of the number remains same as 5. This shows that operation on parameter does not produce effect on arguments.

Pass by reference

Pass by reference is the second way of passing parameters to the function. The address of the argument is copied into the parameter. The changes made to the parameter affect the arguments.  The address of the argument is passed to the function and function modifies the values of the arguments in the calling function. Here is a program which illustrates the working of pass by reference mechanism.

#include<iostream>
using namespace std;
int add(int &number);

int main ()
{
            int number;
            int result;
            number=5;
            cout << "The value of the variable number before calling the function : " << number << endl;
            result=add(&number);
            cout << "The value of the variable number after the function is returned : " << number << endl;
            cout << "The value of result : " << result << endl;
            return(0);
}

int add(int &p)
{
            *p=*p+100;
            return(*p);
}

The result of the program is:-

http://cpp-tutorial.cpp4u.com/images/image006_0012.jpg           

The address of the variable is passed to the function. The variable p points to the memory address of the variable number. The value is incremented by 100. It changes the actual contents of the variable number. The value of variable number before calling the function is 100 and after the function is returned the value of variable number is changed to 105.

           
           
After an introduction of functions, let us move on to discuss structures.


WEB Service


web service
A web service is a web-based functionality accessed using the protocols of the web to be used by the web applications. There are three aspects of web service development:
  • Creating the web service
  • Creating a proxy
  • Consuming the web service
Creating a Web Service
A web service is a web application which is basically a class consisting of methods that could be used by other applications. It also follows a code-behind architecture such as the ASP.NET web pages, although it does not have a user interface.
To understand the concept let us create a web service to provide stock price information. The clients can query about the name and price of a stock based on the stock symbol. To keep this example simple, the values are hardcoded in a two-dimensional array. This web service has three methods:
  • A default HelloWorld method
  • A GetName Method
  • A GetPrice Method
Take the following steps to create the web service:
Step (1) : Select File -> New -> Web Site in Visual Studio, and then select ASP.NET Web Service.
Step (2) : A web service file called Service.asmx and its code behind file, Service.cs is created in the App_Code directory of the project.
Step (3) : Change the names of the files to StockService.asmx and StockService.cs.
Step (4) : The .asmx file has simply a WebService directive on it:
<%@ WebService Language="C#" CodeBehind="~/App_Code/StockService.cs" Class="StockService" %>
Step (5) : Open the StockService.cs file, the code generated in it is the basic Hello World service. The default web service code behind file looks like the following:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml.Linq;

namespace StockService
{
   // <summary>
   // Summary description for Service1
   // <summary>
  
   [WebService(Namespace = "http://tempuri.org/")]
   [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
   [ToolboxItem(false)]
  
   // 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 Service1 : System.Web.Services.WebService
   {
      [WebMethod]
     
      public string HelloWorld()
      {
         return "Hello World";
      }
   }
}
Step (6) : Change the code behind file to add the two dimensional array of strings for stock symbol, name and price and two web methods for getting the stock information.
using System;
using System.Linq;

using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

using System.Xml.Linq;