Using C++ create a simple str class that mimic the functionality of the std::str
ID: 3851092 • Letter: U
Question
Using C++
create a simple str class that mimic the functionality of the std::stringclass. To keep things civilized, I have taken the liberty of implementing part of the str class. You are asked to complete the rest of the functions.
Task
Please implement your functions in the str.cpp class.
-------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Meanwhile the str.cpp file looks like
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Explanation / Answer
PROGRAM CODE:
/*
* str.h
*
* Created on: 12-Mar-2017
* Author: kasturi
*/
#ifndef __str_h__
#define __str_h__
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class str
{
protected:
char* _buf; // pointer to the underlying storage
int _n; // size of the buffer
public:
// constructors of various forms
str();
str(int n);
str(char ch);
str(const char* c_str);
// lets not forget the destructor
~str();
// inline functions for finding length info of the str
inline const int& length() const { return _n; }
inline bool is_empty() const { return length() == 0; }
// index operators
char& operator[](int i) { return _buf[i]; }
const char& operator[](int i) const { return _buf[i]; }
// TODO 1. You need to implement the assignment operator
const str& operator=(const str& s)
{
str strNew;
strNew._buf = s._buf;
strNew._n = s._n;
return strNew;
}
// TODO 2. You need to implement the + operator concatenates two str
friend str operator+(const str& a, const str& b);
friend ostream& operator<<(ostream& os, const str& s);
friend istream& operator>>(istream& is, str& s);
};
#endif
/*
* str.cpp
*
* Created on: 12-Mar-2017
* Author: kasturi
*/
#include "str.h"
str::str() : _n(0), _buf(0)
{}
str::str(int n) : _n(n)
{
_buf = new char[_n];
}
str::str(char ch) : _n(1)
{
_buf = new char[_n];
_buf[0] = ch;
}
str::str(const char* c_str)
{
_n = strlen(c_str);
_buf = new char[_n];
for (int i=0; i<_n; ++i) _buf[i]=c_str[i];
}
str::~str()
{
if (_buf) delete [] _buf;
}
ostream& operator<<(ostream& os, const str& s)
{
if (!s.is_empty()) {
for (int i=0; i<s.length(); ++i) {
cout << s[i];
}
}
else cout << "[null str]";
return os;
}
istream& operator>>(istream& is, str& s)
{
char ch;
do {
ch = is.get();
if (ch == ' ' || ch == ' ') break;
s = s + ch;
} while(true);
return is;
}
str operator+(const str& a, const str& b)
{
str strNew(a._n + b._n);
int i=0;
for (i=0; i<a._n; ++i) strNew._buf[i]=a._buf[i];
for(int j=0; j<b._n; j++)
{
strNew._buf[i++] = b._buf[j];
}
strNew._n = a._n + b._n;
return strNew;
}
OUTPUT:
[null str]
Hello
Hello World
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.