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

Object-Oriented Programming C++ - Class called PFAString with these requirements

ID: 3728857 • Letter: O

Question

Object-Oriented Programming C++ - Class called PFAString with these requirements. Please read everything and make sure they are according to the requirements listed. READ NOTE AT THE END OF POST

Provided guide:

PFAString.hpp

#ifndef PFASTRING_HPP
#define PFASTRING_HPP

#include <string>

class PFAString {
public:
Default constructor
Constructor that sets capacity ONLY
Constructor that sets capacity and initializes all elements to a specific string // If no string is provided, then a null string will be used. This is a single constructor (no overloads for this case)
Copy Constructor // 1st type only (Refer to list of vector constructors on cplusplus.com)
Move Constructor // 1st type only
Destructor
function capacity
function size
function push_back // Places a string at the back of the array, l-value version only
function pop_back // Destroys the string at the back of the array
function resize // Grows or shrinks array accordingly, must behave like vector resize() function
function empty_array // Sets the size to zero
overload operator [] // Needs to work as l-value
overload copy operator =
overload move operator =
overload operator ==
overload operator !=
overload operator <
overload operator <=
overload operator >
overload operator >=
private:
std::string *arr;
int _capacity; // Capacity and size are two different things
int _size;
};

#endif

END CODE

NOTE: Be sure to please to the move constructor and move assignment operator. If anything, do those first; I really only need those two done I think. I'll be sure to give an upvote.

. Use the provided file PFAString.hpp as a guide. Do NOT change the name of the header file. The name of your implementation file shall be PFAString.cpp NOTE: The header file is an outline, and you are required to flesh it out (const where needed, etc.) The provided header file is a base to work on. Nothing should be removed, function names should not be changed, but you are free to add private helper functions and additional functions and member variables as you see fit The copy constructor should construct the new object to have a capaciy equal to the size of the object being copied The same holds true for the copy operator overload of The overloaded [] should be written so that it can function as an l-value For the relational overloads, the comparison should be lexicographical NOTE: You are NOT allowed to use the lexicographical.compare algorithm http://www.cplusplus.com/reference/vector/vector/operators/ http://www.cplusplus.com/reference/algorithm/lexicographical-compare/ Reading these resources will save you a lot of unnecessary work When the array is expanded, it MUST expand to twice its previous capacity. L.E., an array of capacity 20 will expand to capacity 40 when an attempt to add a 21st element occurs For further clarification, read about the equivalent function in the class on either cplusplus. com or cppreference.com

Explanation / Answer

main.cpp

#include "PFAString.hpp"

#include <fstream>

#include <iomanip>

#include <iostream>

#include <string>

bool testDConstr();   

bool testCapConstr();

bool testFillConstr();

bool testCopConstr();

bool testGetCap();

bool testGetSize();

bool testPushBack();

bool testPopBack();

bool testResizeSmaller();

bool testResizeLarger();

bool testEmpty();

bool testExpansion();

bool testSQBLV();

bool testSQAcc();

bool testAssgn();

bool testEQUIVT();

bool testEQUIVF();

bool testLTT();   

bool testLTF();   

bool testGTT();   

bool testGTF();   

bool testLEQT();  

bool testLEQE();  

bool testLEQF();  

bool testGEQT();  

bool testGEQE();  

bool testGEQF();  

const int COL = 37;

int main() {

std::ofstream outf("outfile");

if (outf.fail()) {

    std::cout << "Error opening output file. Exiting... ";

    return 1;

}

outf << std::setw(COL) << std::left << "Default Constructor:" << testDConstr()

       << std::endl;

outf << std::setw(COL) << std::left

      << "Capacity Constructor:" << testCapConstr() << std::endl;

outf << std::setw(COL) << std::left << "Fill Constructor:" << testFillConstr()

       << std::endl;

outf << std::setw(COL) << std::left << "Copy Constructor:" << testCopConstr()

       << std::endl;

outf << std::setw(COL) << std::left << "Function getCapacity:" << testGetCap()

       << std::endl;

outf << std::setw(COL) << std::left << "Function getSize:" << testGetSize()

       << std::endl;

outf << std::setw(COL) << std::left << "Function push_back:" << testPushBack()

       << std::endl;

outf << std::setw(COL) << std::left << "Function pop_back:" << testPopBack()

       << std::endl;

outf << std::setw(COL) << std::left

       << "Function resize (shrink):" << testResizeSmaller() << std::endl;

outf << std::setw(COL) << std::left

       << "Function resize (grow):" << testResizeLarger() << std::endl;

outf << std::setw(COL) << std::left << "Function empty_array:" << testEmpty()

       << std::endl;

outf << std::setw(COL) << std::left << "Array expansion:" << testExpansion()

       << std::endl;

outf << std::setw(COL) << std::left

       << "Square bracket l-value:" << testSQBLV() << std::endl;

outf << std::setw(COL) << std::left << "Square bracket access:" << testSQAcc()

       << std::endl;

outf << std::setw(COL) << std::left << "Operator Assignment:" << testAssgn()

       << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Equivalency(T):" << testEQUIVT() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Equivalency(F):" << testEQUIVF() << std::endl;

outf << std::setw(COL) << std::left << "Operator Less than(T):" << testLTT()

       << std::endl;

outf << std::setw(COL) << std::left << "Operator Less than(F):" << testLTF()

       << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Greater than(T):" << testGTT() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Greater than(F):" << testGTF() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Less than or Equal(T):" << testLEQT() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Less than or Equal(E):" << testLEQE() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Less than or Equal(F):" << testLEQF() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Greater than or Equal(T):" << testGEQT() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Greater than or Equal(E):" << testLEQE() << std::endl;

outf << std::setw(COL) << std::left

       << "Operator Greater than or Equal(F):" << testLEQE() << std::endl;

outf.close();

return 0;

}

