C++ programming (Object Oriented) Please Help! Question 3 130 points) complete t
ID: 3714199 • Letter: C
Question
C++ programming (Object Oriented)
Please Help!
Question 3 130 points) complete the following code which declares a String An object of that class, would store the c (C-string) in a dynamically allocated array as well In solving this question, you may use the e-string library that contains ihe strlen(), strepy, strcat (), stremp (..etc. haracters of a standard Ch+ character strin as storing the length of that stri class String overloading"" operator to perform the concatenation of two strings String &operator;( ' public: // default constructor with parameter that defaults to NULL) String default constructor String( ~String ( ); //destructor ?? getLength( ); // returns the string length bool operator ): copy constructor const; private: int // number of characters in the string. pointer to the array that stores the st // characters. char 1. complete the above code which declares a String class 2. Implement the getLength functionExplanation / Answer
#include<iostream>
#include<cstring>
using namespace std;
class String
{
char *value;
int len;
public:
String()
{
len=0;
value=0;
}
~String()
{
}
//String From Array
String(char *s)
{
len=strlen(s);
value=new char[len+1];
strcpy(value,s);
}
//String using Copy Constructor
String(String & s)
{
len=s.len;
value=new char[len+1];
strcpy(value,s.value);
}
//str3=str1+str2
friend String operator+(String obj1,String obj2)
{
String obj3;
obj3.len=obj1.len+obj2.len;
obj3.value=new char[obj3.len+1];
strcpy(obj3.value,obj1.value);
strcat(obj3.value,obj2.value);
return obj3;
}
int getLength (String & str)
{
return str.len;
}
void display()
{
if(len==0)
{
cout<<" String is Empty ";
}
else
{
cout<<" The result is:"<<value<<" ";
}
}
};
int main()
{
String str1("xxxyyyzzz");
String str2("abc");
String str4("abfdshgkhgkdsgk");
cout << "string Length of string4 = " << str4.getLength(str4) << endl;
str1.display();
str2.display();
String str3;
str3=str1+str2;
str3.display();
return 0;
}
/**************** OUTPUT OF PROGRAM *****************
string Length of string4 = 15
The result is:xxxyyyzzz
The result is:abc
The result is:xxxyyyzzzabc
***************************************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.