Question: Function templates ----------------------------------------------------------------------------- 16.1. Which file should this code go into? functionName (argumentList); A) .h (specification) B) .cpp (implementation) C) client code D) None of
Function templates
-----------------------------------------------------------------------------
16.1. Which file should this code go into?
functionName
A) .h (specification)
B) .cpp (implementation)
C) client code
D) None of the above
-----------------------------------------------------------------------------
16.2. Template this function:
int getSum(int t1, int t2){
int sum;
sum = t1 + t2;
return sum;
}
-----------------------------------------------------------------------------
16.3. What is function overloading?
Class templates
-----------------------------------------------------------------------------
16.4. All operations defined outside of a templated class declaration must be _______________.
-----------------------------------------------------------------------------
16.5. Rewrite the following class as a class template so that a client can manipulate an ordered pair of any simple types, not just ordered ints.
class Math{
public:
Math(int m, int n);
int first() const;
int second() const;
private:
int first;
int second;
};
-----------------------------------------------------------------------------
16.6. The char type within the angle "<>" brackets below is referred to as the ________.
MyClass
A) typedef argument
B) function argument
C) template argument
D) None of the above
Overloading operators
-----------------------------------------------------------------------------
16.7. Write a function prototype for an operator function that overloads the greater than (>) operator for a CoolObj class.
-----------------------------------------------------------------------------
16.8. Name at least three C++ operators that can not be overloaded.
-----------------------------------------------------------------------------
16.9. Name at least two of the four rules of overloading operators.
-----------------------------------------------------------------------------
16.10. Fix the following code in order to properly overload the << operator (assume all variables are properly declared) and that this appears outside of any class definitions:
ostream operator<<( ostream os, Vehicle rhs){
os << "Price: " << rhs.getPrice() << "\tYear: " << rhs.getYear() << "\tModel: " << rhs.getModel() << endl;
return os;
}
Hint: what if the following code is called:
Vehicle gasGuzzler;
Vehicle gasSipper;
cout << gasGuzzler << gasSipper;
-----------------------------------------------------------------------------
Keyword: this
-----------------------------------------------------------------------------
16.11. What does the keyword "this" refer to?
-----------------------------------------------------------------------------
16.12. How can we refer to a whole object from within itself?
-----------------------------------------------------------------------------
Chapter 17.1-17.4 - Introduction to Data Structures Using the Standard Template Library
=============================================================================
Abstract Data Structures Versus Implementations
-----------------------------------------------------------------------------
17.1 Every data structure has two aspects. What are they?
Stacks
-----------------------------------------------------------------------------
17.2. What will the command s.pop() do for a STL stack s?
-----------------------------------------------------------------------------
17.3. What is left (if anything) in the stack myStack after following statements are executed?
while( ! myStack.empty() ){
myStack.pop();
}
myStack.push(20);
myStack.push(34);
myStack.push(8);
myStack.pop();
myStack.push(119);
myStack.pop();
myStack.pop();
-----------------------------------------------------------------------------
17.4. Is an STL stack automatically passed by reference, like an array? Explain.
-----------------------------------------------------------------------------
17.5. A stack makes its data available in a ________ In ________ Out manner.
-----------------------------------------------------------------------------
17.6. Declare a stack object of integers called intStack using the Standard Template Library.
-----------------------------------------------------------------------------
17.7. What is the output for the following code?
stack< char > s;
s.push('s');
s.push(' ');
s.push('m');
s.push(' ');
s.push('I');
while( ! s.empty){
cout << s.top();
s.pop();
}
s.push('r');
s.push('a');
s.push('m');
while( ! s.empty){
cout << s.top();
s.pop();
}
s.push('!');
s.push('t');
while(! s.empty){
cout << s.top();
s.pop();
}
-----------------------------------------------------------------------------
17.8. What are the two basic operations on a stack?
-----------------------------------------------------------------------------
17.9. The first item pushed on the stack is the first item out.
A) True
B) False
-----------------------------------------------------------------------------
Queues
-----------------------------------------------------------------------------
17.10. A queue makes its data available in a ________ In ________ Out manner.
-----------------------------------------------------------------------------
Priority Queues
-----------------------------------------------------------------------------
17.11. How are items removed from a priority queue?
-----------------------------------------------------------------------------
17.12. How is a priority queue different from a queue?
-----------------------------------------------------------------------------
The STL
-----------------------------------------------------------------------------
17.13. What is the difference between the return values of vector's capacity() and size() methods?
-----------------------------------------------------------------------------
17.14. What is the proper syntax for declaring a vector of vectors that stores floats?
-----------------------------------------------------------------------------
17.15. What's the result of compiling the executing the following code?
#include
#include
using namespace std;
int main(){
stack myStack;
for( int i = 0; i < 10; i++)
myStack.push(char(i+'0'));
cout << myStack.top();
myStack.pop();
return 0;
}
A) 0 1 2 3 4 5 6 7 8 9
B) 123456789
C) 0123456789
D) 987654321
E) 9
F) The compiler reports an error message (stack needs to be templated)
-----------------------------------------------------------------------------
17.16. Instantiate a vector of doubles named finalGrades.
-----------------------------------------------------------------------------
17.17. Name at least 4 of the 5 operations that can be performed on a STL stack and tell what each one does.
-----------------------------------------------------------------------------
17.18. When an STL vector reaches full capacity, it increases its size by __________.
-----------------------------------------------------------------------------
17.19. Which of the following is NOT a container?
a) deque
b) array
c) set
d) map
e) struct
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
