?? newstringimp.cpp
字號:
//Implementation file newString.cpp
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cassert>
#include "newString.h"
using namespace std;
//constructor: conversion from the char string to newString
newString::newString(const char *str)
{
strLength = strlen(str);
strPtr = new char[strLength+1]; //allocate memory to store
//the char string
assert(strPtr != NULL);
strcpy(strPtr,str); //copy the string into strPtr
}
//default constructor to store the null string
newString::newString()
{
strLength = 0;
strPtr = new char[1];
assert(strPtr != NULL);
strcpy(strPtr,"");
}
newString::newString(const newString& rightStr) //copy constructor
{
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, rightStr.strPtr);
}
newString::~newString() //destructor
{
delete [] strPtr;
}
//Overload the assignment operator
const newString& newString::operator=(const newString& rightStr)
{
if(this != &rightStr) //avoid self-copy
{
delete [] strPtr;
strLength = rightStr.strLength;
strPtr = new char[strLength + 1];
assert(strPtr != NULL);
strcpy(strPtr, rightStr.strPtr);
}
return *this;
}
char& newString::operator[] (int index)
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
const char& newString::operator[](int index) const
{
assert(0 <= index && index < strLength);
return strPtr[index];
}
//Overloads the relational operators
bool newString::operator==(const newString& rightStr) const
{
return(strcmp(strPtr, rightStr.strPtr) == 0);
}
bool newString::operator<(const newString& rightStr) const
{
return(strcmp(strPtr, rightStr.strPtr) < 0);
}
bool newString::operator<=(const newString& rightStr) const
{
return(strcmp(strPtr, rightStr.strPtr) <= 0);
}
bool newString::operator>(const newString& rightStr) const
{
return(strcmp(strPtr, rightStr.strPtr) > 0);
}
bool newString::operator>=(const newString& rightStr) const
{
return(strcmp(strPtr, rightStr.strPtr) >= 0);
}
bool newString::operator!=(const newString& rightStr) const
{
return(strcmp(strPtr, rightStr.strPtr) != 0);
}
//Overloads the stream insertion operator <<
ostream& operator<<(ostream& osObject, const newString& str)
{
osObject<<str.strPtr;
return osObject;
}
//Overload the stream extraction operator >>
istream& operator>>(istream& isObject, newString& str)
{
char temp[81];
isObject>>setw(80)>>temp;
str = temp;
return isObject;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -