Here's my updated code:
Code:
#include <iostream>
using namespace std;
class text {
char *str;
public:
text(char *s = "Default Constructor") { setText(s, false); }
text(const text &s);
~text() { delete [] str; }
void setText(char *s, bool memalloc);
char* getText() { return str; }
int length() { return strlen(str); }
char &letter(int let);
friend ostream& operator<<(ostream &out,const text ob);
friend istream& operator>>(istream &in, text &ob);
text operator=(text obj);
text operator+(text ob);
};
void text::setText(char *s, bool memalloc = true)
{
if(memalloc == true)
{
delete [] str;
}
int length = strlen(s) + 1;
str = new char[length];
if(!length)
{
cout << "Allocation Error";
exit(1);
}
strcpy(str, s);
}
text::text(const text &s)
{
int length = strlen(s.str) + 1;
str = new char[length];
if(!length)
{
cout << "Allocation Error";
exit(1);
}
strcpy(str, s.str);
}
char &text::letter(int let)
{
if(let > 0 && let < length())
{
return str[let];
}
return str[0];
}
ostream& operator<<(ostream &out, const text ob)
{
out << "The String for this object is " << ob.str << endl;
return out;
}
istream& operator>>(istream &in, text &ob)
{
char charTemp[256];
cout << endl;
cout << "Enter string: ";
in >> charTemp;
ob.setText(charTemp);
return in;
}
int main()
{
text one("jason spaceman");
text two;
text three = one;
text ob;
cin >> ob;
cout << one;
cout << two;
cout << three;
cout << endl;
one.letter(0) = 'J';
cout << one;
cout << three;
cout << ob;
return 0;
}
When I output each of the three objects to the screen I also want to output their respective lengths (the length of each string that each object displays). But I'm not sure how to do this. What do I need to add to my cout << statements?