Summary: In assn1 you will implement a user defined Vector class with similar fu
ID: 3878566 • Letter: S
Question
Summary: In assn1 you will implement a user defined Vector class with similar functionality to the Standard Template Library vector class. In addition, you will implement a test bench with unit tests that thoroughly test, via program execution, each of the member functions of your Vector class. A Vector.h (see below) and sample main.cc file are provided
NOTE: You may NOT alter, edit, change, etc. Vector.h, rather you will write your own Vector.cc implementation solution. You are required to expand the main.cc as your test harness. For full credit, you must also enumerate test harness code that tests ordinary and edge cases. Carefully consider possible inputs to your program’s functions and the correct behavior you intend to test for.
All three files: Vector.h, Vector.cc, and main.cc should compile in a Cloud9 terminal window at the prompt, with the following command, $ g++ -std=c++11 -Wall -Werror -W *.cc
// Vector.h
#ifndef VECTOR_H_
#define VECTOR_H_
class Vector {
private:
int capacity = 100; //initialize capacity to 100
int v[100]; //declare internal array of size 100
int s = 0; //initialize current number of items in v to 0
public:
Vector(); //default constructor
// mutators
void push_back(int item); //inserts item at position size-1
void push_front(int item); //inserts item at position 0, shifts items right
bool swap_i_j(int i, int j); //swaps the items at pos i and j, true if success
void swap(Vector v2); //swaps contents, at same positions of v and v2
void reverse(); //reverses the items in v
void clear(); //clears the items in v, sets s to 0
//accessors
int size() const; // returns the number of items in v
void display() const; //prints the contents of v to std::out
bool is_empty() const; //true if size()==0, false otherwise
const int& at(int i) const; //returns const item& at position i in v
int find(int p) const; //returns position of 1st instance of p in v, -1 if not found
int find(int a[], int size_a); //returns pos of 1st instance a[size_a] in v, -1 if not found
// overloaded operators
bool operator==(Vector rhs); //true if this==rhs, item wise, false otherwise
const int& operator[](int i) const; //returns const item& at position i in v
int& operator[](int i); //returns non-const item& at position i in v
};
#endif /* VECTOR_H_ */
Strategy: It is suggested you comment out sections of Vector.h and implement a Vector.cc that compiles with the sample main.cc provided. For example, you will need push_back(), clear(), display(), and operator[ ] to compile with the supplied main.cc. Consider beginning by implementing push_back() and display() only. Test it with the main.cc, commenting out those sections that operate on clear() and operator[ ]. Then repeat this process as you tackle more member functions.
need a vector .cc file annd main.cc file which would be a test bench
Explanation / Answer
Here is your program. You can modify the code and insert your own test cases in main.cc
/*********************/
#ifndef VECTOR_H_
#define VECTOR_H_
class Vector {
private:
int capacity = 100; //initialize capacity to 100
int v[100]; //declare internal array of size 100
int s = 0; //initialize current number of items in v to 0
public:
Vector(); //default constructor
// mutators
void push_back(int item); //inserts item at position size-1
void push_front(int item); //inserts item at position 0, shifts items right
bool swap_i_j(int i, int j); //swaps the items at pos i and j, true if success
void swap(Vector& v2); //swaps contents, at same positions of v and v2
void reverse(); //reverses the items in v
void clear(); //clears the items in v, sets s to 0
//accessors
int size() const; // returns the number of items in v
void display() const; //prints the contents of v to std::out
bool is_empty() const; //true if size()==0, false otherwise
const int& at(int i) const; //returns const item& at position i in v
int find(int p) const; //returns position of 1st instance of p in v, -1 if not found
int find(int a[], int size_a); //returns pos of 1st instance a[size_a] in v, -1 if not found
// overloaded operators
bool operator==(Vector rhs); //true if this==rhs, item wise, false otherwise
const int& operator[](int i) const; //returns const item& at position i in v
int& operator[](int i); //returns non-const item& at position i in v
};
#endif /* VECTOR_H_ */
/****************************/vector.cc
/*
* Vector.cpp
*
* Created on: 25-Jan-2018
* Author: kuhu
*/
#include "Vector.h"
#include <iostream>
Vector::Vector() {
// TODO Auto-generated constructor stub
for(int i=0;i<capacity;i++)
{
v[i]=0;
}
}
void Vector::push_back(int item){
if(s<capacity)
{
v[s]=item;
s++;
}
}
void Vector::push_front(int item){
if(s<capacity)
{
for(int i=s;i>0;i--)
{
v[i]=v[i-1];
}
v[0]=item;
s++;
}
}
bool Vector::swap_i_j(int i, int j){
if(i<s-1 && j<s-1)
{
int temp=v[i];
v[i]=v[j];
v[j]=temp;
return true;
}
return false;
}
void Vector::swap(Vector& v2){
Vector vtemp=v2;
v2=*this;
*this=vtemp;
}
void Vector::reverse(){
for(int i=0;i<s/2;i++)
{
swap_i_j(i,s-i-1);
}
}
void Vector::clear(){
for(int i=0;i<s;i++)
{
v[i]=0;
s=0;
}
}
//accessors
int Vector::size() const{
return s;
}
void Vector::display() const{
for(int i=0;i<s;i++)
{
std::cout<<v[i]<<" ";
}
std::cout<<std::endl;
}
bool Vector::is_empty() const{
return s==0;
}
const int& Vector::at(int i) const{
if(i<s-1)
{
return v[i];
}
return 0;
}
int Vector::find(int p) const{
for(int i=0;i<s;i++)
{
if(v[i]==p)
{
return i;
}
}
return -1;
}
int Vector::find(int a[], int size_a){
for(int i=0;i<s;i++)
{
if(a[size_a]==v[i])
{
return i;
}
}
return -1;
}
// overloaded operators
bool Vector::operator==(Vector rhs){
if(s!=rhs.s)
return false;
for(int i=0;i<s;i++)
{
if(v[i]!=rhs.v[i])
return false;
}
return true;
}
const int& Vector::operator[](int i) const{
if(i<s-1)
{
return v[i];
}
return 0;
}
int& Vector::operator[](int i){
return v[i];
}
/*******************************/main.cc
//============================================================================
// Name : vec.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <iostream>
#include "Vector.h"
using namespace std;
int main() {
Vector v;
v.push_back(10);
v.push_back(20);
v.push_front(30);
v.display();
v.swap_i_j(0,2);
Vector v2;
v2.push_back(100);
v2.push_front(50);
v2.display();
v.swap(v2);
v.display();
v2.display();
v.reverse();
v.clear();
cout<<"No of items in V2 "<<v2.size()<<endl;
v2.display();
if(v.is_empty())
cout<<"Vector is empty"<<endl;
else
cout<<"Vector is non-empty"<<endl;
cout<<"Item is "<<v2.at(1)<<endl;;
cout<<"Item found at "<<v2.find(10)<<endl;
int a[]={10,20,30,40};
cout<<"Item found at "<<v2.find(a, 2)<<endl;
if(v==v2)
cout<<"Both vectors are same"<<endl;
else
cout<<"Vectors are different "<<endl;
cout<<"Value is "<<v2[0]<<endl;
return 0;
}
/******************************/output
30 10 20
50 100
50 100
30 10 20
No of items in V2 3
30 10 20
Vector is empty
Item is 10
Item found at 1
Item found at 0
Vectors are different
Value is 30
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.