This is a continuation of the previous lab. You will add the “Big Three” member
ID: 3847210 • Letter: T
Question
This is a continuation of the previous lab. You will add the “Big Three” member functions (copy constructor, assigment operator, and destructor) to your Table class. You will need a slightly revised Entry class, which counts the number of Entry objects that are created and destroyed, so we can test your destructor.
Add the declarations (not the definitions) to table.h.
Both your copy constructor and assignment operator must take a constant reference to a Table object as the only parameter.
Your assignment operator must return the calling object as a reference.
Step 3: Update your Tables implementation file
Add the definitions (not the declarations) to table.cpp. You may use tools from any of the standard libraries except, , and .
Both the copy constructor and the assignment operator must make completely independent copies of the source Table. In other words, if Table x is a copy of Table y, then future changes to x will not affect y, and vice-versa.
Your destructor must release all memory allocated via the operator new or calls to malloc().
If your get, remove or output functions did not pass the time tests for h05, then you should improve them now. Doing so might require you to change your overall approach to the problem: if you are using linear probing with open addressing to implement the table, then consider using quadratic probing or double hashing instead, or even consider using a chaining approach instead of open addressing.
BELOW table.h, entry.cpp, and entry.h are given! USE C++ and simplest way. CREATE THE table.cpp FILE using these files!!
table.h
#ifndef TABLE_H
#define TABLE_H
#include
#include
#include "entry.h"
#include
#include
#include
using namespace std;
class Table {
public:
Table(unsigned int max_entries = 100);
Table(unsigned int entries, istream &input);
void put(unsigned int key, string data);
void put(Entry e);
string get(unsigned int key) const;
bool remove(unsigned int key);
void print();
string toString(Entry e);
int tellMax() const;
Entry tellEntry(int i) const;
int partition(vector& A, int left, int right, int who);
void qsort(vector& A, int left, int right);
int getUsed() const;
friend ostream& operator<< (ostream &out, const Table &t);
private:
static unsigned int accesses;
unsigned int max_entries;
string data;
unsigned int entries;
string input;
vector myTable;
int hash(unsigned int key, int mod_value) const;
unsigned int used;
unsigned int capacity;
};
ostream& operator<< (ostream &out, const Table ¶mTable);
#endif
entry.h
#ifndef ENTRY_H
#define ENTRY_H
#include
#include
class Entry {
public:
// constructors and destructor
Entry(unsigned int key = 0, std::string data = "");
Entry(const Entry &other);
~Entry();
// access and mutator functions
unsigned int get_key() const;
std::string get_data() const;
static unsigned int access_count();
static unsigned int instance_count();
void set_key(unsigned int k);
void set_data(std::string d);
// operator conversion function simplifies comparisons
operator unsigned int () const;
// input and output friends
friend std::istream& operator>>
(std::istream& inp, Entry &e);
friend std::ostream& operator<<
(std::ostream& out, Entry &e);
private:
unsigned int key;
std::string data;
static unsigned int accesses;
static unsigned int instances;
};
#endif
entry.cpp
#include "entry.h"
#include
using namespace std;
unsigned int Entry::accesses = 0;
unsigned int Entry::instances = 0;
Entry::Entry(unsigned int key, std::string data)
: key(key), data(data) { ++instances; }
Entry::Entry(const Entry &other)
: key(other.key), data(other.data){ ++instances; }
Entry::~Entry() { --instances; }
unsigned int Entry::get_key() const
{ ++accesses; return key; }
std::string Entry::get_data() const
{ return data; }
unsigned int Entry::access_count()
{ return accesses; }
unsigned int Entry::instance_count()
{ return instances; }
void Entry::set_key(unsigned int k)
{ key = k; }
void Entry::set_data(std::string d)
{ data = d; }
Entry::operator unsigned int () const
{ return get_key(); }
istream& operator>> (istream& inp, Entry &e) {
inp >> e.key;
// get data in two parts to handle white space
string first_word, rest_of_line;
inp >> first_word;
getline(inp, rest_of_line);
e.data = first_word + rest_of_line;
return inp;
}
ostream& operator<< (ostream& out, Entry &e) {
out << e.get_key() << ": " << e.get_data();
return out;
}
Explanation / Answer
#ifndef TABLE_H
#define TABLE_H
#include
#include
#include "entry.h"
#include
#include
#include
using namespace std;
class Table {
public:
Table(unsigned int max_entries = 100);
Table(unsigned int entries, istream &input);
void put(unsigned int key, string data);
void put(Entry e);
string get(unsigned int key) const;
bool remove(unsigned int key);
void print();
string toString(Entry e);
int tellMax() const;
Entry tellEntry(int i) const;
int partition(vector& A, int left, int right, int who);
void qsort(vector& A, int left, int right);
int getUsed() const;
friend ostream& operator<< (ostream &out, const Table &t);
private:
static unsigned int accesses;
unsigned int max_entries;
string data;
unsigned int entries;
string input;
vector myTable;
int hash(unsigned int key, int mod_value) const;
unsigned int used;
unsigned int capacity;
};
ostream& operator<< (ostream &out, const Table ¶mTable);
#endif
entry.h
#ifndef ENTRY_H
#define ENTRY_H
#include
#include
class Entry {
public:
// constructors and destructor
Entry(unsigned int key = 0, std::string data = "");
Entry(const Entry &other);
~Entry();
// access and mutator functions
unsigned int get_key() const;
std::string get_data() const;
static unsigned int access_count();
static unsigned int instance_count();
void set_key(unsigned int k);
void set_data(std::string d);
// operator conversion function simplifies comparisons
operator unsigned int () const;
// input and output friends
friend std::istream& operator>>
(std::istream& inp, Entry &e);
friend std::ostream& operator<<
(std::ostream& out, Entry &e);
private:
unsigned int key;
std::string data;
static unsigned int accesses;
static unsigned int instances;
};
#endif
entry.cpp
#include "entry.h"
#include
using namespace std;
unsigned int Entry::accesses = 0;
unsigned int Entry::instances = 0;
Entry::Entry(unsigned int key, std::string data)
: key(key), data(data) { ++instances; }
Entry::Entry(const Entry &other)
: key(other.key), data(other.data){ ++instances; }
Entry::~Entry() { --instances; }
unsigned int Entry::get_key() const
{ ++accesses; return key; }
std::string Entry::get_data() const
{ return data; }
unsigned int Entry::access_count()
{ return accesses; }
unsigned int Entry::instance_count()
{ return instances; }
void Entry::set_key(unsigned int k)
{ key = k; }
void Entry::set_data(std::string d)
{ data = d; }
Entry::operator unsigned int () const
{ return get_key(); }
istream& operator>> (istream& inp, Entry &e) {
inp >> e.key;
// get data in two parts to handle white space
string first_word, rest_of_line;
inp >> first_word;
getline(inp, rest_of_line);
e.data = first_word + rest_of_line;
return inp;
}
ostream& operator<< (ostream& out, Entry &e) {
out << e.get_key() << ": " << e.get_data();
return out;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.