in c++ please explain with answer
6. Given the following template function definition, which of
the following is not a valid
invocation of the function?
|
template <class T> |
a. |
swap(s1,s2); |
|
void swap(T& left, T& right) |
b. |
swap(int1, int2); |
|
{ |
c. |
swap(ch1, ch2); |
|
//implementation |
d. |
swap(int1, ch2); |
|
} |
||
|
int int1, int2; |
||
|
float flt1, flt2; |
||
|
char ch1, ch2; |
||
|
string s1, s2; |
||
7. Why can you not use the swap template function to swap two complete arrays?
template <class T>
void swap(T& left, T& right)
{
left=right;
right=tmp;
}
8. In the following function template, what must be true in order
to use the function with a
given data type?
|
template <class T> |
a. |
the data type must be a pre-defined data type |
|
int smallest( T array[], int size) |
b. the data type must have a < operator defined for |
|
|
{ |
it |
|
|
int small=0, i; |
c. the data type must be numeric |
|
|
for(i=0;i<size;i++) |
d. the data type must be character based |
|
|
{ |
||
|
if(array[i] < array[small]) |
||
|
small=i; |
||
|
} |
||
|
return small; |
||
|
} |
||
6. The answer is D. Since, you can swap two values of same type as in A, B, C like integer to integer, character to character but in D we are trying to swap values of different types therefore it is incorrect.
7. Answer is C since you cannot directly transfer or swap array elements using =. You must get individual element and then swap.
8. The data type can be of numeric or character but it should be predefined. Therefore, option A is correct.
9. It is E.
10. It is C. It is defined in new header file.
in c++ please explain with answer 6. Given the following template function definition, which of the...
in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...
/* FILE NAME: Class{aSet}.cpp FUNCTION: A template class for a set in C++. It implements all the set operations, except set compliment: For any two sets, S1 and S2 and an element, e A. Operations which result in a new set: (1) S1 + S2 is the union of S1 and S2 (2) S1 - S2 is the set difference of S1 and S2, S1 - S2 (3) S1 * S2 is the set intersection of S1 and S2, S1 * S2 (4) S1 + e (or e +...
//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 -...
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...
IN C++ Create a class to act as a generic array (i.e. the user will be able to choose the data type to be stored by passing the appropriate template argument. Integer template arguments will also be used to set the upper and lower bounds of the array. Provide all necessary functionality to allow the class to act as an array ([] operator, = operator etc.). The array does not need to provide input or output methods to act on...
PLEASE HELP ME WITH THIS HOMEWORK. Create a template function that, given an array of elements of any template type, deletes an element on a given position. Submit in the standard format in Dropbox Hw 4 before the deadline. Example for insertion (needs a fix!!! - 1st e-mail gets extra-credit) #include <iostream> using namespace std; // A template function to implement element insertion on given position in array. template <class T> void insert(T a[], int &n,T el, int place )...
Redesign your Array class from lab6 as a class template to work with the application below. write your overloaded output stream operator as an inline friend method in the class declaration. Include the class template header file in your application as below. #include "Array.h" main() { Array<char> c(3); c.setValue(0,'c'); c.setValue(1,'s'); c.setValue(2,'c'); cout << c; Array<int> i(3); i.setValue(0,1); i.setValue(1,2); i.setValue(2,5); cout << i; Array<int> j(3); j.setValue(0,10); j.setValue(1,20); j.setValue(2,50); cout << j; Array<int> ij; ij = i + j; cout << ij;...
Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T> T data TreeNode<T> left TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...
Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...
Template Deque Class (C++)
In this assignment, we will use a given test menu for the
template Deque Class (a Linked List based Double Ended Queue,
Deque) that you have put together.
Recommended Steps
testDeque.cpp :
// C++ implementation of doubly linked list Deque doubly linked
list
#include <bits/stdc++.h>
using namespace std;
class Timer {
// To replace with the full timer class definition
// inside this folder: LearnCpp9_18_timeSortArray.cpp
};
// Node of a doubly linked list
template<class T>
class...