Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Function templates -------------------------------------------------------------

ID: 3830147 • Letter: F

Question

Function templates

-----------------------------------------------------------------------------

16.1. Which file should this code go into?

functionName<int>(argumentList);

  

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<char> initial;

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() << " Year: " << rhs.getYear() << " Model: " << 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 <iostream>

#include <stack>

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

Explanation / Answer

16.1 -B).cpp(implementation)

16.2 -
template <class T>
T getSum(T t1, T t2){
T sum;
sum = t1 + t2;
return sum;
}


16.3 -Function Overloading is when multiple function with same name exist in a class. Function Overriding is when function have same prototype in base class as well as derived class.

16.4 -equivalent to the declaration inside the class

16.5 -
template <calss T>
public:
Math(T m, T n);
T first() const;
T second() const;
private:
T first;
T second;
};

16.6 -c)teamplate argument

16.8 - comparision operator(==)
addition (+)
increment operator(++)
16.9 -1) Only built-in operators can be overloaded. New operators can not be created.
2) Arity of the operators cannot be changed.
3) Precedence and associativity of the operators cannot be changed.
4) Overloaded operators cannot have default arguments except the function call operator () which can have default arguments.

16.11 - The 'this' pointer is passed as a hidden argument to all nonstatic member function calls and is available as a local variable within the body

16.12 -The way to look at this would be to understand what would happen if this was actually legal. When a class is instantiated, all its class members are constructed first. So, in your example, the construction of A a will take place before A is fully constructed, which is not possible.

Also, this will result in an infinite recursive construction of objects of A.

17.1 -for stack it's puch and pop

17.2 -It delets the element(LIFO)

17.3 - only the element 20 will remain

17.4 -yes it uses pass by reference as arrays

17.5 - LAST in FIRST Out

17.8 -push ,pop

17.9 -false

17.10 -FIRST in FIRST out

17.11 -it depends on the priority of the element

17.12 -in pripority queue with the elements priority also inserted

17.19 -d)map