Sunday 1 January 2012

VII. POLYMORPHISM



VII. POLYMORPHISM


Paragraph Answers


1.What is a function overloading ? Explain with an example.
The ability of the function to process the message or data in more than one form is called as function overloading.
The rules of Function overloading
  • Each overloaded function must differ either by the number of its formal parameters or their data types.
  • The return type of overloaded functions may or may not be the same data type.
  • The default arguments of overloaded functions are not considered by the C++ compiler as part of the parameter list.
  • Do not use the same function name for two unrelated functions.
Example:
#include<iostream.h>
#include<conio.h>
float area ( float radius )
{ return ( 22/7 * radius * radius );}
float area ( float length, float breadth )
{ return ( length *breadth ) ;}
void main()
{
clrscr();
float r,l,b;
cin>>r>>l>>b;
cout<<“\n The area of circle is ... “<< area(r);
cout<<“\n The area of a rectangle is ... “<< area(h,b);
}
Here,
float area ( float radius ) ,
float area ( float length, float breadth ) Overload Functions

2. What is operator overloading? Explain with an example.
The mechanism of giving special meaning to an operator is called as operator overloading.
Operator overload provide
  • New function definitions for basic C++ operators like +, *, -, ++, --, >, <, + = and the like.
  • One cannot overload C++ specific operators like membership operator (.), scope resolution operator (::), sizeof operator and conditional operator.
  • The overloaded function definitions are permitted for user defined data type.
  • Operator functions must be either member functions or friend functions.
  • The new definition that is provided to an operator does not overrule the original definition of the operator.
Process of operator overload
  • Create a class that defines the data type that is to be used in the overloading operations
  • Declare the operator function operator () in the public part of the class.
  • Define the operator function to implement the required operations(symbol).
Ex.
#include <iostream.h>
#include <conio.h>
#include <string.h>
void strings()
{
cout<<”number..”;
}
char operator + (strings x)
{…..
……
}
void main()
{
Strings s1,s2;
a=s1+s2;
cout<<a;
}
3.What does the operator overloading provide?
Ref. PQ.2
4. List out the rules of Function overloading
Ref. PQ.1
5.List out the rules for overloading operators.
  • Only existing operators can be overloaded. New operators cannot be created.
  • The overloaded operator must have atleast one operand of user defined type.
  • The basic definition of an operator cannot be replaced .
  • One can give additional functions to an operator.
  • Overloaded operators behave same as the basic operators in terms of their operands.
  • When binary operators are overloaded, the left hand object must be an object of the relevant class.
  • Binary operators overloaded through a member function take one explicit argument
SHORT ANSWERS
1.What is function overloading?
The ability of the function to process the message or data in more than one form is called as function overloading.
2.How is polymorphism achieved?
Polymorphism is achieved through function overloading and operator overloading.
3.Group the overloaded function from the given list of prototypes.
int add (int x, int y); int sub (int x, int y); int add (float a, float b, float c); int mul (int x, int y); int sub (float x);
Ans: 1)int add (int x, int y);
int add (float a, float b, float c);
2)int sub (int x, int y);
int sub (float x);
4.From the given prototype of the function answer the questions.
float sum (float a, float b. float c);
a)What type of arguments does function prototype has?
b)What type of value does function return?
c}What is the name of the function?
a)float type. b)float type. c)sum
5.Write whether the following prototypes for overloading are valid or invalid. State reason.
int addition (int x, int y); float addition (int x, int y);
Invalid.
Overloaded functions must differ either by the number of parameters or by their datatype.
6.Write whether the following prototypes for overloading are valid or invalid. State reason.
int matrix_add (int x, int y);
int matrix_add (int x, int y, int z);
Valid.
Number of parameters differ.
7.Write whether the following prototypes for overloading are valid or invalid. State reason.
float time (int m, int n);
float time (float m, float n);
Valid.
The parameters differ by their type.
8.How are functions invoked in function overloading?
  • The compiler adopts BEST MATCH strategy, so the compiler will
  • Look for the exact match of a function prototype with that of function call statement.
  • When C++ did not find the exact match of a function call,
  • The compiler will promote data to find the nearest match.
  • For example, char type can be promoted to integer/float/ double.
  • It is called integral promotion of data.
9.What is integral promotion of data?
  • When C++ did not find the exact match of a function call,
  • The compiler will promote data to find the nearest match.
  • For example, char type can be promoted to integer/float/ double.
  • It is called integral promotion of data.
  • Integral promotions are purely compiler oriented.
10.What are the possible integral promotions while invoking overloaded functions?
  • Char data type can be converted to integer / float / double.
  • Int data type can be converted to char / double / float.
  • Float data type to integer / double / char.
  • Double data type to float or integer.
11.Write are two rules for function overloading.
  • Ref.PA
12..List out any four operators that can be overloaded.
13..List out the operators that cannot be overload in C++.
  • membership operator (.),
  • scope resolution operator (::),
  • sizeof operator and
  • conditional operator (?:)
15..What are the rules of operator overloading by using binary operators
  • When binary operators are overloaded, the left hand object must be an object of the relevant class.
  • Binary operators overloaded through a member function take one explicit argument.
17..What are the process of operator overloading? or
List out the steps involved to define an overloaded operator.
  • Create a class that defines the data type that is to be used in the overloading operations
  • Declare the operator function operator () in the public part of the class.
  • Define the operator function to implement the required operations.
18..What is the use of strcat() function?
It is used to concatenate (merge)strings

19.What is the advantage of operator overloading?
Advantages of operator overloading
  • We can give addition functionality to the normal C++ operators like, +, -, x, /, ++, -- etc.
  • We can be use operator overloading to add or substract two strings, or to add two complex numbers or to add two matrices etc. with usual addition and subtraction .
  • Along with new functionality to the operator, we can also use the original definition of the operator
20.What is operator overloading?
The mechanism of giving special meaning to an operator is called as operator overloading.

No comments:

Post a Comment