This is the code that I have so far for this asgn. #include<iostream> using name
ID: 3620374 • Letter: T
Question
This is the code that I have so far for this asgn.#include<iostream>
using namespace std;
class String1 {
private:
char str[80];
public:
// Constructors
String1();
String1(char *);
// Fill a character buffer argument
void GetString(char *);
// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);
friend String1 operator+(char *, String1 &);
// Assignment Operators
// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1 operator=( String1 &);
String1 operator=( char *);
// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
};
ostream & operator<<(ostream & stream, sIn &)
{
stream << sIn.str;
return stream;
}
String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
String1::operator =(String1 & sIn)
{
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
String1 temp(this->str);
return temp;// string with no name
}
void String1::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String1::String1()
{
str[0] = 0;
}
String1::String1(char * strIn)
{
strcpy(str, strIn);
}
void main()
{
String1 S("Bob");
char buffer[20];
S.GetString(buffer);
cout << buffer;
//String1 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
// String1 S1, S2("Bob");
//cout << S1<< S1;
}
I need this:without a pointer
class String1 {
private:
char str[80];
public:
// Constructors
String1();
String1(char *);
// Fill a character buffer argument
void GetString(char *);
// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);
friend String1 operator+(char *, String1 &);
// Assignment Operators
// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1 operator=( String1 &);
String1 operator=( char *);
// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
};
Be sure to fully test all the functions in main. Main should have lines of the following form, plus more to test the constructor calls, output, etc.
"Dan " + S; // calls friend operator+(char *, String1 &);
S + "Bob"; // calls operator+(char *);
S + S; // calls operator+(String1 &);
S = S3; // calls operator=( String1 &);
S = "Bob"; // calls operator=( char *);
cout << S // calls friend ostream &operator<<(ostream &, const String1 &);
Explanation / Answer
#include<iostream>
using namespace std;
class String1 {
private:
char str[80];
public:
// Constructors
String1();
String1(char *);
// Fill a character buffer argument
void GetString(char *);
// Concatenation Operators
String1 operator+(String1 &);
String1 operator+(char *);
friend String1 operator+(char *, String1 &);
// Assignment Operators
// would work without this cuz we do not have
// any pointer members but do anyway for practice
String1& operator=( String1 &);
String1& operator=( char *);
// Console Stream Output
friend ostream &operator<<(ostream &, const String1 &);
};
//-------------------------------------
ostream & operator<<(ostream & stream, const String1 &sIn)
{
stream << sIn.str;
return stream;
}
//-------------------------------------
String1 String1::operator+(String1 & sIn)
{
String1 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
//-------------------------------------
String1 String1::operator+(char * strIn)
{
String1 temp(this->str);
strcat(temp.str, strIn);
return temp;
}
//-------------------------------------
String1 operator+(char * strIn, String1 & sIn)
{
String1 temp(strIn);
strcat(temp.str, sIn.str);
return temp;
}
//-------------------------------------
String1& String1::operator =(String1 & sIn)
{
if (this != &sIn)
{
strcpy(str, sIn.str);
}
return *this;
}
//-------------------------------------
String1& String1::operator =(char * strIn)
{
strcpy(str, strIn);
return *this;
}
//-------------------------------------
void String1::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
//-------------------------------------
String1::String1()
{
str[0] = 0;
}
//-------------------------------------
String1::String1(char * strIn)
{
strcpy(str, strIn);
}
//-------------------------------------
//-------------------------------------
void main()
{
String1 S("Bob");
char buffer[20];
S.GetString(buffer);
cout << buffer << endl;
//String1 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
// String1 S1, S2("Bob");
//cout << S1 << S1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.