private // Data members int myStringSize; char *Arr; ___________________________
ID: 3711919 • Letter: P
Question
private // Data members
int myStringSize;
char *Arr;
______________________________________________________
don't use
_______________________________________________________________________________
Mystring.h
#ifndef myString_H
#define myString_H
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <stdexcept>
//#include <string>
using namespace std;
class myString
{
private:
//string mystr;
//int SIZE;
int myStringSize;
char *Arr;
friend istream & operator >> (istream& input, myString& inputmyString); //can access private data members
friend ostream & operator << (ostream& output, const myString& outputmyString); //friend only in h file
//purpose is to outputstring
public:
myString(const char *charString = "");
char operator [] (int n) const; // will take int locations and return a char from srting
//links to pointer
void operator = (const myString &other); //assignment operator
myString(const myString &other); //copy const - only has one arg. and is passed by ref.
~myString();
void showStructure() const; //print strin
int getLength() const; //returns lenght
bool operator<(const myString& other) const;
bool operator>(const myString& other) const;
bool operator==(const myString& other) const;
myString toUpper(); //convert a -> A
myString toLower(); //conver A->a
void clear();
myString toLower();
};
#endif
test,cpp
Explanation / Answer
I have answered this question earlier. Here is the myString.cpp file according to the .h file. Hope it helps.
#include "myString.h"
#include <cstring>
#include <string>
#include <iostream>
#include <cctype>
using namespace std;
myString::myString(const char *charString) //links to pointer
{
myStringSize = strlen(charString);
Arr = new char[myStringSize+1];
strcpy(Arr, charString);
}
myString::myString(const myString &other) //copy const - only has one arg. and is passed by ref.
{
myStringSize = other.myStringSize;
Arr = new char[myStringSize+1];
strcpy(Arr, other.Arr);
}
void myString::operator = (const myString &other) //assignment operator
{
if(&other != this)
{
if(Arr != NULL)
delete[] Arr;
myStringSize = other.myStringSize;
Arr = new char[myStringSize+1];
strcpy(Arr, other.Arr);
}
}
myString::~myString()//destructor is preceded by ~
{
delete []Arr;
}
int myString::getLength() const //returns length
{
return myStringSize;
}
char myString::operator [ ] (int n) const // will take int locations and return a char from string
{
return Arr[n];
}
void myString::clear() // makes head null
{
if(Arr != NULL)
delete[] Arr;
Arr = NULL;
myStringSize = 0;
}
void myString::showStructure() const //print string
{
cout << *this << endl;
}
myString myString::toUpper() const //convert a -> A
{
myString result(*this);
for(int i = 0; i < result.myStringSize; i++)
result.Arr[i] = std::toupper(result.Arr[i]);
return result;
}
myString myString::toLower() const //conver A->a
{
myString result(*this);
for(int i = 0; i < result.myStringSize; i++)
result.Arr[i] = std::tolower(result.Arr[i]);
return result;
}
bool myString::operator == (const myString& other) const //equal length
{
return myStringSize == other.myStringSize;
}
bool myString::operator < (const myString& other) const //less than length
{
return myStringSize < other.myStringSize;
}
bool myString::operator > (const myString& other) const //greater length
{
return myStringSize > other.myStringSize;
}
istream & operator >> (istream& input, myString& inputmyString) //can access private data members
{
string s;
input >> s;
inputmyString = myString(s.c_str());
return input;
}
ostream & operator << (ostream& output, const myString& outputmyString) //friend only in h file purpose is to outputstring
{
if(outputmyString.Arr != NULL)
output << outputmyString.Arr;
return output;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.