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

Help with my C++ code, Im using visual studio #include <iostream> #include <ioma

ID: 3854480 • Letter: H

Question

Help with my C++ code, Im using visual studio

#include <iostream>

#include <iomanip>

#include <cstdlib> // For the exit function

using namespace std;

IntArray::IntArray(int s)

{

arraySize = s;

aptr = new int[s];

for (int count = 0; count < arraySize; count++)

* (aptr + count) = 0;

}

IntArray::IntArray(const IntArray &obj)

{

arraySize = obj.arraySize;

aptr = new int[arraySize];

for (int count = 0; count < arraySize; count++)

* (aptr + count) = *(obj.aptr + count);

}

IntArray::~IntArray()

{

if (arraySize > 0)

delete[] aptr;

}

void IntArray::subscriptError()

{

cout << "ERROR: Subscript out of range. ";

exit(0);

}

int &IntArray::operator[](const int &sub)

{

if (sub < 0 || sub >= arraySize)

subscriptError();

return aptr[sub];

}

IntArray.h;

// Specification file for the IntArray class

#ifndef INTARRAY_H

#define INTARRAY_H

class IntArray

{

private:

int *aptr; // Pointer to the array

int arraySize; // Holds the array size

void subscriptError(); // Handles invalid subscripts

public:

IntArray(int); // Constructor

IntArray(const IntArray &); // Copy constructor

~IntArray(); // Destructor

int size() const // Returns the array size

{

return arraySize;

}

int &operator[](const int &); // Overloaded [] operator

};

#endif

Here was my qestion

String Class implementation. DO NOT USE/INCLUDE string type (e.g. #include <string>)

Objective: Learning design and use of object oriented programming, operator overloading, constructors (copy constructor, default constructor etc.), destructors, ‘this’ pointer, friendship relation, and static variables.

Input: String or strings (e.g. s1+s2 will return concatenation of strings s1 and s2)

Output: Required operations’ outputs e.g. s1>s2 returns true or false, s1[2] returns third char in string s1, s1.Length() returns length of s1 etc.

Project Description: You are required to create your own String class (Note: String class is different than built in string class) like string type in C++. This class should include following operations:

Separate specifications from implementation. Keep header file and class implementation in separate files.

Can initialize created object with a given string or given instance of the same class (use copy constructor)

Dynamically allocates memory for a given string (e.g. String s(“Joe”); in this case allocates 3 memory spaces for String class instance s)

Returns string length if it is requested (e.g. s.Length())

Clears dynamically allocated memory for String object while destructing (do it in destructor)

Checks boundaries (e.g. if String s size is 10, s[11]= ‘A’; will display error message on screen)

In public section of the String class only include interfaces. All other variables and functions must be in private section.

Overloads + operator so that two string can be concatenated. (e.g. String s1,s2,s3; s3 = s1+s2; ) when only have s1+s2; concatenated string will be included in s1.

Overloads [ ] operator so that s[index] will be possible.

Compares two given objects (e.g. String a,b) a>b or a<b or a==b. To do that overload <,>,== operators

Copies one object to another (e.g. a=b or a=b=c) (overload ‘=’ operator for this purpose)

When required returns number of objects created so far. (e.g. String obj; obj.count()). Obj.count() returns total number of non-empty String objects).

Can get input from the istream class instance (e.g. cin>>s;). Make it friend of the String class.

Can output to the ostream class instance (e.g. cout<<s;). Make it friend of the String class. Total:

You may use below given class definition as a reference. Complete code for IntArray class is given under Sample Codes folder in Blackboard.

class IntArray

{ private:

   int *aptr;                     // Pointer to the array    int arraySize;                 // Holds the array size    void subscriptError();         // Handles invalid subscripts public:

   IntArray(int);                 // Constructor

   IntArray(const IntArray &);    // Copy constructor

   ~IntArray();                   // Destructor

   

   int size() const               // Returns the array size       { return arraySize; }

    int &operator[](const int &); // Overloaded [] operator    friend ostream& operator <<(ostream& stream,IntArray ob); };

Explanation / Answer

simpstring.h

#ifndef SIMPSTRING_H
#define SIMPSTRING_H
#include "iostream.h"
class MyString
{
public:
MyString(char *x =0);
MyString(const MyString &);
~MyString();
MyString& operator =(const MyString&);
int operator ==(const MyString&);
int operator !=(const MyString&);
MyString operator +=(const MyString&);
char* c_str()const;
int length();
MyString readLine(istream&);//read to end of line
istream& read(istream&); //read to white space
ostream& write(ostream &)const;
private:
char *_data;
int _length;
};

MyString operator + (const MyString&,const MyString&);

ostream& operator <<(ostream &out,const MyString &s);

istream& operator >> (istream&, MyString&);
#endif

================================

simpstring.cpp

#ifndef SIMPSTRING_H

#include "simpstring.h"

#endif

#include <string.h>

#include <iostream.h>

MyString::MyString(char *x )

{

if(x)

{

_length = strlen(x);

_data = new char[_length+1];

strcpy(_data,x);

}

else

{

_data = NULL;

_length = 0;

}

}

MyString::MyString(const MyString &x)

{

if(x._length >0)

{

_length = x._length;

_data = new char[_length+1];

strcpy(_data,x._data);

}

else

{

_length=0;

_data = NULL;

}

}

MyString::~MyString()

{

if(_data)

delete [] _data;

}

MyString& MyString::operator =(const MyString &x)

{

if(x._length >0)

{

_length = x._length;

char *temp = new char[_length+1];

strcat(temp,x._data);

if(_data)

delete [] _data;

_data = temp;

}

else

{

_length=0;

if(_data)

delete [] _data;

_data = NULL;

}

return *this;

}

int MyString::operator ==(const MyString &s)

{

return !strcmp(_data,s._data);

}

int MyString::operator !=(const MyString &s)

{

return strcmp(_data,s._data);

}

MyString MyString::operator +=(const MyString &s)

{

_length += s._length;

char *temp = _data;

_data = new char[_length +1];

strcpy(_data,temp);

strcat(_data,s._data);

delete [] temp;

return *this;

}

char* MyString::c_str()const

{

return _data;

}

int MyString::length()

{

return _length;

}

MyString MyString::readLine(istream &in)

{

long int eoln;

char inval[255];

char *ptr;

do

{

in.getline(inval,255);

ptr = strchr(inval,'');

eoln = ptr -inval;

} while (!eoln && !in.eof());

if(_data)

delete [] _data;

_length = strlen(inval);

_data = new char[_length + 1];

strcpy(_data,inval);

return *this;

}

istream& MyString::read(istream &in)

{

char inval[255];

inval[0]='';

char c;

in >> noskipws;

do{

in>>c;

if(c != ' ')

{

char temp[2];

temp[0] = c;

temp[1]='';

strcat(inval,temp);

}

}while(c!=' ');

_length = strlen(inval);

_data = new char[_length + 1];

strcpy(_data,inval);

return in;

}

ostream& MyString::write(ostream &out)const

{

out<<_data;

return out;

}

MyString operator + (const MyString &s1,const MyString &s2)

{

MyString c = s1;

c += s2;

return c;

}

ostream& operator <<(ostream &out,const MyString &s)

{

return s.write(out);

}

istream& operator >> (istream &in, MyString &s)

{

return s.read(in);

}

======================================================================

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote