Thursday 23 March 2017

FUNCTION OVERLOADING

FUNCTION OVERLOADING
What is polymorphism?
One of the important features of OOPS is Polymorphism. Polymorphism refers to ‘one name having many forms’ different behavior of an instance depending upon the situation.
In what two ways C++ implement polymorphism?
C++ implements polymorphism through overloaded functions and overloaded operators.
What does overloading mean?
The term overloading means a name having two or more distinct meanings.
Thus an overloaded function refers to a function having(one name and) more than one distinct meanings.
What is an overloaded operator?
Similarly, when two or more distinct meanings are defined for an operator, it is said to be an overloaded operator.
What is static or early binding?
The ability of the compiler to select the appropriate function for a particular call at the compile time itself from a set of overloaded functions that are defined. In other words, the required function gets determined only at compile time.
What is function overloading?
When several function declarations are specified for a single function name in the same scope, the (function) name is said to be overloaded. C++ allows functions to have the same name if it can distinguish them by their number and type of arguments.
Or
A function name having several definitions that are differentiable by the number or types of their arguments is known as overloading of functions.
Eg:
void prnsqr(int i);//overloaded for integer
void prnsqr(char c);// overloaded for character
void prnsqr(float f);//overloaded for float
void prnsqr(double d);//overloaded for double


void prnsqr(int i)
{
cout<<"Integer "<<i<<"\n";
}
void prnsqr(char c)
{
cout<<c<<" is a character "<<"\n";
}
void prnsqr(float f)
{
cout<<" Float "<<f"\n";
}
void prnsqr(double d)
{
cout<<"Double "<<d<<"square is"<<i*i<<"\n";
}
What is the need for function overloading?
Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster.
What is a function’s signature?
A function’s argument list(i.e. number and type of arguments) is known as the function’s signature.
Are functions with same signature and same name but different return types allowed?
No
What are the restrictions on overloaded functions?
·         Any  two functions in a set of overloaded functions must have different argument lists.
·         Overloading functions with argument lists of the same types, based on return type alone is an error.
·         Member functions cannot be overloaded .
·         Typedef declarations do not affect overloading mechanism.
What is argument matching?
Argument matching involves comparing the actual arguments of the call with the formal arguments of each declared instance of the function.
What are the three possible cases a function call may result in?
1)      A match – A match is found for a function call
2)      No match – No match is found for the function call
3)      Ambiguous match – More than one defined instance match for the function call.
Ambiguity between float and double, int and long.

What are the steps followed by compiler in order to find the best possible match?

1)      Search for an exact match

void afunc(int);
void afunc(double);
afunc(13);// exact match. Matches afunc(int)

2)      A match through promotion
 void afunc(int);
void afunc(double);
afunc('c');// match through promotion. Matches afunc(int)

3)      A match through application of standard C++ conversion rules
void afunc(char);
void afunc(double);
afunc(97);// match through standard conversion rules. Matches afunc(char)

4)      A match through application of a user-defined conversion.


SA Qns – P-137

1)      Based on the number and type of arguments, the compiler is able to interpret more than one definition during the compile time.
Steps:
i)                    Looks out for exact match, if not found
ii)                   Looks out for match in promotion , if not found
iii)                 Looks out for promotion through conversion

2)      When the number and type of the  formal and actual arguments are the same, then it is said to be exact match.
Eg:
void fun(int a)
{  }

void main()
{
fun(5);
}

4)

####
91827
5)
i)

void repeat(int x, char c)
{
for(int i=1;i<=x;i++)
cout<<c;
}
void repeat()
{
for(int i=1;i<=12;i++)
cout<<"*";
}
void repeat(int x, char c='*')
{
for(int i=1;i<=x;i++)
cout<<c;
}
void repeat(char c, int x=12)
{
for(int i=1;i<=x;i++)
cout<<c;
}

                Both
             ii)
                               
                Function overloading

                iii)
                Default argument


                iv)
                Neither
iv)                 Function overloading onlye

6)

C++ allows a function to assign a parameter a default value when no argument corresponding to that parameter is specified in a call to that function. The default value is specified in a manner syntactically similar to a variable initialization. 
- For example, this declares MyFunction as taking one int argument with a default value of 0 :
void MyFunction(int i =0)
{
   //…
   //…
}
- Now, this MyFunction() can be called one of the following two ways :
MyFunction(10);   //passing explicit value
MyFunction();   //not passing any value; function will give default value

7)
int N=5;
int sum(int arr[])
{
int total=0;
for(int i=0;i<N;i++)
total+=arr[i];
return total;
}
int sum(int arr[], char ch)
{
int etotal=0,ototal=0;

if(ch=='E')
{for(int i=0;i<N;i++)
  if(arr[i]%2==0)
   etotal+=arr[i];
   return etotal;}
else if(ch=='O')
{for(int i=0;i<N;i++)
   if(arr[i]%2!=0)
   ototal+=arr[i];
   return ototal;}

}

Tuesday 21 March 2017

Review of C++ - Text Book Questions

Text Book P -89 Very Short/Short Answer Questions:

2) What is the difference between fundamental data types and derived data types? Explain with examples.
Fundamental data types are those data types that are not composed of other data types.
eg int x; float f;
Derived data types derive other types from the fundamental types by using declaration operators.
int arr[4]; const int x=10;

3) What is the purpose of a header file in a program?

enables us to work with all declarations/definitions which are inside the header files.

4) What main integer types are offered by C++?
short, int , long

5) Explain the usage of the following with the help of an example.
constant - value of the named constant cannot be altered during the program run.
const int x = 5;

