string1.h
//
// Created by lance on 10/16/18.
//
#ifndef CPP_PRIMER_STRING1_H
#define CPP_PRIMER_STRING1_H
#include <iostream>
using std::ostream;
using std::istream;
class String {
private:
// pointer to string
char *str;
// length of string
int len;
// number of objects
static int numStrings;
// cin input limit
static const int CIN_LIM = 80;
public:
// constructors
String();
String(const char *s);
String(const String &st);
~String();
// other methods
int length() const {
return this->len;
}
void showAddr();
// overloaded operator methods
String &operator=(const String &st);
String &operator=(const char *s);
char &operator[](int i);
const char &operator[](int i) const;
// overloaded operator friends
friend bool operator<(const String &st1, const String &st2);
friend bool operator>(const String &st1, const String &st2);
friend bool operator==(const String &st1, const String &st2);
friend ostream &operator<<(ostream &os, const String &st);
friend istream &operator>>(istream &is, String &st);
// static function
static int howMany();
};
#endif //CPP_PRIMER_STRING1_H
string1.cpp
//
// Created by lance on 10/16/18.
//
#include <cstring>
#include "string1.h"
using std::cout;
using std::endl;
// initializing static class member
int String::numStrings = 0;
int String::howMany() {
return numStrings;
}
void String::showAddr() {
cout << "memory addr: " << this << endl;
}
// class methods
String::String() {
cout << "========call String()" << endl;
this->len = 4;
str = new char[1];
str[0] = '\0';
numStrings++;
}
String::String(const char *s) {
cout << "========call String(const char *s)" << endl;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, s);
numStrings++;
}
String::String(const String &st) {
cout << "========call String(const String &st)" << endl;
numStrings++;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
}
String::~String() {
--numStrings;
delete[] str;
}
// overloaded operator methods
// assign a String to a String
String &String::operator=(const String &st) {
cout << "========call operator=(const String &st)" << endl;
if (this == &st) {
return *this;
} else {
delete[] str;
len = st.len;
str = new char[len + 1];
std::strcpy(str, st.str);
return *this;
}
}
// assign a C string to a String
String &String::operator=(const char *s) {
cout << "========call operator=(const char *s)" << endl;
delete[] str;
len = std::strlen(s);
str = new char[len + 1];
std::strcpy(str, 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::CIN_LIM];
is.get(temp, String::CIN_LIM);
if (is) {
st = temp;
}
while (is && is.get() != '\n') {
continue;
}
return is;
}
vegnews.cpp
//
// Created by lance on 10/17/18.
//
#include <iostream>
#include "string1.h"
using std::cout;
using std::endl;
void callme1(String &str);
void callme2(String str);
int main(void) {
cout << "Starting an inner block." << endl;
String headline1("Celery Stalks at Midnight");
cout << "memory addr: " << &headline1 << endl;
headline1.showAddr();
callme1(headline1);
callme2(headline1);
String headlineCp1 = headline1;
callme1(headlineCp1);
String headlineCp2;
headlineCp2 = headline1;
callme1(headlineCp2);
String headline2 = "Lettuce Prey";
callme1(headline2);
String sports;
sports = "Spinach Leaves Bowl for Dollars";
callme1(sports);
return 0;
}
void callme1(String &str) {
cout << "String passed by reference: " << endl;
cout << " \"" << str << "\"" << endl;
str.showAddr();
}
void callme2(String str) {
cout << "String passed by value: " << endl;
cout << " \"" << str << "\"" << endl;
str.showAddr();
}
特别注意
String headline2 = "Lettuce Prey";
和
String sports;
sports = "Spinach Leaves Bowl for Dollars";
是完全不同的。