1. #ifndef MYLIST_H_
#define MYLIST_H_
#include<iostream>
#include<cctype>
using namespace std;
//PROJECT 2 STUDENT FILE
//template class that simulates an ordered list of common
elements
//It is assumed that the list will either be empty or completely
full
template <class type>
class myList
{
protected:
int length; //the number of elements in the list
type *items; //dynamic array to store the elements
public:
~myList();
//destructor for memory cleanup
//Postcondition: Deallocates the memory occupied by the
items array
myList();
//default constructor
//Postcondition: creates items array of size 0 and sets size
to zero
myList(int n, type t);
//assignment constructor
//Postcondition: creates items array of size n. Each
element in the list
//is assigned the value of type t, sets length to n
myList(int n, type *anArray);
2. //assignment constructor
//Postcondition: copies contents of anArray parameter into
items array
//sets length to n
myList(const myList & otherList);
//copy constructor
//Postcondition: makes a deep copy of the parameter
otherList to
//the calling list
void print(ostream& outStream);
//prints the elements of the list using outStream
//Postcondition: The elements of the list are printed to the
//output stream each separated by a comma. The last
element
//printed should not have a comma after it
void append(const type& theItem);
//assigns the parameter theItem to the end of the list
//Postcondition: the parameter theItem is the last element
in the list
bool isIn(const type& theItem);
//determines if an element is currently in the list
//Postcondition: Returns true if the parameter theItem is in
the list,
//otherwise returns false
bool insert(const type& theItem, int location);
//inserts an element into the list
//Postcondition: inserts the parameter theItem into the list
at position
//determined by the parameter location and then returns
true, if the location
//is greater than the length of the list + 2, then the function
returns false
3. //LOCATION MEANS COUNTING FROM 1, SO
LOCATION 1 MEANS INDEX POSITION 0, ETC..
void sort(char ch);
//Assuming the list contains numbers, characters, or
strings this functions
//sorts the elements in the list, where type is either
'A','a'
//for ascending, or 'D','d' for descending
//Choose any sorting algorithm of your choice
//Postcondition: sorts the elements in the list
//OVERLOADED OPERATORS
myList& operator=(const myList& rhs);
//overloading of the assignment operator to allow for
list to list assignment
myList& operator+=(const type& t);
//overloading of the addition_equal operator that adds
the parameter t
//to the end of the list
myList& operator+=(const myList& rhs);
//overloading of the addition_equal operator that adds
another list
//to the end of the list
/*
FOR 10% EXTRA CREDIT ON THIS PROJECT GRADE,
OVERLOAD THE FOLLOWING OPERATORS
AS NON-MEMBER FUNCTIONS
(1) << (insertion)
(2) + (addition) that adds one element to a list
(3) + (addition) that adds two lists
HINT: to overload both addition operators as stated above
4. use the member
functions +=
KEEP IN MIND THAT THE SYNTAX GETS TRICKY
WHEN WRITING NON-MEMBER FUNCTIONS
FOR TEMPLATE CLASSES
See example prototypes below
*/
//friend ostream& operator<< (ostream& outStream, const
myList<type>& theList){}
//friend myList& operator+ (myList<type> &lhs, const
type& t){}
//friend myList& operator+ (myList<type> &lhs, const
myList<type>& rhs){}
};
template <class type>
myList<type>::~myList()
{
delete [] items;
}
template <class type>
myList<type>::myList()
{
length=0;
items=new type;
items = nullptr;
}
#endif /* MYLIST_H_ */
Create a template class that implements a dynamic array based
list of common elements from above code
Source Files:
myList.hpp (which is the code above)
testmyList.cpp (the name of the test program to be created)
Since you will be writing a “template class”, it is easiest to
implement the class in the header file myList.hpp
5. Therefore, you will not need a myList.cpp implementation file.
Just do all of your implementation of the class myList in
the header file myList.hpp.
To compile the header file myList.hpp issue the command: g++
-c myList.hpp
You should be in charge of creating your own test program.
Call it testmyList.cpp
Solution
#ifndef MYLIST_H_
#define MYLIST_H_
#include<iostream.h>
//PROJECT 2 STUDENT FILE
//template class that simulates an ordered list of common
elements
//It is assumed that the list will either be empty or completely
full
template <class type>
class myList
{
protected:
int length; //the number of elements in the list
type *items; //dynamic array to store the elements
6. public:
~myList()
{
delete items;
//destructor for memory cleanup
//Postcondition: Deallocates the memory occupied by the
items array
}
myList()
{
items=0;
//default constructor
//Postcondition: creates items array of size 0 and sets size to
zero
}
myList(int n, type t)
{
length=n;
items=new <t>items[n];
//assignment constructor
//Postcondition: creates items array of size n. Each element
in the list
//is assigned the value of type t, sets length to n
}
myList(int n, type *anArray)
7. {
for(int i=0;i<n;i++)
{
items[i]=anArray[i];
}
//assignment constructor
//Postcondition: copies contents of anArray parameter into
items array
//sets length to n
}
myList(const myList & otherList)
{
items=otherList.items;
//copy constructor
//Postcondition: makes a deep copy of the parameter
otherList to
//the calling list
}
void print(ostream& outStream)
{
outStream<<items;
//prints the elements of the list using outStream
//Postcondition: The elements of the list are printed to the
//output stream each separated by a comma. The last element
//printed should not have a comma after it
8. }
void append(const type& theItem)
{
//assigns the parameter theItem to the end of the list
//Postcondition: the parameter theItem is the last element in
the list
}
bool isIn(const type& theItem)
{
//determines if an element is currently in the list
//Postcondition: Returns true if the parameter theItem is in
the list,
//otherwise returns false
}
bool insert(const type& theItem, int location)
{
//inserts an element into the list
//Postcondition: inserts the parameter theItem into the list at
position
//determined by the parameter location and then returns true,
if the location
//is greater than the length of the list + 2, then the function
returns false
//LOCATION MEANS COUNTING FROM 1, SO
LOCATION 1 MEANS INDEX POSITION 0,
9. }
void sort(char ch)
{
switch(ch)
{
case 'd':
case 'D':
type *n;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(type[i]<type[j])
{
n=type[i];
type[i]=type[j];
type[j]=n;
}
}
}
break;
case 'a':
case 'A':
type *n;
for(int i=0;i<n;i++)
10. {
for(int j=i+1;j<n;j++)
{
if(type[i]>type[j])
{
n=type[i];
type[i]=type[j];
type[j]=n;
}
}
}
break;
}
//Assuming the list contains numbers, characters, or strings
this functions
//sorts the elements in the list, where type is either 'A','a'
//for ascending, or 'D','d' for descending
//Choose any sorting algorithm of your choice
//Postcondition: sorts the elements in the list
}
//OVERLOADED OPERATORS
myList& operator=(const myList& rhs)
{
//overloading of the assignment operator to allow for list to
list assignment
11. }
myList& operator+=(const type& t)
{
//overloading of the addition_equal operator that adds the
parameter t
//to the end of the list
}
myList& operator+=(const myList& rhs)
{
//overloading of the addition_equal operator that adds
another list
//to the end of the list
}
};