Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 using namespace std; //default constructor NIUString::NIUString() { StringSize = 0; StringCapacity = 0; StringArray = NULL; } //intializer constructor NIUString::NIUString(const char* other) { StringSize = strlen(other); StringCapacity = StringSize; if(StringCapacity == 0) { StringArray = NULL; } else { StringArray = new char[StringCapacity]; } for(int i = 0; i != '\0'; i++) { StringArray[i] = other[i]; } } size_t NIUString::capacity() const { return StringCapacity; }

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 #include #include #include #include #include #include using std::ostream; using std::istream; class NIUString { friend ostream & operator<<(ostream&, const NIUString&); friend bool operator==(const char*, const NIUString&);

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional IPhone And IPad Database Application Programming

Authors: Patrick Alessi

1st Edition

0470636173, 978-0470636176

More Books

Students also viewed these Databases questions

Question

Define nudges.

Answered: 1 week ago

Question

What is conservative approach ?

Answered: 1 week ago

Question

Question What is a Roth 401(k) feature?

Answered: 1 week ago