2004
1.a) What is polymorphism? Give an example in C ++ to show its implementation in C++.
Ans:Polymorphism is the attribute that allows one interface to be used with different situation.C++ implements polymorphism through virtual functions, through overloaded functions and overloaded operators.
A virtual function is used to specify the interface in abstract class, but its implementation details are made available by the concrete class(es).
An overloaded function refers to a function having (one name and) more than one distinct meanings. Similarly, when two or more distinct meanings are defined for an operator, it is said to be an ‘overloaded operator’. It is the compiler’s job to select the specific action as it applies to each situation. Eg: The program in the next answer.
2003:
2.a) What do you understand by function overloading? Give an example illustrating its use in a c++ program.
Ans: A function name having several definitions that are differentiable by the number or types of their arguments, is known as an overloaded function and this process is known as function overloading.
Function overloading not only implements polymorphism but also reduces number of comparisons in a program and thereby makes the program run faster.
Example: program illustrating function overloading:
//Program to find out area of a circle or area
of rectangle using //function overloading.
#include<iostream.h>
#include<conio.h>
void area(float r)
{cout<<”\nThe area of the circle =“<<3.1415*r*r; }
void area(float l,float b)
{ cout<<”\nThe area of the rectangle =“<<l*b; }
void main( )
{ float rad,len,bre;
int n;
clrscr( );
cout<<”\n1. Area of a Circle…”;
cout<<”\n2. Area of a Rectangle…”;
cout<<”\n\nEnter your choice: “;
cin>>n;
switch(n)
{
case 1: cout<<”\nEnter the radius: “;
cin>>rad;
area(rad);
break;
case 2: cout<<”\nEnter the length and breadth: “;
cin>>len>>bre;
area(len,bre);
break;
default: cout<<”\nYou have to enter either 1 or 2”;
} //end of switch
getch( );
}
2000:
1.a) Illustrate the concept of function overloading with the help of an example.
1
Ans: The above answer.
CBSE Computer Science Solved Revision Tour By Mr. Ravi Kiran ( [email protected] )