Write your question here.
Could someone please help me with overloading relational operator?
[code]
Put the code you need help with here.
#include <string> // for the use of the string class
#include <vector> // For the use of the vector class.
#include <iostream> // std::cout, std::boolalpha
#include <algorithm> // std::lexicographical_compare
#include <cctype> // std::tolower
#include "PFAString.hpp"
using namespace std;
// Default constructor
PFAString::PFAString()
{
}
// Constructor that sets capacity
PFAString::PFAString(int capValue)
{
arr = new string[capacity];
}
// Constructor that sets capacity and initializes all elements to a specific string
PFAString::PFAString(int capValue, PFAStringObject)
{
arr = new string[capacity];
capacity = 0;
size = 0;
//vector < PFAString *> arr;
//std::vector< std::string> myString;// Initializing empty vector of string
}
// Copy Constructor
PFAString::PFAString(const PFAString& PFAStringObject)
{
capacity = arr.get_capacity();
size = arr.get_size();
arr = new string[capacity];
for ( int i = 0; i < size; i++)
arr[i] = myString.arr[i];
}
// Destructor
PFAString::~PFAString()
{
delete [] arr ;
}
// Accessor member functions:
int PFAString::get_capacity() const
{
return capacity;
}
int PFAString::get_size() const
{
return size;
}
// Places a string at the back of the array
void PFAString::push_back()
{
vector < PFAString *> arr;
for (int i= 0; i < arr.get_size();i++)
arr.push_back();
}
// Destroys the string at the back of the array
void PFAString::pop_back()
{
vector < PFAString *> arr;
for (int i= 0; i <= arr.get_size();i++)
arr.pop_back();
}
// Grows or shrinks array accordingly
void PFAString::resize()
{
vector < PFAString *> arr;
for (int i= 0; i < arr.get_size();i++)
arr.resize();
}
// Sets the size to zero
void PFAString::empty_array()
{
size = 0 ;
}
// Needs to work as l-value
PFAString& PFAString::operator[] (int index)
{
if (index >= size)
{
cout << "Illegal index in PFAString.\n";
exit(0);
}
return arr[index];
}
// MUST work as l-value
PFAString& PFAString:: operator = (const PFAString& rSide)
{
//if the right side is the same as the left side :
if (this == &rSide)
{
return *this;
}
else
{
capacity = rSide.get_capacity();
size = rSide.get_size() ;
delete [] arr ;
arr = new string[capacity];
for (int i = 0; i < size; i++)
{
arr = rSide.arr[i];
return *this;
}
}
}
// Overloading the relational operators using string::compare():
bool operator == (const PFAString& lSide,const PFAString& rSide)
{
if (!(lSide<rSide) && !(rSide<lSide))
{
return true;
}else
{
return false;
}
// need to iterate the array and make the comparason
//return strcmp(lSide, rSide) == 0;
}
bool operator < (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) < 0;
}
bool operator > (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) > 0;
}
bool operator <= (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) <= 0;
}
bool operator >= (const PFAString& lSide,const PFAString& rSide)
{
return strcmp(lSide, rSide) >= 0;
}
//bool mycomp (char c1, char c2)
//{ return std::tolower(c1)<std::tolower(c2); }
[/#ifndef PFASTRING_HPP
#define PFASTRING_HPP
#include <string>
#include <vector>
class PFAString {
public:
// Default constructor
PFAString();
// Constructor that sets capacity
PFAString(int capValue);
// Constructor that sets capacity and initializes all elements to a specific string
PFAString(int capValue,PFAStringObject);
// Copy Constructor
PFAString(const PFAString& PFAStringObject);
// Destructor
~PFAString();
int get_capacity()const;
int get_size() const;
void push_back(); // Places a string at the back of the array
void pop_back(); // Destroys the string at the back of the array
void resize(); // Grows or shrinks array accordingly
void empty_array(); // Sets the size to zero
PFAString& operator [](int index); // Needs to work as l-value
PFAString& operator = (const PFAString& rSide) ; // MUST work as l-value
//int length() const { return front – end; }
//char& operator[](int i) { return front[i]; } // will work as left_value
//const char& operator[](int i) const { return front[i]; } //// will work as left_value page 366.
friend bool operator == (const PFAString& lSide,const PFAString& rSide);
friend bool operator < (const PFAString& lSide,const PFAString& rSide);
friend bool operator > (const PFAString& lSide,const PFAString& rSide);
friend bool operator <= (const PFAString& lSide,const PFAString& rSide);
friend bool operator >= (const PFAString& lSide,const PFAString& rSide);
private:
std::string *arr; // Dynamic array for string .
int capacity; // Capacity and size are two different things
int size;
//char *a; // thinking about dynamic array for characters in the string.
//void initializeFrom(const char *front, const char *end);
//char *front;
]