In this activity, you will implement your own version of a string class in C++. This exercise is designed to give you hands-on experience with:
==
and <<
)In Java and Python, strings are managed objects that handle memory automatically. In C++, while we have std::string
, it's important to understand how strings could be implemented behind the scenes — especially in a language where you are responsible for memory management.
This assignment will introduce you to the Rule of Three, which states that if your class allocates dynamic memory, you must explicitly define:
You will implement a class called SimpleString
that holds a dynamically allocated null-terminated C-style string (i.e., a char*
).
You will:
new[]
and delete[]
SimpleString.h
— class declaration with TODO commentsSimpleString.cpp
— function stubs with commentsmain.cpp
— a test driver using Check()
for test output and validationMakefile
— a file for building the projectclass SimpleString {
public:
SimpleString(); // Default constructor
SimpleString(const char* text); // Construct from a C-string
SimpleString(const SimpleString& other); // Copy constructor
SimpleString& operator=(const SimpleString& other); // Copy assignment
bool operator==(const SimpleString& other) const; // Equality
~SimpleString(); // Destructor
int Length() const; // Get string length
friend std::ostream& operator<<(std::ostream& os, const SimpleString& str); // Stream output
private:
char* data;
};
You will test your implementation using the provided main.cpp
. This file uses a custom Check()
function to provide [PASS]/[FAIL] results for:
==
operator correctness<<
You are encouraged to add your own additional tests as needed!
std::string
in your implementation.g++ -std=c++11
or later.Destructor called
, Assignment operator called
, etc., printed to the terminal to help trace program behavior.Submit via Gitlab in a directory called SimpleString
Ensure your submission compiles and runs cleanly with no memory leaks.