Polymorphism is implemented using function overloading. Justify the statement.

1 Answer

Answer :

Polymorphism is a mechanism that allows a developer to have more than one function with same name but different signature. In function overloading, one can make use of more than one function with different signature as well. Hence polymorphism is implemented using function overloading, where one can have more than one functions possess same name but different functionality and behavior.

Example:

#include<iostream.h>

#include<conio.h>

int add(int ar[]);

float add(float arr[]);

void main()

 {

int a[10],i,isum;

float b[5],fsum;

clrscr();

cout<<"\nEnter 10 numbers ";

for(i=0;i<10;i++)

 {

cin>>a[i];

 }

cout<<"\nEnter 5 Float numbers ";

for(i=0;i<5;i++)

 {

cin>>b[i];

 }

isum=add(a);

fsum=add(b);

cout<<"\nThe addition of 10 integer numbers is "<<isum;

cout<<"\nThe addition of 05 Float numbers is "<<fsum;

getch();

}

int add(int x[])

 {

int sum = 0, i;

for(i=0;i<10;i++)

 {

sum = sum+x[i];

 }

return sum;

 }

float add(float x[])

 {

float sum = 0, i;

for(i=0;i<5;i++)

 {

sum = sum+x[i];

 }

return sum;

 }

As shown in above example there are two different functions add, having same name but argument and return type differs and performs different tasks. Based on the argument appropriate function will be called. Function selection will be done at compile time itself. 

Related questions

Description : Write a program to calculate area of circle and area of rectangle using function overloading. 

Last Answer : #include<iostream.h> #include<conio.h> float area(float a) { return (3.14*a*a); } int area(int p,int q) { return(p*q); } void main() { clrscr(); cout< ... ;<<area(6); cout<<"Area of Rectangle:"<<area(5,6); getch(); }

Description : With suitable example, describe use of virtual function in polymorphism.

Last Answer : In order to achieve polymorphism, objects belonging to different classes should be able to respond to the same message at different instances which initiates the use of single pointer variable to refer to objects of ... void accept() { coutroll; coutname; } virtual void display() { cout

Description : What is polymorphism? Enlist different types of polymorphism. What are the differences between them?

Last Answer : Polymorphism: It is a feature of object oriented programming which allows a programmer to have a more than one function having same name but different /same parameters but performs different/ ... Compile Time polymorphism o Virtual Function Difference between Types of Polymorphism:

Description : Compare run-time and compile-time polymorphism.

Last Answer : Compare run-time and compile-time polymorphism.

Description : How to achieve compile time polymorphism explain in detail.

Last Answer : The process of linking of function call to function definition at compile time is called as Compile Time Polymorphism. It can be through Function and operator overloading. Function overloading: The process of defining the ... \n Concatenated String is:"; str1.display(); getch(); }  

Description : Define polymorphism. Enlist its types.

Last Answer : Definition: Polymorphism means ability to take more than one form that means a program can have more than one function with same name but different behavior. Types of polymorphism: 1) Compile time polymorphism 2) Runtime polymorphism 

Description : Write any four differences between compile time and run time polymorphism.

Last Answer : Sr. No. Compile-time Polymorphism Run-time Polymorphism 1 Compile time polymorphism means that an object is bound to its function call at compile time. Run time polymorphism means that selection ... binding. 7 E.g. overloaded function call. E.g. virtual function.

Description : Define the following terms: (i) Data abstraction (ii) Class (iii) Dynamic binding (iv) Polymorphism

Last Answer : (i) Data abstraction: Abstraction refers to the act of representing essential features without including the background details or explanation. Data abstraction is the process of defining a data type, often ... more than one form at different instances depending on the type or number of arguments. 

Description : Write a program for overloading of ++unary operator for inch to feet conversion. 12 inch = 1 feet.

Last Answer : #include #include class abc { int i,f; public: abc(int f1,int i1) { f=f1; i=i1; } void operator ++() {  while(i>11)  {  f++;  i=i-12;  }  cout