bool testDConstr() {

PFAString obj;

return (obj.get_capacity() == 0 && obj.get_size() == 0);

}

bool testCapConstr() {

PFAString obj(5);

return (obj.get_capacity() == 5 && obj.get_size() == 0);

}

bool testFillConstr() {

int cap = 5;

std::string PATTERN = "HI-DIDDLY-HO";

PFAString obj(cap, PATTERN);

if (obj.get_capacity() == cap && obj.get_size() == cap) {

    for (int i = 0; i < cap; ++i) {

      if (obj[i] != PATTERN) {

        return false;

      }

    }

    return true;

} else {

    return false;

}

}

bool testCopConstr() {

PFAString one;

one.push_back("THIS");

one.push_back("IS");

one.push_back("SPARTA");

PFAString two = one;

if (two.get_capacity() == 3 && two.get_size() == 3) {

    for (int i = 0; i < one.get_size(); ++i) {

      if (two[i] != one[i]) {

        return false;

      }

    }

  return true;

} else {

    return false;

}

}

bool testGetCap() {

int cap = 10;

PFAString obj(cap);

return (obj.get_capacity() == cap);

}

bool testGetSize() {

PFAString obj(3, "HUH");

return (obj.get_size() == 3);

}

bool testPushBack() {

PFAString obj(3);

obj.push_back("HUUH");

return (obj[0] == "HUUH");

}

bool testPopBack() {

PFAString obj(3, "GANGNAM");

obj.pop_back();

return (obj.get_size() == 2);

}

bool testResizeSmaller() {

PFAString obj(3, "HULLO");

obj.resize(1);

return (obj.get_size() == 1 && obj.get_capacity() == 3 && obj[0] == "HULLO");

}

bool testResizeLarger() {

PFAString obj(3, "HULLO");

obj.resize(5, "PSY");

for (int i = 0; i < 3; ++i) {

    if (obj[i] != "HULLO") {

      return false;

    }

}

for (int i = 3; i < 5; ++i) {

    if (obj[i] != "PSY") {

      return false;

    }

}

return (obj.get_size() == 5 && obj.get_capacity() == 5);

}

bool testEmpty() {

PFAString obj(3, "PSY");

obj.empty_array();

return (obj.get_capacity() == 3 && obj.get_size() == 0);

}

bool testExpansion() {

PFAString obj(3, "GANGNAM");

obj.push_back("STYLE");

return (obj.get_capacity() == 6 && obj.get_size() == 4);

}

bool testSQBLV() {

PFAString obj(2, "LILY");

obj[1] = "FLOWER";

return (obj[0] == "LILY" && obj[1] == "FLOWER");

}

bool testSQAcc() {

PFAString obj(1, "LILY");

return (obj[0] == "LILY");

}

bool testAssgn() {

PFAString obj(3, "HOW");

PFAString test(4, "NOW");

test = obj;

for (int i = 0; i < test.get_size(); ++i) {

    if (test[i] != "HOW") {

      return false;

    }

}

return (test.get_capacity() == 3 && test.get_size() == 3);

}

bool testEQUIVT() {

PFAString obj;

PFAString test(3, "HI");

obj.push_back("HI");

obj.push_back("HI");

obj.push_back("HI");

return (obj == test);

}

bool testEQUIVF() {

PFAString obj, test;

obj.push_back("HI");

test.push_back("HO");

return !(obj == test);

}

bool testLTT() {

PFAString obj, test;

obj.push_back("A");

test.push_back("B");

  return (obj < test);

}

bool testLTF() {

PFAString obj, test;

obj.push_back("A");

test.push_back("B");

return !(test < obj);

}

bool testGTT() {

PFAString obj, test;

obj.push_back("A");

test.push_back("B");

return (test > obj);

}

bool testGTF() {

PFAString obj, test;

obj.push_back("A");

test.push_back("B");

return !(obj > test);

}

bool testLEQT() {

PFAString obj, test;

obj.push_back("B");

test.push_back("B");

test.push_back("B");

return (obj <= test);

}

