Question

Study the c++ code below and answer the question on templates after // A polymorphic swap...

Study the c++ code below and answer the question on templates after

// A polymorphic swap function for any type of object (an example)

template<typename T>

void MySwap( T& x, T& y) {
T temp;
temp = x;
x = y;
y = temp;
}
// A polymorphic class holding an [x,y] position of any type
template<class T>
class Position {
public:
Position(T x=0, T y=0) : x_(x), y_(y) {}
friend std::ostream& operator<<(std::ostream& os, const Position& p) {
return os << "[" << p.x_ << ", " << p.y_ << "]";
}
// >>> ADD an overloaded operator to add two positions.
T get_x() const { return x_; }
void set_x(T x) { x_ = x; }
T get_y() const { return y_; }
void set_y(T y) { y_ = y; }
private:
T x_;
T y_;
};
// You still can't mix types with the above template, but ...
template<class T1, class T2>
Position<T1> operator-(Position<T1> p1, Position<T2> p2) {
return Position<T1>(p1.get_x()-p2.get_x(), p1.get_y()-p2.get_y());

}

1,1 In the above file, the template function void MySwap(T& x, T&y) is defined. If in your code, you included this template and had the lines below, how many versions of MySwap will the compiler link in to your program?

int x = 5;
int y = 2
int* px = &x;
int* py = &y;
string s1="this":
string s1="that";

MySwap(x, y);
MySwap(px, py);
MySwap(s1, s2);

Answer choices: 1, 2, 3, 4, depends on compiler


0 0
Add a comment Improve this question Transcribed image text
Answer #1

since\;we\;are\;calling\;MySwap\;with\;3\;different\;types\;

compiler\;links\;3\;versions\;of\;MySwap\;in\;the\;program

Answer:\;\;3

Add a comment
Know the answer?
Add Answer to:
Study the c++ code below and answer the question on templates after // A polymorphic swap...
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
  • Hi, for the below output the run is: x : 1 | y : 1 x...

    Hi, for the below output the run is: x : 1 | y : 1 x : 0 | y : 2 x : 0 | y : 0 x : 2 | y : 1 x : 1 | y : 8 Also compare your output output should not be wrong, run on "Xcode" to see the about output. Thanks int x = 1, y = -1; void swapplus1(int n1, int n2) {          int temp = n1 +...

  • /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) {...

    /* FILE: ./shapes7/shape.h */ #include <iostream> using std::ostream; class shape{ int x,y; public: shape( ) { x=y=0;} shape(int xvalue, int yvalue); void setShape(int new_x, int new_y); void setX(int new_x); void setY(int new_y); int getX( ) const; int getY( ) const; virtual void move(int x, int y) = 0; virtual void shift(int dx, int dy) = 0; virtual void draw( ) = 0; virtual void rotate(double r) = 0; virtual void print(ostream&)const; friend ostream & operator<<(ostream & os, const shape& s);...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • I need help with the code below. It is a C program, NOT C++. It can...

    I need help with the code below. It is a C program, NOT C++. It can only include '.h' libraries. I believe the program is in C++, but it must be a C program. Please help. // // main.c // float_stack_class_c_9_29 // // /* Given the API for a (fixed size), floating point stack class, write the code to create a stack class (in C). */ #include #include #include #include header file to read and print the output on console...

  • Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class...

    Please answer all the questions thank you 1) (Classes – 20 Points) Consider the following class declaration for Time to complete the questions below: DO NOT WRITE MORE THAN ASKED FOR. class Time private: int hours; int minutes; public: Time(); Time (int , int m = 0); void addMin(int m); void addHr(int h); void reset(int h = 0, int m = 0); Time operator+(const Time & t) const; Time operator-(const Time & t) const; Time operator*(double n) const; friend Time...

  • Question 2 (8 pts): Will this C++ program compile, and if not why? 001 nclude estring>...

    Question 2 (8 pts): Will this C++ program compile, and if not why? 001 nclude estring> 002 include ccmath> 003 004 uning namespace std 005 006 007 templatesclass T> 008 T operator+ (const Telassa% t1, const Telassct 009 010 templatecclass T> 011 class Tolass( t2); 012 friend T operatort (const TelasseTs t1, const Telasscs t2) 014 Telass (const T& value) :m_value (value)t 015 T NumericValue) constt 016 017 018 private: 019 T value: 020 } ; 021 02 template<class T>...

  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • USING C++: Referring to the header file below named coord2d.h, Implement each of the 8 operator...

    USING C++: Referring to the header file below named coord2d.h, Implement each of the 8 operator overloads (operators: <<, [], >, <, two versions of +, two versions of *) #ifndef COORD2D_H #define COORD2D_H #include <iostream> using namespace std; //implement a class that keeps track of the coordinates of a point in the X-Y plane class coord2d { //overload the << operator so that if p is of type "coord2d" then "cout<<p"; //will print out (x_coord, y_coord) //e.g. if p.x_coord=3.4,...

  • I need help solving this question from the practice final exam given to us to prepare...

    I need help solving this question from the practice final exam given to us to prepare for the final in C++ #include <iostream> using namespace std; /* The following is code for an OrderedCollection container and its related iterator. The container has a capacity determined by a constructor parameter. The container does not grow. Code that adds elements to the container ensures that the capacity of the container is never exceeded. An attempt to add an item to a full...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

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
ADVERTISEMENT