Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ HELP My professor is offering resubmission of this homework problem and i\'m

ID: 3733338 • Letter: C

Question

C++ HELP

My professor is offering resubmission of this homework problem and i'm unsure how to fix it:

Template Classes – Chapter 12

Write an example of a template function that can swap 2 generic elements (page 462 – code example 12.8).

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>

Write a test that unit tests the public interface of this class (Look at listing 12.8 on page 472).


His Comments:

- Vector class code is just a copy of my code

- Vector public interface test is just a copy of my code

My Code:

template_func.cpp

#include<iostream>

using namespace std;

template <class T>

void swapp(T&a, T&b) //Function Template

{

T temp = a;

a = b;

b = temp;

}

int Swap()

{

int x1 = 5, y1 = 10;

double x2 = 4.57, y2 = 8.58;

cout << "Before Swap:" << endl;

cout << "x1=" << x1 << " " << "y1=" << y1 << endl;

cout << "x2=" << x2 << " " << "y2=" << y2 << endl;

swapp(x1, y1);

swapp(x2, y2);

cout << "After Swap:" << endl;

cout << "x1=" << x1 << " " << "y1=" << y1 << endl;

cout << "x2=" << x2 << " " << "y2=" << y2 << endl;

return 0;

}

vector.h

#pragma once

template< typename T >

class vector

{

public:

vector();

vector(unsigned);

//vector(unsigned, T);

//~vector();

void push_back(T);

//void pop_back();

unsigned size() const;

//T at(int) const;

//bool empty() const;

void clear();

//void swap( vector<T>& );

private:

unsigned m_size = 0;

unsigned pos = 0;

T* m_data = nullptr;

};

template< typename T >

vector<T>::vector()

{

// fill me out

}

template< typename T >

vector<T>::vector(unsigned sz)

{

m_size = sz;

m_data = new T[sz];

}

template< typename T >

void vector<T>::push_back(T elem)

{

if (m_data != nullptr &&

pos != m_size && pos < m_size)

{

m_data[pos] = elem;

pos++;

}

else

{

// send out an APB for the out of bounds villian

}

}

template< typename T >

unsigned vector<T>::size() const

{

return pos;

}

template< typename T >

void vector<T>::clear()

{

m_size = 0;

m_pos = 0;

if (m_data != nullptr)

{

delete[] m_data;

m_data = nullptr;

}

}

mainfunc.cpp

#include "vector.h"

int main(int argc, char* arg[])

{

vector<int> vint(10);

for (int i = 10; i < 20; i++)

vint.push_back(i);

unsigned s = vint.size();

return 0;

}

Explanation / Answer

Please find my answer for Q1. (swapping function).

Please repost others in separate post.

#include <iostream>

using namespace std;

template <class T>
void swapp(T&a,T&b) //Function Template
{
T temp=a;
a=b;
b=temp;
}

int main()
{
int x1=5,y1=10;
float x2=4.57,y2=8.58;

cout<<"Before Swap:"<<endl;
cout<<"x1="<<x1<<" "<<"y1="<<y1<<endl;
cout<<"x2="<<x2<<" "<<"y2="<<y2<<endl;

swapp(x1,y1);
swapp(x2,y2);

cout<<"After Swap:"<<endl;
cout<<"x1="<<x1<<" "<<"y1="<<y1<<endl;
cout<<"x2="<<x2<<" "<<"y2="<<y2<<endl;

return 0;
}