Write a console program that implements this class. In main, write a “driver pro
ID: 3620420 • Letter: W
Question
Write a console program that implements this class. In main, write a “driver program” that fully tests the functionality of the class.class String2 {
private:
char * str;
public:
// Constructors/Destructor
String2();
String2(char *);
~String2();
String2(const String2 &); // copy constructor
// Fill a character buffer argument
void GetString(char *);
// Concatenation Operators
String2 operator+(String2 &);
String2 operator+(char *);
friend String2 operator+(char *, String2 &);
// Assignment Operators
// we NEED this cuz we have a pointer now
String2 operator=(String2 &);
String2 operator=(char *);
// Console Stream Output
friend ostream & operator<<(ostream & stream, String2 &);
};
/*
handy function for debugging the copy constructor
get this working first to test the copy constructor
before you write the functions that return String2 copies
*/
void testCopy(String2 S)
{
// the copy constructor will be called here for S
// you can look at S's members and see if they are cool
// the heap pointer should be different, but the heap data
// should be the same
cout << S;
}
I have some code but I don't think I did it right. I did it without a pointer;
#include<iostream>
using namespace std;
class String2
{
private:
char str[80];
public:
String2();
//copy constructor
String2(const String2 &);
String2(char *);
~String2();
void GetString(char *);
//concatenation
String2 operator+(String2 &);
//assignment
String2 operator=(String2 &);
friend ostream & operator<<(ostream & stream, String2 &);
};
String2::String2(const String2 & sIn);
{
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
}
ostream & operator<<(ostream & stream, String2 &)
{
stream << sIn.str;
return stream;
}
String2 String2::operator+(String2 &sIn)
{
String2 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
String2 &String2::operator =(String2 &sIn)
{
//make things equal
if (str) delete str;
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
String2 temp(this->str);
return temp;// string with no name
}
void String2::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String2::String2()
{
str[0] = 0;
}
String2::String2(char * strIn)
{
//get memory
str = new char[strlen(strIn)+1];
strcpy(str, strIn);
}
String2::~String2()
{
delete str; //give stuff back
}
void main()
{
String2 S("Bob");
String2 S2("Sam");
S2 = S;
//char buffer[20];
//S.GetString(buffer);
//cout << buffer;
//String2 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
//String2 S1, S2("Bob");
//cout << S1<< S1;
}
Explanation / Answer
This should give you a good start: (Note the red (modifications/removals) / green (additions) parts)... It seems to run now and i think you can see were you were having problems.... good luckThe code you put up: } The debugged code #include <iostream>
using namespace std;
class String2
{
private:
char *str; //you said you wanted a pointer?
public:
String2();
//copy constructor
String2(const String2 &);
String2(char *);
~String2();
void GetString(char *);
//concatenation
String2 operator+(String2 &);
//assignment
String2 operator=(String2 &);
friend ostream & operator<<(ostream & stream, String2 &);
};
String2::String2(const String2 & sIn)
{
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
}
ostream & operator<<(ostream & stream, String2 & sIn)
{
stream << sIn.str;
return stream;
}
String2 String2::operator+(String2 &sIn)
{
String2 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
String2 String2::operator =(String2 &sIn)
{
//make things equal
if (str) delete str;
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
String2 temp(this->str);
return temp;// string with no name
}
void String2::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String2::String2()
{
str[0] = 0;
}
String2::String2(char * strIn)
{
//get memory
str = new char[strlen(strIn)+1];
strcpy(str, strIn);
}
String2::~String2()
{
delete str; //give stuff back
}
void main()
{
String2 S("Bob");
String2 S2("Sam");
S2 = S;
//char buffer[20];
//S.GetString(buffer);
//cout << buffer;
//String2 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
//String2 S1, S2("Bob");
cout << S2;
}
using namespace std;
class String2
{
private:
char *str; //you said you wanted a pointer?
public:
String2();
//copy constructor
String2(const String2 &);
String2(char *);
~String2();
void GetString(char *);
//concatenation
String2 operator+(String2 &);
//assignment
String2 operator=(String2 &);
friend ostream & operator<<(ostream & stream, String2 &);
};
String2::String2(const String2 & sIn)
{
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
}
ostream & operator<<(ostream & stream, String2 & sIn)
{
stream << sIn.str;
return stream;
}
String2 String2::operator+(String2 &sIn)
{
String2 temp(this->str);
strcat(temp.str, sIn.str);
return temp;
}
String2 String2::operator =(String2 &sIn)
{
//make things equal
if (str) delete str;
str = new char[strlen(sIn.str) + 1];
strcpy(str, sIn.str);
String2 temp(this->str);
return temp;// string with no name
}
void String2::GetString(char * strBufPtr)
{
strcpy(strBufPtr, str);
}
String2::String2()
{
str[0] = 0;
}
String2::String2(char * strIn)
{
//get memory
str = new char[strlen(strIn)+1];
strcpy(str, strIn);
}
String2::~String2()
{
delete str; //give stuff back
}
void main()
{
String2 S("Bob");
String2 S2("Sam");
S2 = S;
//char buffer[20];
//S.GetString(buffer);
//cout << buffer;
//String2 S1, S2("Bob"), S3("Jones");
//S1 = S2;
//S1 = S2 + S3("Bob"), S3("Jones");
//String2 S1, S2("Bob");
cout << S2;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.