Question

In: Computer Science

Give an example of a program that uses the nongeneric version of a class from the...

Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?

Solutions

Expert Solution

STL Without GENERICS:

#include <iostream>

#include <stack>

using namespace std;

void displayStack(stack <int> s) {

  while (!s.empty())

  {

    cout << " " << s.top();

    s.pop();

  }

  cout << endl;

}

int main ()

{

  stack <int> s;

  s.push(10);

  s.push(30);

  s.push(20);

  s.push(5);

  s.push(1);

  cout << "The stack is : ";

  displayStack(s);

  cout << "\ns.size() : " << s.size();

  cout << "\ns.top() : " << s.top();


  cout << "\ns.pop() : ";

  s.pop();

  displayStack(s);

  return 0;

}


STL With GENERICS

#include <iostream>

#include <stack>

using namespace std;

template <class T>

void displayStack(stack <T> s) {

  while (!s.empty())

  {

    cout << " " << s.top();

    s.pop();

  }

  cout << endl;

}

void intStack() {

  stack<int> s;

s.push(10);

  s.push(30);

  s.push(20);

  s.push(5);

  s.push(1);

  cout << "The stack is : ";

  displayStack(s);

  cout << "\ns.size() : " << s.size();

  cout << "\ns.top() : " << s.top();


  cout << "\ns.pop() : ";

  s.pop();

  displayStack(s);

}

void charStack() {

  stack<char> s;

s.push('A');

  s.push('B');

  s.push('C');

  s.push('D');

  s.push('E');

  cout << "The stack is : ";

  displayStack(s);

  cout << "\ns.size() : " << s.size();

  cout << "\ns.top() : " << s.top();


  cout << "\ns.pop() : ";

  s.pop();

  displayStack(s);

}

int main ()

{

  

intStack();

charStack();

  return 0;

}

You can clearly see from the code that the same displayStack function could be used to display the content of both int and char stack. Without generics, we would have to write separate methods for each type.


Related Solutions

Give an example of a program that uses the nongeneric version of a class from the...
Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
Temperature Converter Modify the previous version of this program so that it uses a loop to...
Temperature Converter Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit. Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them...
Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
give an example of an application that should not be implemented using n-version Programming. Explain why?
give an example of an application that should not be implemented using n-version Programming. Explain why?
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent. public class Polynomial { // P is a linear array of Terms private Term[ ] P; … // Re-implement the six methods of Polynomial (including the constructor) … }
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT