NEEDS TO BE DONE IN C++. THERES SOME EXAMPLES OF CODE TO FOLLOW FOR REQUIREMENTS
ID: 639686 • Letter: N
Question
NEEDS TO BE DONE IN C++. THERES SOME EXAMPLES OF CODE TO FOLLOW FOR REQUIREMENTS BELOW PROBLEM DESCRIPTION. SEEMS LIKE ALOT OF TEXT BUT MOSTLY EXAMPLES FOR WRITTEN PROGRAM.
HERES THE PROBLEM DESCRIPTION
Objectives:
1. Students will gain an experience to use array to implement the ADT sorted list.
Problem Descriptions:
One of the most frequently performed computing tasks is the maintenance, in some specified order, of a collection of data. Examples are such as students placed in order by their names, baseball players listed in order by their batting averages, and corporations listed in order by their assets. This is called sorted list. In contrast, the items on a grocery list might be ordered
Explanation / Answer
/** @file BagInterface.h */
# ifndef BAGINTERFACE_H
# define BAGINTERFACE_H
# include <vector>
#include <string>
template < class ItemType >
class BagInterface
{
public:
BagInterface();
virtual int getCurrentSize () /*const = 0*/;
virtual bool isEmpty () /*const = 0*/;
virtual bool add (const ItemType & newEntry) /*= 0*/;
}
; // end BagInterface
#endif /* BAGINTERFACE_H_ */
ArrayBag.h
/** @file BagInterface.h */
# ifndef ARRAYBAG_H
# define ARRAYBAG_H
#include <vector>
#include <string>
#include "BagInterface.h"
template < class ItemType >
class ArrayBag : public BagInterface < ItemType >
{
private:
ItemType theBag [100];
int count;
public:
ArrayBag();
int getCurrentSize () /*const = 0*/;
bool isEmpty () /*const = 0*/;
bool add (const ItemType & newEntry) /*= 0*/;
}; // end
#endif /* BAGINTERFACE_H_ */
ArrayBag.cpp
# include "ArrayBag.h" // For ADT bag
template < class ItemType > // it is template for class name
ArrayBag<ItemType>::ArrayBag() // defualt constructor
{
count = 0; // initial value is zero for count
}
template < class ItemType >
int ArrayBag<ItemType>::getCurrentSize()
{
return count;
} // end getcurrentSize
template < class ItemType >
bool ArrayBag<ItemType>::isEmpty(){
return count==0;
} // end isempty
// flag variable is true after it will check if count value is greater or equal that 100. If count is greater than 100
// return value is flase. If return count value is less than 100, it will implement thabag array is count = new Entry parameter
// after count will be increasing/
template < class ItemType >
bool ArrayBag<ItemType>::add (const ItemType & newEntry){
bool flag = true;
if(count>=100)
{
return false;
} //end if
else
{
theBag[count]=newEntry;
count++; // it is incrementation
}
return flag;
} // end add
main.cpp
#include "ArrayBag.h"
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main ()
{
/*Attempt 1
BagInterface < string > *bagPtr = nullptr;
bagPtr = new ArrayBag < string > ();
bagPtr.add("hello");
cout << add.getCurrentSize()<<endl;
delete bagPtr;
bagPtr = nullptr*/
//Attempt 2
ArrayBag<string> b;
//b.add("hello");
//cout << b.getCurrentSize()<<endl;
system("PAUSE");
return 0;
} // end main
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.