reference -  is an alternative name for a variable. it provides an alias name for previously defined variable.
int total = 50;
int &sum=total;
total+=25;
cout<<sum; // will display 75

variable:named memory location.
eg:int x;

Union: user defined data type . it is a memory location that is shared by two or more different variables.
union share{
int i;
char ch;
";
union share cnvt;

6) How many ways can a variable be initialized into? Give examples for each type of initialization.

3 ways.
1) int x=9;
2) int x(9);
3) int x=9+3;

7)How are the following 2 statements differ?
  char pcode =75;
  char pcode = 'K';
 1st one integer constant is assigned to a character variable, 2nd a character constant is assigned.

9)If value is an identifier of int type and is holding value 200, is the following statment correct?
  char code = value;

compiler doesnt give error, but since char holds maximum 127 as value, it results in data loss.

10)The data type double is another floating point type. Then why is it treated as a distinct data type?

 because it stores maximum size in floating point

11)Explain the impact of access modifier const over variables. Support your answer with examples.
 By prefixing const to  data types, it doesnt allow to change values during program run.

const int x=5;

12) What are the arithmetic operators in C++? Distinguish between unary and binary arithmetic operators. Give examples for each of them.
=,-,*,/,%
unary - operates on a single operand -
a++;b--;
binary - operates on two operands -
c=a+b;
c=a%b;

13) What is the function of increment/decrement operators? How many varieties do they come in? How are these two varieties different from one another?

The function of increment/decrement operators is to increment or decrement the operands/variables by 1.They come in 2 varieties - pre and post. i.e. pre-increment/pre-decrement, post-increment/post-decrement. pre-increment is based on change and use rule. post-increment is based on use and change rule.

19) Observe the following C++ code and write the name(s) of the header files which will be essentially required to run it in a C++ compiler.
void main()
{
char CH,STR[20];
cin>>STR;
CH=toupper(STR[0]);
cout<<STR<<" starts with "<<CH<<endl;
}
iostream.h, ctype.h

20) Observe the following C++ code and write the name(s) of the header files which will be essentially required to run it in a C++ compiler:
void main()
{
int Number;
cin>>Number;
if(abs(Number)==Number)
 cout<<"Positive:"<<endl;
}
iostream.h, math.h

21) string.h, iostream.h

24) #include <iostream.h>
  void main()
{
int present = 25, past=35;
void assign(int ,int=30);
void assign(past);
}
void assign(int default1, int default2=30)
{
default1=default1+default2;
cout<<default1<<default2;
}

25) i) typedef char STRING[50];
 void main()
{
STRING City;
gets(City);
cout<<City[0]<<"\t"<<City[2];
cout<<City<<endl;
}

ii) #define Convert(P,Q) P+2*Q
    void main()
{
 float A,B,Result;
 cin>>A>>B;
Result=Convert(A,B);
cout<<"Output :"<<Result<<endl;
}



28) iostream.h, ctype.h

29)
















31)










32) Point=3





















33) Min and Max value is 2 and 3
iv) is the ans

34)When Turn is 3, min and max value are 0 and 2
Ans:i) & iii)

35)Mynum - least and highest - 20,24
Ans:iv)

36)
42)








49) The benefit of using function prototype for a function is that it specifies the return type of the function and the type of arguments if any.
void check(int );// prototype for the function check which doesnt return anything and takes int as parameter.

50) cin>>ch; // ignores space
     cin.get(ch);// doesnt ignore space

51) Array is a collection of homogeneous finite number of elements stored contiguously , all of which are of a same data type. To efficiently store and retrieve data by using a single variable array name and its subscript.

52) A collection of 1-d arrays is 2-d arrays. Data is stored in form of rows and columns.
      students' marks in different subjects, prices of commodities

53) Array of strings - each string ends with '\0' null character
     other 2-d arrays - numeric arrays dont end with a null character

char str[5][12]; // 5 strings of 12 characters each
int [3][4];/ 3 rows of 4 elements each.

54)can be sized or unsized.
55) when the array is not given a size, can be used in situations where the size is unknown.
56) Function prototyping specifies the function return type and type of arguments if any.
Advantage - by knowing the prototype, it is possible to determine the function return type and type of arguments. Function can be defined anywhere in the program.
57) Actual parameters are used in function call. Formal parameters are used in function definition.
58) void test();
 int convert(float);
double promote(double,double);
long(int[],int);
int(char[]);

59)default argument is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn't provide a value for the argument with default value. 
// A function with default arguments
void sum(int x, int y=9)
{
   cout<<x<<" "<<y<<endl;
}


void main()
{
    sum(10); // function 1
sum(5,3); // function 2
}
function 1 will produce following output
10 9
function 2 will produce foll output
5 3

In C++, an argument to a function can be declared as const as shown below:
int length(const int &s);
The qualifier const tells the complier that the function should not modify the argument. The compiler will generate an error when this condition is violated. This type of declaration is significant only when we pass arguments by reference or pointers.

60)In call by value, a copy of actual parameters is made to formal parameters. Function works only with the copy. Any change made in formal will not be reflected in the actual.
In call by reference, an alias of the actual parameters is received as formal in the function, any changes made in formal will be reflected in the actual.

Eg: call by value
void change(int a, int b)
{
a=6;b=10;
}
void main()
{
int x=1,y=2;
change(x,y);  
cout<<x<<" "<<y;// will display 1 2
}

Eg: call by reference
void change(int &a, int &b)
{
a=6;b=10;
}
void main()
{
int x=1,y=2;
change(x,y);  
cout<<x<<" "<<y;// will display 6 10
}