版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42787086/article/details/84484150
string2.h
// string1.h -- fixed and augmented string class definition
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
#include<cctype>
using std::ostream;
using std::istream;
class String
{
private:
char * str; // pointer to string
int len; // length of string
static int num_strings; // number of objects
static const int CINLIM = 80; // cin input limit
public:
// constructors and other methods
String(const char * s); // constructor
String(); // default constructor
String(const String &); // copy constructor
// Newly added: Stringup(),Stringlow()
void stringup();
void stringlow();
int has(char ch) const;
~String(); // destructor
int length () const { return len; }
// overloaded operator methods
String & operator=(const String &);
String & operator=(const char *);
char & operator[](int i);
const char & operator[](int i) const;
// overloaded operator friends
friend bool operator<(const String &st, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st, const String &st2);
friend ostream & operator<<(ostream & os, const String & st);
friend istream & operator>>(istream & is, String & st);
// overload operator+
friend String operator+(const String&str1, const String& str2);
friend String operator+(const char* s, const String& str2);
// static function
static int HowMany();
};
#endif
string2.cpp
// string1.cpp -- String class methods
#include <cstring> // string.h for some
#include "string2.h" // includes <iostream>
#include<cctype>
using std::cin;
using std::cout;
// initializing static class member
int String::num_strings = 0;
// static method
int String::HowMany()
{
return num_strings;
}
// class methods
String::String(const char * s) // construct String from C string
{
len = std::strlen(s); // set size
str = new char[len + 1]; // allot storage
strcpy_s(str,len+1,s); // initialize pointer
num_strings++; // set object count
}
String::String() // default constructor
{
len = 4;
str = new char[1];
str[0] = '\0'; // default string
num_strings++;
}
String::String(const String & st)
{
num_strings++; // handle static member update
len = st.len; // same length
str = new char [len + 1]; // allot space
strcpy_s(str, len+1,st.str); // copy string to new location
}
String::~String() // necessary destructor
{
--num_strings; // required
delete [] str; // required
}
// overloaded operator methods
// assign a String to a String
String & String::operator=(const String & st)
{
if (this == &st)
return *this;
delete [] str;
len = st.len;
str = new char[len + 1];
strcpy_s(str,st.len+1, st.str);
return *this;
}
// assign a C string to a String
String & String::operator=(const char * s)
{
delete [] str;
len = std::strlen(s);
str = new char[len + 1];
strcpy_s(str,len+1, s);
return *this;
}
// read-write char access for non-const String
char & String::operator[](int i)
{
return str[i];
}
// read-only char access for const String
const char & String::operator[](int i) const
{
return str[i];
}
// overloaded operator friends
bool operator<(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) < 0);
}
bool operator>(const String &st1, const String &st2)
{
return st2 < st1;
}
bool operator==(const String &st1, const String &st2)
{
return (std::strcmp(st1.str, st2.str) == 0);
}
// simple String output
ostream & operator<<(ostream & os, const String & st)
{
os << st.str;
return os;
}
// quick and dirty String input
istream & operator>>(istream & is, String & st)
{
char temp[String::CINLIM];
is.get(temp, String::CINLIM);
if (is)
st = temp;
while (is && is.get() != '\n')
continue;
return is;
}
/********************** Newly added ***********************/
void String::stringup()
{
for (int i = 0; i < len; i++)
{
if (islower(str[i]))
str[i] = toupper(str[i]);
}
}
void String::stringlow()
{
for (int i = 0; i < len; i++)
{
if (isupper(str[i]))
str[i] = tolower(str[i]);
}
}
int String::has(char ch) const
{
int count = 0;
for (int i = 0; i < len; i++)
{
if (str[i] == ch)
count++;
}
return count;
}
// Two approaches
// 1.use loop to copy value from str1 and str2 one by one
// 2.use strcpy_s() and strcat_s() to copy at one go
// Approach 1
String operator+(const String&str1, const String& str2)
{
int new_len = str1.len + str2.len;
char*ps = new char[new_len + 1];
for (int i = 0; i < str1.len; i++)
ps[i] = str1[i];
for (int i = str1.len; i < new_len + 1; i++)
ps[i] = str2[i - str1.len];
String temp(ps);
delete[]ps;
return temp;
}
/*
// Approach 2
String operator+(const String&str1, const String& str2)
{
int new_len = str1.len + str2.len;
char*ps = new char[new_len + 1];
// end element '\0' copied
strcpy_s(ps, new_len + 1, str1.str);
// str1's end element '\0' covered and str2's '\0' copied
strcat_s(ps, new_len + 1, str2.str);
String temp(ps);
delete[]ps;
return temp;
}
*/
String operator+(const char* s, const String& str2)
{
String temp(s);
return temp + str2;
}
main
// pe12_2.cpp
#include <iostream>
using namespace std;
#include "string2.h"
int main()
{
String s1(" and I am a C++ student.");
String s2 = "Please enter your name: ";
String s3;
cout << s2; // overloaded << operator
cin >> s3; // overloaded >> operator
s2 = "My name is " + s3; // overloaded =, + operators
cout << s2 << ".\n";
s2 = s2 + s1;
s2.stringup(); // converts string to uppercase
cout << "The string\n" << s2 << "\ncontains " << s2.has('A')
<< " 'A' characters in it.\n";
s1 = "red"; // String(const char *),
// then String & operator=(const String&)
String rgb[3] = { String(s1), String("green"), String("blue") };
cout << "Enter the name of a primary color for mixing light: ";
String ans;
bool success = false;
while (cin >> ans)
{
ans.stringlow(); // converts string to lowercase
for (int i = 0; i < 3; i++)
{
if (ans == rgb[i]) // overloaded == operator
{
cout << "That's right!\n";
success = true;
break;
}
}
if (success)
break;
else
cout << "Try again!\n";
}
cout << "Bye\n";
cin.get();
return 0;
}