Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi I need help finishing the header file. The main is good and should not be tou

ID: 3638231 • Letter: H

Question

Hi I need help finishing the header file. The main is good and should not be touched.
I just need to add anything necessary to make it work with the main.
In the header fill I did a full comment out of what I added. feel free to use those or change them in anyway if you like. If it's not commented out then either it was given or needs to be used somehow.


So please look at my header file and help me add anything necessary to make it work with my main. Also please include any and all comments to help with understanding.

header file below:
**************************************************************************************************
//String.h
//#include "String.h"
#include <string.h>
class String
{
int size;
char * buffer;
public:
String();
String(const String &);
String(const char *);
~String();


//char& operator[] (const int );
//const String& operator= (const String&);
//bool operator== (const String&);
//int length();

};


String::String()
{
size=0;
buffer=0;
}

String::String(const char*p)
{
size = strlen (p);
buffer = new char [size];
for (int i=0; i < size; i++)
buffer [i] = p[i];
}



String::String (const String& source)
{
size = source.size;
buffer = new char[size]; //allocate memory for characters
strcpy(buffer, source.buffer);
for (int i = 0; i < size; i++)
buffer[i] = source.buffer[i];

}


String String::operator = (const char *p)
{
delete[] buffer; // only for destructor
size = strln(p);
buffer = new char [];
for(int i = 0; i < size; i++)
buffer [i] = p[i];
}



/*
int String::length()
{
return size;
}




char& String::operator[] (const int index)
{
return buffer[index];
}

const String& String::operator= (const String& other)
{
size = other.size;buffer = new char[size]; //allocate memory for characters
strcpy(buffer, other.buffer);
for (int i = 0; i < size; i++)
buffer[i] = other.buffer[i];
memcpy (buffer, other.buffer, size);

return other;
}

bool String::operator== (const String& other)
{
if (buffer == other.buffer)
return;

}
*/


Main below:
****************************************************************************************************
//main.cpp
#include <iostream>
#include <cassert>
#include "String.h"

using namespace std;

int main()
{
String s1; // s1 == ""
assert(s1.length() == 0);

String s2("hi"); // s2 == "hi"
assert(s2.length() == 2);

String s3(s2); // s3 == "hi"
assert(s3.length() == 2);
assert(s3[0] == 'h');
assert(s3[1] == 'i');

s1 = s2; // s1 == "hi"
assert(s1 == s2);

s3 = "bye"; // s3 == "bye"
assert(s3.length() == 3);
assert(s3[0] == 'b');
assert(s3[1] == 'y');
assert(s3[2] == 'e');

s1 += "re"; // s1 == "hire"
assert(s1 == "hire");

s1 += "d"; // s1 == "hired"
assert(not (s1 == "hire"));

cout << "SUCCESS" << endl;

system("PAUSE");
}

Explanation / Answer

The for statement makes a common type of while loop easier to write. Many while loops have the general form: initialization while ( continuation-condition ) { statements update } For example, consider this example, copied from an example in Section 2: years = 0; // initialize the variable years while ( years < 5 ) { // condition for continuing loop interest = principal * rate; // principal += interest; // do three statements System.out.println(principal); // years++; // update the value of the variable, years } This loop can be written as the following equivalent for statement: for ( years = 0; years < 5; years++ ) { interest = principal * rate; principal += interest; System.out.println(principal); }