详细介绍了String类的具体实现,其中包络string类的默认构造函数、复制构造函数的实现;多种运算符的重载定义与操作;多种友元函数的使用。
当然在现如今的C++ STL里面有string类,可以直接使用。当然本文仅仅是通过一个具体的实例阐述C++面向对象程序设计的基本套路,以便于可以较为熟练的使用C++程序设计语言。该例子可以作为学习C++语言的练习实例。
String.h
// This is a class statement of String
#pragma once
#include <iostream>
using std::ostream;
using std::istream;
class String {
public:
String(); //default constructor function
String(const char* ostr); //general constructor function
String(const String&); //copy constructor function
~String();
// Overload operator method
String& operator=(const String&);
String& operator=(const char*);
char& operator[](int i);
const char& operator[](int i) const;
// Overload operator < > = with frined function
friend bool operator<(const String& str1, const String& str2);
friend bool operator>(const String& str1, const String& str2);
friend bool operator==(const String& str1, const String& str2);
friend ostream& operator<<(ostream& os, const String& str);
friend istream& operator>>(istream& is, String& str);
// Static function
static int howManyObject();
private:
char* string_;
int len_;
static int obj_nums_; //The object numbers
static const int ELEMENTS = 100;
};
String.cpp
// This is a class define of String.
#include <cstring>
#include "String.h"
int String::obj_nums_ = 0;
String::String() {
std::cout << "Default constructor func is called. ";
len_ = 1;
string_ = new char[1];
string_[0] = '\0';
obj_nums_++;
std::cout << "Object nums = " << obj_nums_ << std::endl;
}
String::String(const char* basic_str) {
std::cout << "Basic constructor func is called. ";
len_ = std::strlen(basic_str);
string_ = new char[len_ + 1];
strcpy(string_, basic_str);
obj_nums_++;
std::cout << "Object nums = " << obj_nums_ << std::endl;
}
String::String(const String& cp_String) {
std::cout << "Copy constructor func is called. ";
len_ = cp_String.len_;
string_ = new char[len_ + 1];
strcpy(string_, cp_String.string_);
obj_nums_++;
std::cout << "Object nums = " << obj_nums_ << std::endl;
}
String::~String() {
delete [] string_;
--obj_nums_;
std::cout << "Deconstructor function is called. Object_nums = " << obj_nums_ << std::endl;
}
// Overload operator method, e.g. assign, [] etc.
String& String::operator=(const String& org_String) {
std::cout << "Operator assign (object assign) method is called.";
if (this == &org_String) {
return *this;
}
delete [] string_;
len_ = org_String.len_;
string_ = new char[len_ + 1];
strcpy(string_, org_String.string_);
return *this;
}
String& String::operator=(const char* org_char) {
std::cout << "Operator assign (char) method is called." << std::endl;
delete [] string_;
len_ = strlen(org_char);
string_ = new char[len_ + 1];
strcpy(string_, org_char);
return *this;
}
char& String::operator[](int i) {
std::cout << "Operator [] read & write method is called." << std::endl;
return string_[i];
}
const char& String::operator[](int i) const {
std::cout << "Operator [] only read method is called." << std::endl;
return string_[i];
}
// Overload operator in this class
bool operator<(const String& str1, const String& str2) {
std::cout << "Friend overload < operator is called." << std::endl;
return strcmp(str1.string_, str2.string_) < 0;
}
bool operator>(const String& str1, const String& str2) {
std::cout << "Friend overload > operator is called." << std::endl;
return str2 < str1;
}
bool operator==(const String& str1, const String& str2) {
std::cout << "Friend overload == operator is called." << std::endl;
return strcmp(str1.string_, str2.string_) == 0;
}
ostream& operator<<(ostream& os, const String& str) {
std::cout << "Friend overload ostream << operator is called." << std::endl;
os << str.string_;
return os;
}
istream& operator>>(istream& is, String& str) {
std::cout << "Friend overload istream >> operator is called." << std::endl;
char temp[String::ELEMENTS];
is.get(temp, String::ELEMENTS);
if (is) {
str = temp;
}
while (is && is.get() != '\n') {
continue;
}
return is;
}
int String::howManyObject() {
std::cout << "howManyObject function is called.";
return obj_nums_;
std::cout << "Here has object: " << obj_nums_ << std::endl;
}
Main.cpp
#include "String.h"
using namespace std;
const int SIZE = 4;
const int MAXLEN = 100;
int main() {
String str;
cout << "Input a string: " << endl;
cin >> str;
cout << str;
String saying[SIZE];
cout << "*1.-----------------------------" << endl;
char temp[MAXLEN];
int i;
for (i = 0; i < SIZE; i++) {
cout << i + 1 << ": ";
cin.get(temp, MAXLEN);
while (cin && cin.get() != '\n') {
continue;
}
if (!cin && temp[0] == '\0') {
break;
}
else {
saying[i] = temp;
}
}
cout << "*2.-----------------------------" << endl;
int total = i;
if (total > 0) {
cout << "Here is a saying:\n";
for (int i = 0; i < total; i++) {
cout << saying[i][0] << ": " << saying[i] << endl;
}
cout << "length = " << saying[1] << endl;
}
cout << "*3.-----------------------------" << endl;
cout << "Object nums: " << String::howManyObject() << endl;
if (saying[0] < saying[1]) {
cout << "From small to big." << endl;
}
else {
cout << "From big to small." << endl;
}
cout << "*4.-----------------------------" << endl;
String obj = saying[2];
cout << "OBJ: " << obj << endl;
return 0;
}