Description : Use the concept of operator overloading to overload unary ‘-‘ operator to negate value of variables.

Last Answer : # include <iostream.h> #include<conio.h> class unary { int x, y, z; public: void getdata (int a, int , int c); void display (void); void operator - (); // overload unary minus. }; ... u. display ( ) ; -u; cout<< " u : " ; u. display ( ) ; }

Description : State any four rules for operator overloading.

Last Answer : Rules for operator overloading: 1. Only existing operators can be overloaded. New operators cannot be created. 2. The overloaded operator must have at least one operand that is of user defined type. 3. ... * and / must explicitly return a value. They must not attempt to change their own arguments. 

Description : …………. Is the process of creating new classes, called derived classes from existing classes called base class. A) Inheritance B) Encapsulation C) Polymorphism D) Overloading

Last Answer : A) Inheritance

Description : ………… enable us to hide, inside the object, both the data fields and the methods that act on that data. A) Encapsulation B) Polymorphism C) Inheritance D) Overloading

Last Answer : A) Encapsulation

Description : Which of the following correctly describes overloading of functions? (A) Virtual polymorphism (B) Transient polymorphism (C) Ad-hoc polymorphism (D) Pseudo polymorphism

Last Answer : (C) Ad-hoc polymorphism

Description : .................. allows to create classes which are derived from other classes, so that they automatically include some of its "parent's" members, plus its own members. (A) Overloading (B) Inheritance (C) Polymorphism (D) Encapsulation 

Last Answer : (B) Inheritance

Description : Explain object as a function argument using following points with suitable example: (i) Pass by value (ii) Pass by reference

Last Answer : (i) Pass by Value: When an object is passed by value to a function, a copy of that object is created and changes are reflected on the copy object not on original object. Example: #include #include class Example { int x; public: Example(int a)  {  x=a;  }  void print()  { cout

Description : Write a program in C++ to declare a class measure having data members as add 1, add 2, add 3. Initialize the values of two data members using constructor and display their addition using function.

Last Answer : #include<iostream.h> #include<conio.h> class measure { public: int add1,add2,add3; measure(int a,int b) { add1=a; add2=b; } void cal() { add3=add1+add2; } void display() ... ;>a>>b; measure m1(a, b); m1.cal(); m1.display(); getch(); }

Description : What is Polymorphism? How is it implemented in Java?

Last Answer : A: It literally means the ability to take ‘more than one forms’. The ability of a method to behave in more than one form is called polymorphism. In Java, it is implemented by method (function) overloading (compile time polymorphism) and method overriding (runtime polymorphism).

Description : What is function? What is call by value? What is call by reference? What is the difference between them?

Last Answer : Function: A function is a group/set of statements/instruction that performs a specific task. It can be system defined i.e. clrscr(), getch(), or user defined i.e. add(), sub(). ... reflected inside as well as outside the function. Difference between Call by Value and Call by Reference:

Description : How to define virtual function? Give example.

Last Answer : A virtual function is a member function that is declared within a base class and redefined by a derived class. To define virtual function following syntax is used: Class baseclass { virtual function_name() { } ... } }; Example: #include class Base { public: virtual void show( ) { cout

Description : Explain how to pass object as function argument.

Last Answer : An object may be used as function arguments in two methods:- i) A copy of the entire object is passed to the function. ii) Only the address of the object is transferred to the function. 1) Pass-by-value Since a copy ... (int h, int m) { hours = h; minutes = m; } void puttime(void)  { cout

Description : Explain the need of static member function with example.

Last Answer : A static member function can have access to only other static members (functions or variables) declared in the same class. A static member function can be called using the class name as follows: class_name:: ... test::showcount(); -------------Call to static member function. }

Description : What is pure virtual functions? Write down the rules used for pure virtual function.

Last Answer : A pure virtual function is a function which is declared in a base class and which does not have definition relative to the base class. In such cases, the compiler requires each derived class to either ... A class containing pure virtual functions cannot be used to declare any objects of its own.  

