Question
How would I cout s2? .CPP file #include NIUString.h #include using namespace std; //default constructor NIUString::NIUString() { StringSize = 0; StringCapacity = 0; StringArray =
How would I cout s2?
.CPP file
#include "NIUString.h" #include
bool NIUString::empty() const
{ if(StringSize == 0) { return true; } else { return false; } } ostream & operator<<(ostream &leftObj, const NIUString &rightObj) { for(int sub = 0; sub < rightObj.StringSize; sub ++) { leftObj << rightObj.StringArray[sub] << " "; } return leftObj; }
void NIUString::clear() { StringSize = 0; StringCapacity = 0; delete []StringArray; StringArray = NULL; } NIUString::~NIUString() { clear(); }
.h
#ifndef NIUSTRING_H
#define NIUSTRING_H
#include
private: char* StringArray; size_t StringCapacity; size_t StringSize;
public: NIUString(); NIUString(const char*); NIUString(const NIUString&); ~NIUString(); NIUString& operator=(const NIUString&); NIUString& operator=(const char*); size_t capacity()const; size_t size()const; bool empty()const; void clear();
void reserve(size_t); bool operator==(const NIUString&)const; bool operator==(const char*)const; const char& operator[](size_t)const; char& operator[](size_t); }; #endif
TestFile
#include
#include "NIUString.h" using std::cout; using std::endl; int main() { cout << "Testing default constructor ";
const NIUString s1; cout << "s1: " << s1 << endl; cout << "s1 size: " << s1.size() << endl; cout << "s1 capacity: " << s1.capacity() << endl; cout << "s1 is " << ((s1.empty()) ? "empty " : "not empty "); cout << endl;
cout << "Testing second constructor ";
const NIUString s2 = "Hello, world!";
cout << "s2: " << s2 << endl; cout << "s2 size: " << s2.size() << endl; cout << "s2 capacity: " << s2.capacity() << endl; cout << "s2 is " << ((s2.empty()) ? "empty " : "not empty "); cout << endl;
return 0; }
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
What I get with the code above
Testing default constructor
s1: s1 size: 0 s1 capacity: 0 s1 is empty
Testing second constructor
s2: s2 size: 13 s2 capacity: 13 s2 is not empty
What I should get
Testing default constructor
s1: s1 size: 0 s1 capacity: 0 s1 is empty
Testing second constructor
s2: "Hello, World" s2 size: 13 s2 capacity: 13 s2 is not empty
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