Question

Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file)

Template Classes – Chapter 12

  1. Write an example of a template function that can swap 2 generic elements.
  2. Create a C++ class vector using the class diagram shown in Figure 12.2. in Liangs textbook. Do not use the C++ provided header file <vector>
  3. Write a test that unit tests the public interface of this class.Figure 12.2 lists several frequently used functions in the vector class in a UML class diagram vectorcelementType> +vector<el
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. Template function for swapping two numbers.

#include<iostream>
using namespace std;
template <class T>
void Swap(T &x, T &y)// function to swap generic elements
{
T temp = x;
x = y;
y = temp;
}

int main()
{
int x,y;
float a,b;
// check for int values
cout << "Enter two int numbers :"; // enter two int elements
cin>>x>>y;
cout << "\nBefore:\n"; // print before values
cout << "\nx value is :"<<x;
cout << "\ny value is :"<<y;
Swap(x, y); // call swap function and print after values
cout << "\nAfter:\n";
cout << "\nx value is :"<<x;
cout << "\ny value is :"<<y;

// check for float values and repeat the same
cout<<"Enter two float numbers"
cin>>a>>b;
cout << "\nBefore:\n";
cout << "\na value is :"<<a;
cout << "\nb value is :"<<b;
Swap(a, b);
cout << "\nAfter:\n";
cout << "\na value is :"<<a;
cout << "\nb value is :"<<b;
return 0;
}

2. Header file for the vector class:

vector.h

#include<iostream>
using namespace std;

template <class T>
class vector
{
   public:
       T* values;
       int pos = 0,siz = 0;
       vector()
       {
       values = new T[10];
       siz=10;
       }
       vector(int size)
       {
       int i;
           values = new T[size];
           for(i=0;i<size;i++)
               values[i] = 0;
           siz=size;
       }
       vector(int size, T x)
       {
       int i;
           values = new T[size];
           for(i=0;i<size;i++)
               values[i] = x;
           siz = size;  
       }
       void push_back(T x)
       {
       values[pos++] = x;
       }
       void pop_back()
       {
       values[--pos] = 0;
       siz = pos;
       }
       const unsigned int size()
       {
       return pos;
       }
       const T at(int index)
       {
       if(index<0 || index>pos)
           {
               cout<<"\nProvide correct position";
       return 0;
           }
       else
           return(values[index]);
       }
       bool const empty()
       {
       if(pos==0)
       return 1;
       return 0;
       }
       void clear()
       {
       int i;
       for(i=0;i<pos;i++)
       values[i] = 0;
       pos = 0;
       siz = pos;
       }
       void swap(vector v)
       {
       int i;
       for(i=0;i<pos;i++)
       this->values[i] = v.values[i];
       }
       void printValues()
       {
           cout<<"\n";
           int i;
           for(i=0;i<siz;i++)
               cout<<"\n"<<this->values[i];
       }
};

main:

#include"vector.h"
int main()
{
   //create an int vector of size 10
vector<int> intvector1(10);
// then print the values in the vector
intvector1.printValues();
  
//create a vector using simple constructor
vector<double> vector1();
  
//create a vector using the size and element value parameterized constructor
vector<char> vector2(5,'c');
//display the values
vector2.printValues();
  
//push the elements into intvector
intvector1.push_back(1);
intvector1.push_back(2);
intvector1.push_back(3);
intvector1.push_back(4);
intvector1.push_back(5);
  
//display the values
intvector1.printValues();
  
//pop one element
intvector1.pop_back();
//print
intvector1.printValues();
  
//print the value at position 5
cout<<"\n Value at position 5 is: "<<intvector1.at(5);
  
//print the size of the vector
cout<<"\n Size of the vector is: "<<intvector1.size();
  
//create another int vector for swapping
vector<int> intvector2(10);
intvector2.push_back(10);
intvector2.push_back(20);
intvector2.push_back(30);
intvector2.push_back(40);
intvector2.push_back(50);
//use the swap function to swap the values
   intvector1.swap(intvector2);
   //print the new values
   intvector1.printValues();
return 0;
}
OUTPUT:


0
0
0
0
0
0
0
0
0
0

c
c
c
c
c

1
2
3
4
5

1
2
3
4

Provide correct position
Value at position 5 is: 0
Size of the vector is: 4

10
20
30
40
Add a comment
Know the answer?
Add Answer to:
Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Template...
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
  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 file...

    Provide code and full projects neatly and in proper form and in the correct header and cpp files((we have to make 3 files header file , one .cpp file for function and one more main cpp file) Operator Overloading – Chapter 14 Design a class Complex for representing complex numbers and the write overloaded operators for adding, subtracting, multiplying and dividing 2 complex numbers. Also create a function that will print a complex number on the screen. Provide appropriate constructors...

  • NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT...

    NO ONE HAS PROVIDED THE CORRECT CODE TO PROVIDE THE GIVEN OUTPUT. PLEASE PROVIDE CODE THAT WOULD CAUSE THE HW1.java TO PRINT THE RIGHT DATA.!!! The LinkedList class implements both the List interface and the Stack interface, but several methods (listed below) are missing bodies. Write the code so it works correctly. You should submit one file, LinkedList.java. Do not change the interfaces. Do not change the public method headers. Do not rename the LinkedList class. None of your methods...

  • Design a class that contains: we have to make 3 files header file , one .cpp...

    Design a class that contains: we have to make 3 files header file , one .cpp file for function and one more main cpp file 9.3 (The Account class) Design a class named Account that contains: An int data field named id for the account. A double data field named balance for the account. A double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and...

  • vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector...

    vector.h: #ifndef VECTOR_H #define VECTOR_H #include <algorithm> #include <iostream> #include <cassert> template <typename T> class Vector {     public:         Vector( int initsize = 0 )         : theSize( initsize ),          theCapacity( initsize + SPARE_CAPACITY )         { objects = new T[ theCapacity ]; }         Vector( const Vector & rhs )         : theSize( rhs.theSize),          theCapacity( rhs.theCapacity ), objects( 0 )         {             objects = new T[ theCapacity ];             for( int k = 0; k < theSize; ++k)                 objects[ k ] = rhs.objects[ k...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • //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 -...

  • Please help me modify the Stash3.cpp and Stash3.h files below to utilize default arguments in the...

    Please help me modify the Stash3.cpp and Stash3.h files below to utilize default arguments in the constructor. Please test the constructor by creating two different versions of a Stash object. Please see the source code below: //Stash3.cpp //: C07:Stash3.cpp {O} // From Thinking in C++, 2nd Edition // Available at http://www.BruceEckel.com // (c) Bruce Eckel 2000 // Copyright notice in Copyright.txt // Function overloading #include "Stash3.h" #include "../require.h" #include <iostream> #include <cassert> using namespace std; const int increment = 100;...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

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