Description : Write a program to show object as function argument. 

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> class objarg  { char str[10]; public: void get() { cout<<"\n Enter a Message"; cin>>str; } void ... clrscr(); o1.get(); o2.copy(o1); o2.display(); getch();  }

Description : What is the need of virtual function? Explain with example.

Last Answer : When base class and its derived class both contain same name and prototype member function then derived class function overrides base class function. Base class pointer is used to refer member functions of its ... Program/Example: #include class Base { public: virtual void show( ) { cout

Description : Why friend function is required? Give four characteristics of friend function.

Last Answer : Friend function: Private members of a class cannot be accessed from outside the class. A non-member function cannot have an access to the private data of a class. Sometimes, two classes may need to ... part of a class without affecting its meaning. 6. Usually it has the objects as the arguments.

Description : Explain concept of function overriding with example.

Last Answer : Function Overriding:- When derived class defines same name function, as defined in its base class then it is called as function overriding. In this a function in the derived class overrides the inherited function. Example : #include #include class Base { public: void Display() { cout

Description : How do we invoke a constructor function?

Last Answer : A constructor is invoked automatically when an object of its class is created. Example: class ABC { public: ABC( ) { } }; void main( ) { ABC obj; } In the above example, creating „obj‟ object automatically invokes constructor ‟ABC ( )‟.

Description : List characteristics of static data member and static member function.

Last Answer : Characteristics of static member variable are: i) It is initialized to zero when the first object of its class is created. No other initialization is permitted. ii) Only one copy of that ... the class name with a scope resolution operator instead of object name as follows: class_name::function_name;

Description : Differentiate between function definition inside and outside the class

Last Answer : Sr. No Inside function definition Outside function definition  1 A member function of a class is defined inside the class. A member function of a class is declared inside class and as defined outside the ... }; void item :: getdata(int a, float b) { number = a; cost = b; }

Description : Write a program to demonstrate the use of pure virtual function.

Last Answer : Consider the following example where parent class provides an interface to the base class to implement a function called getArea() as pure virtual function: #include // Base class class Shape { protected: int ... ; Rect.setWidth(5); Rect.setHeight(7); // Print the area of the object. cout

Description : Explain static member function.

Last Answer : Static member function: - a static member function can have access to only other static variables or functions declared in the same class. It can be called using the class name instead of its object. It can be declared ... ; } static void showcount()----------------static member function { cout

Description : Explain friend function. Give example.

Last Answer : Friend function: The private members of a class cannot be accessed from outside the class but in some situations two classes may need access of each other s private data. So a common function can be declared which can be made friend of ... xyz x1; a1.get1(); x1.get1(); add(a1,x1); }

Description : Explain the concept of virtual function with example.

Last Answer : Virtual Function: A virtual function is a member function that is declared within a base class and redefined by its derived class. When base class and its derived class both contain same name member function then ... pointer. Example: #include class Base { public: virtual void show( ) { cout

Description : How to define a member function outside the body of class? 

Last Answer : The user can declare member function outside the class with the help of scope resolution operator (::). The label class_name:: tells the compiler that the function_name belongs to the class class_name. ... In above example accept() member function of class student is defined outside of the class. 

Description : Write a program in C++ to overload a „volume‟ function to calculate volume of cube and rectangular box. 

Last Answer : #include<iostream.h> #include<conio.h> void volume(float); void volume(float, float, float); void main() { float a, length, width, height; clrscr(); cout<<"\n ... width*height; cout<<"\n Volume of a rectangular box is:"<<v; }

Description : State any four characteristics of friend function.

Last Answer : Characteristics of friend function: 1. It is not in the scope of the class to which it has been declared as friend. 2. As it is not in the scope of the class, it cannot be called using the ... public or the private part of a class without affecting its meaning. 6. It has the objects as arguments.

Description : Describe syntax and use of defining member function outside class. Give one example.

Last Answer : Member function that is declared inside a class has to be defined separately outside the class. These member functions associate a membership identify label in the header. This label tells the ... ) A member function can call another member function directly, without using the dot operator. 

Description : With example, describe use of static member function.

Last Answer : A static member function can have access to only other static members (functions or variables) declared in the same class. A static member function can be called using the class name as follows: class_name:: ... static void showcount()----------------//static member function { cout

Description : Write a program which concate and reverse string by using pointer to string.

Last Answer : #include #include void main()  { char str1[20], str2[20], rev[20],*p1,*p2; int cnt=0; clrscr(); coutstr2;  p1=&str1[0];  p2=&str2[0]; while(*p1!='\0')  { p1++;  } while(*p2!='\0')  { *p1=*p2; p1++; p2++;  }  *p1='\0'; cout

Description : Write a program which perform arithmetic operation using pointer.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int num[5]={56,75,22,18,90}; int ptr; int i; cout<< array elements are:: ; for(i=0;i<5;i++) ptr=num; cout<< ... 3; cout<< value of ptr+=3:: <<*ptr; cout<< \n ; getch(); }

Description : Write a program to copy content of one string to another string using pointer to string.

Last Answer : #include<iostream.h> #include<conio.h> void main() { char str1[10],str2[10],*p1,*p2; clrscr(); cout<<"\n Enter a String"; cin>>str1; p1=&str1[0]; p2= ... } *p2='\0'; cout<<"Copied String is "<<str2; getch(); }

Description : Write a program to swap two integer values by using call by reference.

Last Answer : #include #include void swap(int *a,int *b) { int c; c=*a; *a=*b; *b=c; } void main() { int a,b; couta; coutb; cout

Description : Write a program to find length of a string using pointer to string.

Last Answer : #include<iostream.h> #include<conio.h> void main() { char str1[10],*ptr; int len=0; cout<<"enter string:"; cin>>str1; ptr=&str1[0]; while(*ptr!='\0 ... +; } cout<<"\nThe Length of a string is"<<len; getch(); }

Description : Write a program to search a number from an array using pointer to array.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int a[5],i,*a1,no,flag=1; clrscr(); a1=&a[0]; cout<<"\nEnter array elements :"<<endl; for(i=0;i<5 ... =0) { cout<<"\n\t Number is not present.... "; } getch(); }

Description : Implement a program to declare a class city with data members city name and state. Accept and display data for 1 object using pointer to object.

Last Answer : #include<iostream.h> #include<conio.h> class city { char city_name[20],state[20]; public: void accept() { cout<<"\nEnter city data:"; cout<<"\nName:"; ... ); ptr=&c; ptr->accept(); ptr->display(); getch(); }

Description : Write a program using concept of pointers to string for performing following operations: (i) String concatenation (ii) String comparisons

Last Answer : (i) Program to implement String Concatenation: #include #include void main() { char s1[50],s2[30],*p,*q; clrscr(); couts1>>s2; p=s1; q=s2; while(*p!=NULL) { p++; } while(*q!=NULL) { *p=*q; p++; q++; } *p='\0'; cout

Description : Explain searching elements in array using pointers.

Last Answer : Consider an array of five elements as shown below: A[5]={ 10,20,30,40,50}; Search element(SE)=30 Pointer variable is declare as *ptr; Before starting search process ... If search element is not present in an array, then after comparing all elements stop the search process.

Description : Write a program in C++ to accept a string from a user and display its reverse using pointer.

Last Answer : #include<iostream.h> #include<conio.h> #include<string.h> void main() { char str[20],*ptr; int l; clrscr(); cout<<"\n Enter a string : "; cin>>str; l=strlen(str); ... (l!=0) { ptr--; cout<<*ptr; l--; } getch(); }

Description : Write a program in C++ to search an element from an array using pointer.

Last Answer : #include<iostream.h> #include<conio.h> void main() { int a[10], n, i,*p, flag=0, x; clrscr(); cout<<"Enter no. of array elements \n"; cin>>n; cout<< ... } if(flag==0) cout<<x<<"is not found \n"; getch(); }