bool testLEQE() {

PFAString obj, test;

obj.push_back("THIS");

obj.push_back("IS");

obj.push_back("SPARTA! *punt*");

test = obj;

return (obj <= test);

}

bool testLEQF() {

PFAString obj, test;

obj.push_back("THIS");

obj.push_back("IS");

obj.push_back("SPARTA! *punt*");

test = obj;

test.push_back("AAaah");

return !(test <= obj);

}

bool testGEQT() {

PFAString obj, test;

obj.push_back("AIAIAI");

test.push_back("AHAHAH");

return (obj >= test);

}

bool testGEQE() {

PFAString obj, test;

obj.push_back("THIS");

obj.push_back("IS");

obj.push_back("SPARTA! *punt*");

test = obj;

return (test >= obj);

}

bool testGEQF() {

PFAString obj, test;

obj.push_back("THIS");

obj.push_back("IS");

obj.push_back("SPARTA! *punt*");

test = obj;

test.push_back("AAaah");

return !(obj <= test);

}

PFAString.cpp

#include "PFAString.hpp"

#include <algorithm>

#include <iostream>

#include <iterator>

#define min(X, Y) ((X) < (Y) ? (X) : (Y))

PFAString::PFAString() {

capacity = 0;

arr = new std::string[capacity];

size = 0;

}

PFAString::PFAString(const int requestedCapacity) {

capacity = requestedCapacity;

arr = new std::string[capacity];

size = 0;

}

PFAString::PFAString(const int requestedCapacity, const std::string fillWith) {

capacity = requestedCapacity;

arr = new std::string[capacity];

size = capacity;

for (int i = 0; i < capacity; i++) {

    arr[i] = fillWith;

}

}

PFAString::PFAString(const PFAString &copy) {

capacity = copy.size;

arr = new std::string[capacity];

size = copy.size;

for (int i = 0; i < size; i++) {

    arr[i] = copy[i];

}

}

PFAString::~PFAString() { delete[] arr; }

int PFAString::get_capacity() { return capacity; }

int PFAString::get_size() { return size; }

void PFAString::push_back(std::string insert) {

if (capacity < size + 1) {

    if (capacity == 0) {

      resize(1);

    } else {

      resize(2 * capacity);

    }

}

arr[size] = insert;

size++;

}

void PFAString::pop_back() { size--; }

void PFAString::resize(int newCapacity) {

if (newCapacity < 0) {

    resize(0);

    return;

}

if (newCapacity < capacity) {

    size = newCapacity;

    return;

}

std::string *new_arr = new std::string[newCapacity];

for (int i = 0; i < newCapacity && i < capacity; i++) {

    new_arr[i] = arr[i];

}

delete[] arr;

arr = new_arr;

size = min(size, newCapacity);

capacity = newCapacity;

}

void PFAString::resize(int newCapacity, std::string fillWith) {

resize(newCapacity);

for (int i = size; i < newCapacity; i++) {

    arr[i] = fillWith;

}

size = newCapacity;

}

void PFAString::empty_array() { size = 0; }

std::string &PFAString::operator[](const int index) const {

if (index < 0 || index > size) {

    exit(1);

}

return arr[index];

}

PFAString PFAString::operator=(const PFAString copy) {

capacity = copy.size;

arr = new std::string[capacity];

size = copy.size;

for (int i = 0; i < size; i++) {

  arr[i] = copy[i];

}

return *this;

}

bool PFAString::operator==(const PFAString compareTo) const {

return !(*this < compareTo || compareTo < *this);

}

bool PFAString::operator<(const PFAString compareTo) const {

int minLength = min(size, compareTo.size);

for (int i = 0; i < minLength; i++) {

    if (arr[i] != compareTo[i]) {

      return arr[i] < compareTo[i];

    }

}

return size < compareTo.size;

}

bool PFAString::operator>(const PFAString compareTo) const {

return compareTo < *this;

}

bool PFAString::operator<=(const PFAString compareTo) const {

return !(compareTo < *this);

}

bool PFAString::operator>=(const PFAString compareTo) const {

return !(*this < compareTo);

}

PFAString.hpp

#ifndef PFASTRING_HPP

#define PFASTRING_HPP

#include <string>

class PFAString {

public:

PFAString();

PFAString(const int);

PFAString(const int, const std::string);

PFAString(const PFAString &);

~PFAString();

int get_capacity();

int get_size();

void push_back(std::string);             

void pop_back();                       

void resize(int);

void resize(int, std::string);  

void empty_array();                      

std::string &operator[](const int) const;

PFAString operator=(const PFAString);    

bool operator==(const PFAString) const;

bool operator<(const PFAString) const;

bool operator>(const PFAString) const;

bool operator<=(const PFAString) const;

bool operator>=(const PFAString) const;

private:

std::string *arr;

int capacity;                            

int size;

};

#endif