Deck 4: Parameters and Overloading
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Question
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/31
Play
Full screen (f)
Deck 4: Parameters and Overloading
1
The fundamental rule for testing functions requires that every function be tested in an already fully tested and debugged program.How can this be accomplished? Give a testing scenario for testing a program with main function that calls several functions.
The main function is tested with stub functions replacing each` function.Stubs should be small enough that their correctness is assured.Stubs return enough information to let the main function compile and run,so that it can be tested.Then test the main function.Having done this,each function in turn can be tested.
An alternative is to test the functions with driver functions.Drivers are functions that call the function to be tested.Drivers,like stubs,should be simple enough that their correctness is assured.Then the function is called and tested.Clearly the assumptions of 'small enough' that 'correctness is assured' are really questionable.However,this procedure is better than writing a 200 line program or worse a 2000 line program and then testing it.
An alternative is to test the functions with driver functions.Drivers are functions that call the function to be tested.Drivers,like stubs,should be simple enough that their correctness is assured.Then the function is called and tested.Clearly the assumptions of 'small enough' that 'correctness is assured' are really questionable.However,this procedure is better than writing a 200 line program or worse a 2000 line program and then testing it.
2
A call-by-value parameter may pass data only into a function.
True
3
Write a void function using two type int call-by-reference parameters that swaps the values in the arguments.Be sure to include testable pre and post conditions.
//precondition: arguments arg1,arg2 are initialized
//postcondition: arg1post == arg2pre && arg2post == arg1pre
void swap( int & lhs,int & rhs)
{
int temp = lhs;
lhs = rhs;
rhs = tmp;
}
//postcondition: arg1post == arg2pre && arg2post == arg1pre
void swap( int & lhs,int & rhs)
{
int temp = lhs;
lhs = rhs;
rhs = tmp;
}
4
Functions that call other functions can be tested independently of the called functions.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
5
There is only one kind of parameter passing in C++,namely call-by-value.
Explain.
Explain.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
6
Mixing call-by-reference and call-by-value parameters is strictly prohibited.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
7
Carefully describe the call-by-value mechanism.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
8
Call-by-reference is restricted to void functions.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
9
A call-by-reference parameter may pass data only out of a function.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
10
Suppose a programmer supplies the ampersand for call-by-reference in a prototype,but forgets to put the ampersand in the definition.The compiler will nevertheless correctly interpret the programmer's intent and compile the function.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
11
Write a definition for a void-function that has two int value parameters and outputs to the screen the product of these arguments.Write a main function that asks the user for these two numbers,reads them in,calls your function,then terminates.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
12
Names of parameters in functions,especially function prototypes,have no meaning,and may be selected quite arbitrarily.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
13
There is no problem with the compiler distinguishing these two function definitions:
void func(double x){/*…*/}
int func(double x){/*…*/ return something_double;}
void func(double x){/*…*/}
int func(double x){/*…*/ return something_double;}
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
14
A program should have all of its functions written before testing can begin.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
15
The call-by-reference mechanism is specified in the function declaration and definition,using a $ between the type and the parameter.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
16
The position of the ampersand in the function header is of no importance to the proper compiling and execution of code that uses call-by-reference parameters.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
17
Default arguments can be used with either call-by-value or call-by-reference parameters.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
18
What does the function given here do when called if both the ampersands (&)are removed? Why?
void func(int& x,int& y)
{
int t = x;
x = y;
y = t;
}
void func(int& x,int& y)
{
int t = x;
x = y;
y = t;
}
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
19
There is no problem with these two function definitions:
void func(int x){/*…*/}
int func(double x){/*…*/ return something_double;}
void func(int x){/*…*/}
int func(double x){/*…*/ return something_double;}
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
20
The compiler ha no problem distinguishing these two function definitions:
void func(double &x){/*…*/}
void func(double x){/*…*/}
void func(double &x){/*…*/}
void func(double x){/*…*/}
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
21
Write a stub for the function whose declaration is given below.Do not write a program that calls this,just write the stub.
Hint: This is very short.
double yield (double pressure,
double density,double temp);
// Precondition: pressure is newtons per square meter
// density is in kilograms per cubic meter
// temp is in degrees Celcius
// Postcondition: Return value is the relative yield of
// a chemical process.It is a number between 0 and 1.
// 0 means no output and 1 means ideal yield.
Hint: This is very short.
double yield (double pressure,
double density,double temp);
// Precondition: pressure is newtons per square meter
// density is in kilograms per cubic meter
// temp is in degrees Celcius
// Postcondition: Return value is the relative yield of
// a chemical process.It is a number between 0 and 1.
// 0 means no output and 1 means ideal yield.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
22
Which of the following overloadings will be invoked by this call? g(1,2);
A)int g(int count,double value);
B)void g(double value,int count);
C)void g(int value,int count);
D)Neither,the compiler cannot decide which of these to use.
A)int g(int count,double value);
B)void g(double value,int count);
C)void g(int value,int count);
D)Neither,the compiler cannot decide which of these to use.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
23
Which of the following function declarations with default arguments are correct?
A)void g(int length,int width,int height = 1);
B)void g(int length=1,int width,int height);
C)void g(int length,int width=1,int height = 1);
D)void g(int length=1,int width=1,int height);
A)void g(int length,int width,int height = 1);
B)void g(int length=1,int width,int height);
C)void g(int length,int width=1,int height = 1);
D)void g(int length=1,int width=1,int height);
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
24
Given the function,and the main function calling it: What is the output of the following code if you omit the ampersand (&)from the first parameter,but not from the second parameter? (You are to assume this code is embedded in a correct function that calls it. ): #include
Using namespace std;
Void func(int & x,int & y)
{
Int t = x;
X = y;
Y = t;
}
Int main()
{
Int u = 3;v = 4;
// )..
Cout << u << " " << v << endl;
Func ( u,v )
Cout << u << " " << v << endl;
// )..
}
A)3 4 3 3
B)3 4 4 3
C)3 4 3 4
D)3 4 4 4
E)none of the above.If you choose this,you must specify the output.
Using namespace std;
Void func(int & x,int & y)
{
Int t = x;
X = y;
Y = t;
}
Int main()
{
Int u = 3;v = 4;
// )..
Cout << u << " " << v << endl;
Func ( u,v )
Cout << u << " " << v << endl;
// )..
}
A)3 4 3 3
B)3 4 4 3
C)3 4 3 4
D)3 4 4 4
E)none of the above.If you choose this,you must specify the output.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
25
Write a stub for the following function prototype:
double root( double a,double b,double c,int i);
// Precondition: a != 0 and a,b,c are coefficients of
// a quadratic equation a*x*x + b*x + c = 0 The value
// of i is either +1 or -1 to choose which root.
// Postcondition: return value,x,satisfies the
// equation a*x*x + b*x + c = 0
double root( double a,double b,double c,int i);
// Precondition: a != 0 and a,b,c are coefficients of
// a quadratic equation a*x*x + b*x + c = 0 The value
// of i is either +1 or -1 to choose which root.
// Postcondition: return value,x,satisfies the
// equation a*x*x + b*x + c = 0
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
26
What is a driver program?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
27
Write a void function definition for a function called zeroBoth that has two call-by-reference parameters,both of which are variables of type int,and that sets the values of both variables to 0.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
28
Which of the following overloadings will be invoked by this call? g(1.0,2.0);
A)int g(int count,double value);
B)void g(double value,int count);
C)void g(int value,int count);
D)Neither,the compiler cannot decide which of these to use.
A)int g(int count,double value);
B)void g(double value,int count);
C)void g(int value,int count);
D)Neither,the compiler cannot decide which of these to use.
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
29
What is a stub?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
30
Define function signature:
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck
31
Consider the revised pizza buying program of Display 4.7.This program provides the following overloading for unitPrice functions for round and rectangular pizza:
double unitPrice(int diameter,double price);
double unitPrice(int length,int width,double price);
Suppose we are faced with the need for the unit price on a square pizza.The problem here is to devise in a 'natural' way to overload unitPrice to compute the price per square inch of a square as well as a round pizza?
double unitPrice(int diameter,double price);
double unitPrice(int length,int width,double price);
Suppose we are faced with the need for the unit price on a square pizza.The problem here is to devise in a 'natural' way to overload unitPrice to compute the price per square inch of a square as well as a round pizza?
Unlock Deck
Unlock for access to all 31 flashcards in this deck.
Unlock Deck
k this deck