Question

MUST USE C++ PLEASE READ THE QUESTION CAREFULLY!! ADD COMMENTS/EXPLAIN Question 1: Template Function with Pointer...

MUST USE C++

PLEASE READ THE QUESTION CAREFULLY!!

ADD COMMENTS/EXPLAIN

Question 1: Template Function with Pointer Arguments

Design and implement a template function, called swapt(T *p1, T *p2), which takes two parameters with the same generic data type. The function swaps the values of these two parameters.

Test this function in the following main( ) :

int main( )

{

            int x = 30,   y = 40;

            swapt(&x, &y);

            cout << “x = “ << x << “ y= “ << y << endl;

            float w = 3.7, v = 1.2;

            swapt(&w, &v);

            cout << “w = “ << w << “ v= “ << v << endl;

string a = ‘good’,   b= ‘morning’;

            swapt(&a, &b);

            cout << “a = “ << a << “ b= “ << b << endl;

            return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <string>

using namespace std;

template<class T>
void swapt(T *n1, T *n2) {
    T temp = *n1; // store n1 in temp
    *n1 = *n2; // store n2 in n1
    *n2 = temp; // finally store temp in n2 to complete the swap
}

int main() {
    int x = 30, y = 40;
    swapt(&x, &y);
    cout << "x = " << x << " y= " << y << endl;
    float w = 3.7, v = 1.2;
    swapt(&w, &v);
    cout << "w = " << w << " v= " << v << endl;
    string a = "good", b = "morning";
    swapt(&a, &b);
    cout << "a = " << a << " b= " << b << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
MUST USE C++ PLEASE READ THE QUESTION CAREFULLY!! ADD COMMENTS/EXPLAIN Question 1: Template Function with Pointer...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given...

    MUST USE C++ PLEASE READ THE QUESTION CAREFULLY ADD COMMENTS AND EXPLAIN THE CODES PLEASE. Given the following skeleton of an unsorted list class that uses an unsorted linked list: template < class ItemType > struct NodeType {                 ItemType item;                 NodeType* next; }; template < class ItemType > class UList { public:                 UList(); // default constrctor                 UList(const UList &x); // we implement copy constructor with deep copy                 UList& operator = (UList &x); // equal sign...

  • please provide full answer with comments this is just begining course of c++ so don't use...

    please provide full answer with comments this is just begining course of c++ so don't use advanced tequenicks I'll put main.cpp, polynomial.h, polynomial.cpp and Cimg.h at the bottom of pictures. If you help me with this will be greatly thankful thank you main.cpp #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include "polynomial.h" #include <iostream> using std::cout; using std::endl; int main() { pic10a::polynomial p1; p1.setCoeff(0, 1.2); p1.setCoeff(3, 2.2); p1.setCoeff(7, -9.0); p1.setCoeff(7, 0.0); //degree of polynomial is now 3 cout << p1 <<...

  • C++ only : Use an integer pointer returning function which would determine the sum of square...

    C++ only : Use an integer pointer returning function which would determine the sum of square of each of two integer pointer variables used as argument of the function. Call the function in the main() using pointer variables and dynamic allocation Question 4: Print both the strings using string double-pointer only in the main() void print(string ** s){ **s = "University of Oregon"; cout<< **s <<endl; } void print(string *x){ *x = "Portland"; cout<<*x<<endl; }

  • 81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5...

    81. The following function call doesn’t agree with its prototype:              cout << BoxVolume( 10, 5 );                    int BoxVolume(int length = {1}, int width = {1}, int height = {1});                                                     T__   F__                                                                     82. The following function is implemented to swap in memory the          argument-values passed to it:         void swap(int a, int b)                   {           int temp;             temp = a;             a = b;             b = temp;        ...

  • //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm....

    //CODE 16-02.cpp //Demonstrates a template function that implements //a generic version of the selection sort algorithm. #include <iostream> using std::cout; using std::endl; template<class T> void sort(T a[], int numberUsed); //Precondition: numberUsed <= declared size of the array a. //The array elements a[0] through a[numberUsed - 1] have values. //The assignment and < operator work for values of type T. //Postcondition: The values of a[0] through a[numberUsed - 1] have //been rearranged so that a[0] <= a[1] <=... <= a[numberUsed -...

  • Hi, please the code should be good es. Onless you Thera Problem3: Write a template class Box with Three class param...

    Hi, please the code should be good es. Onless you Thera Problem3: Write a template class Box with Three class parameters representing the three dimensions (length, width and height), of the Box. Include public methods to display and set the data values as well as a function that swaps the values so that, after the swap, the first element is cast into the second and the second is cast into the Third and the third is cast first. overlaod <<...

  • c++ 1) Write a header file that contains the prototype for a function that takes two...

    c++ 1) Write a header file that contains the prototype for a function that takes two value parameters (both floats) and returns a single character output. The function name is “charIn”. We do not care what the function does at this point. (8) 2)   Fix the Errors of the C++ program: a.   #include <cmath> b.   Void main(){cout<< “Math test”<<endl;//start math test c.   Int i=10 int j=3 k=i%3 cout << “test of mod<<k<<endl d.   Float d=0 float f=e/d cout << “div...

  • Please use C++ and add comments to make it easier to read. Do the following: 1)...

    Please use C++ and add comments to make it easier to read. Do the following: 1) Add a constructor with two parameters, one for the numerator, one for the denominator. If the parameter for the denominator is 0, set the denominator to 1 2) Add a function that overloads the < operator 3) Add a function that overloads the * operator 4) Modify the operator<< function so that if the numerator is equal to the denominator it just prints 1...

  • C++ problem a 6. 9. Read the following program carefully and answer questions. Rinclude <iostream using...

    C++ problem a 6. 9. Read the following program carefully and answer questions. Rinclude <iostream using namespace std; class Grand Parent public Grand Parent cout "Grandparent constructor! <<endl. class Father public GrandParent public: Father Grandparent cout "Father Constructor Kendl. void cout In Father functiob <<endl y virtual void cout In Father, y function <<endl i class Mother public Grand Parent public Motber() Grandparent() cout Mother Constructor <<endl void cout "In Nother, x function <<endli yo void y() cout <<"In Mother,...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT