Question
The goals of this assignment are: Create a user-defined string class Implement constructors, accessors, and mutators In this assignment you will create a custom string
The goals of this assignment are:
- Create a user-defined string class
- Implement constructors, accessors, and mutators
In this assignment you will create a custom string class, similar to the C++ STL string class you used in CS 161. Your version of string will not be as comprehensive or sophisticated as the C++ STL version, but your goal is to write a version that you can use in later assignments if you decide you are tired of using C-strings. Of course, you'll still be using C-strings, but you'll hide that detail in your string class!
Since your string class will use char arrays to hold the data, and since you do not (yet) know how to change the size of an array, some limitations will exist. In particular, you can assume that the maximum length of a string is 256 bytes. If the user tries to store a longer string in one of your string objects, simply truncate the input and store only the first 256 bytes.
In this assignment you'll implement the following class called xstring. Here is the xstring.h file you will use:
#pragma once #include#define max_length 256 class xstring { public: // Constructors xstring(); xstring(const char *); // Overloaded operators xstring & operator = (const xstring &); friend std::ostream & operator << (std::ostream &, const xstring &); private: char data[max_length + 1]; int length; };
The class members you must implement are:
- xstring() - This is the default constructor, and should initialize length to zero, and set all elements of the data array to '\0'.
- xstring(const char *) - This is the parameterized constructor. Don't worry about the pointer notation, all it means is that the constructor accepts a C-string as a parameter. You'll need to copy the C-string to the data array and set the appropriate length value.
- xstring & operator = (const xstring &); - This is the overloaded copy assignment operator. It should copy the data string and length from its parameter.
- std::ostream & operator << (std::ostream &, const xstring &) - This is the overloaded stream insertion operator. It should output the string in the data array.
-
existing files
-
main.cpp
#include "main.h" using namespace std;int main() { xstring var_a; xstring var_b("Hello World"); cout << "var_a = \"" << var_a << "\"" << endl; cout << "var_b = \"" << var_b << "\"" << endl; var_a = var_b; cout << "var_a = \"" << var_a << "\"" << endl; cout << "var_b = \"" << var_b << "\"" << endl;
return 0; }
-
main.h
-
#pragma once #include "xstring.h" #include
-
-
makefile
-
# Log file name LOGC=log_compile LOGL=log_link
# Executable name EXEC=main
# Create the course code file list. Expansion happens now. SOURCES=$(wildcard *.cpp) HEADERS=$(wildcard *.h)
# Create object file variable. Expansion happens when variable is referenced OBJECTS= *.o
# Compiler options CC=g++ CFLAG=-v -c -g -std=c++11 -Wall
# Make targets all: $(EXEC)
$(EXEC): $(SOURCES) $(HEADERS) @rm -f $(LOGC) $(LOGL) @$(CC) $(CFLAG) $(SOURCES) &>>$(LOGC) @$(CC) -o main $(OBJECTS) &>>$(LOGL)
compile: $(SOURCES) $(HEADERS) @rm -f $(LOGC) @$(CC) $(CFLAG) $(SOURCES) &>> $(LOGC)
clean: @rm -f $(EXEC) $(LOGC) $(LOGL) $(patsubst %.cpp,%.o,$(wildcard *.cpp))
-
-
Do not change any of the existing files!
-
In the project directory create the xstring.cpp file that implements the xstring class defined in the xstring.h file, and described above. In other words, implement the xstring class.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started