Polymorphism: book back answers
14 solved questions from the end of this chapter, Class 11 Computer Science, Tamil Nadu State Board. Each answer is checked against the textbook, and you can open the page that proves it.
← Notes and quiz for PolymorphismSECTION – A Choose the correct answer
Which of the following refers to a function having more than one distinct meaning?
- (A) Function Overloading
- (B) Member overloading
- (C) Operator overloading
- (D) Operations overloading
Try it, then show the answerAnswer
(A) Function Overloading
An overloaded function has more than one distinct meaning.
From the book: “an ‘overloaded function’ refers to a function having more than one distinct meaning”
Which of the following reduces the number of comparisons in a program ?
- (A) Operator overloading
- (B) Operations overloading
- (C) Function Overloading
- (D) Member overloading
Try it, then show the answerAnswer
(C) Function Overloading
The chapter notes that function overloading reduces the number of comparisons in a program.
From the book: “Function overloading is not only implementing polymorphism but also reduces the number of comparisons in a program”
void dispchar(char ch=’$’,int size=10) for(int i=1;i<=size;i++) cout<<ch; How will you invoke the function dispchar() for the following input? To print $ for 10 times
- (A) dispchar();
- (B) dispchar(ch,size);
- (C) dispchar($,10);
- (D)dispchar(‘$’,10 times);
Try it, then show the answerAnswer
(A) dispchar();
Both parameters have default values, so calling the function without arguments uses '$' and 10.
From the book: “The default arguments of overloaded functions are not considered as part of the parameter list in function overloading.”
Which of the following is not true with respect to function overloading?
- (A) The overloaded functions must differ in their signature.
- (B) The return type is also considered for overloading a function.
- (C) The default arguments of overloaded functions are not considered for Overloading.
- (D) Destructor function cannot be overloaded.
Try it, then show the answerAnswer
(B) The return type is also considered for overloading a function.
Return type is not considered when deciding whether functions are overloaded; they must differ in argument number or types.
From the book: “The return type of overloaded functions are not considered for overloading same data type”
Which of the following is invalid prototype for function overloading (A) void fun (intx); void fun (char ch) ; (B) void fun (intx); void fun (inty); (C) void fun (double d); void fun (char ch); (D) void fun (double d); void fun (inty);
- (A) void fun (intx); void fun (char ch) ;
- (B) void fun (intx); void fun (inty);
- (C) void fun (double d); void fun (char ch);
- (D) void fun (double d); void fun (inty);
Try it, then show the answerAnswer
(B) void fun (intx); void fun (inty);
These prototypes have the same name and parameter type, so their signatures do not differ.
From the book: “The overloaded function must differ in the number of its arguments or data types”
SECTION-B
What is function overloading?
Try it, then show the answerAnswer
Function overloading is when two or more functions in the same scope share the same name but have different parameters.
The shared function name can process data in more than one form because the parameters differ.
From the book: “two or more functions in the same scope share the same name but their parameters are different”
List the operators that cannot be overloaded.
Try it, then show the answerAnswer
Scope resolution operator (::), sizeof, member selector (.), member pointer selector (✳), and ternary operator (?:).
These are the operators listed in the chapter as not overloadable.
From the book: “Scope operator (::) • Sizeof • Member selector (.) • Member pointer selector (✳) • Ternary operator (?:)”
class add{int x; public: add(int)}; Write an outline definition for the constructor.
Try it, then show the answerAnswer
add::add(int a) { x = a; }
A constructor definition uses the class name and accepts the integer argument, which can initialize x.
From the book: “A class can have more than one constructor with different signature.”
Does the return type of a function help in overloading a function?
Try it, then show the answerAnswer
No. The return type does not help in overloading a function.
Overloaded functions must differ in their argument number or types; return type is not considered.
From the book: “The return type of overloaded functions are not considered for overloading same data type”
What is the use of overloading a function?
Try it, then show the answerAnswer
It lets functions with the same name process different forms of data, and reduces the number of function names a programmer must remember.
The suitable function definition is selected based on the arguments used in the call.
From the book: “It also helps the programmer by reducing the number of function names to be remembered.”
SECTION-C Short Answers
How does a compiler decide as to which function should be invoked when there are many functions? Give an example.
Try it, then show the answerAnswer
The compiler compares the types of the supplied arguments with the parameter types in the overloaded definitions and selects the most appropriate one. For example, print(10) selects print(int), while print(10.10) selects print(double).
This selection process is called overload resolution.
From the book: “the compiler determines the most appropriate definition to use, by comparing the argument types you have used to call the function”
What is operator overloading? Give some examples of operators which can be overloaded.
Try it, then show the answerAnswer
Operator overloading gives an existing C++ operator additional, user-defined meaning. Examples include +, ++, -, +=, -=, *, < and >.
For example, the + operator can be given a meaning for adding complex numbers or concatenating strings.
From the book: “giving additional functionality to the normal C++ operators like +,++,−,—,+=,−=,*.<,>”
Discuss the benefits of constructor overloading ?
Try it, then show the answerAnswer
Constructor overloading provides flexibility in creating multiple types of objects for a class.
Different constructor signatures allow objects to be created with different sets of arguments.
From the book: “Constructor overloading provides flexibility of creating multiple type of objects for a class.”
class sale ( int cost, discount ;public: sale(sale &); Write a non inline definition for constructor specified;
Try it, then show the answerAnswer
sale::sale(sale &s) { cost = s.cost; discount = s.discount; }
This is a non-inline copy-constructor definition that copies the two data members from the supplied sale object.
From the book: “add (add &a) { cout<<"\n Copy Constructor ... "; num1= a.num1; num2=a.num2;”