#C++PrimerPlus# Chapter12_Exersice4&5&6_stringV1

续上题改进String类,修正完善之前定义的构造析构函数与赋值运算符函数,增加对关系运算符和对访问运算符的重载,以及其他一些改动。

String1类

私有部分:1,数据成员 char* 指向字符串起点的指针;

     2,int 记录字符串长度的变量(不含\0);

     3,静态类成员变量 static int 记录类对象的总数;

     4,常量 字符串长度限制;

公有部分:1,构造函数与析构函数;

     2,返回字符串长度的函数;

     3,重载运算符函数;

     4,重载关系运算符 输入输出运算符的友元函数;

     5,静态类成员函数 用以返回类实例总个数的值。

程序清单如下:


// string1.h
#ifndef STRING1_H_
#define STRING1_H_
#include <iostream>
  using std::ostream;
  using std::istream;

class String
{
private:
  char* str; // 指向字符串的指针
  int len; // 字符串的长度
  static int numStrings; // 对象总数
  static const int CINLIM = 80; // 成员常量 输入长度限制
public:
  // 构造和析构函数
  String(); // 默认构造函数
  String(const String& s); // 复制构造函数(deep copy)
  String(const char* s); // 构造函数
  ~String(); // 析构函数
  // 一般成员函数
  int length() const {return len;}
  // 重载运算符函数
  String& operator=(const String& s); // 重载赋值运算符
  String& operator=(const char* s); // 同上
  char& operator[](int i); // 重载访问运算符
  const char& operator[](int i) const; // 重载访问运算符的const版本
  // 友元函数
  friend bool operator<(const String& a, const String& b);
  friend bool operator>(const String& a, const String& b);
  friend bool operator==(const String& a, const String& b); // 重载关系运算符
  friend ostream& operator<<(ostream& os, const String& b);
  friend istream& operator>>(istream& in, String& b); // 重载输入输出运算符
  // 静态成员函数
  static int HowMany(); // 返回对象总数numStrings
};

#endif


// string1.cpp
#include "string1.h"
#include <cstring>
using std::cout;
using std::cin;

// 初始化静态类成员
int String::numStrings = 0;

// 定义静态成员函数
int String::HowMany()
{
  return numStrings;
}

// 定义构造和析构函数
String::String()
{
  len = 0;
  str = new char[len + 1];
  str[len] = '\0';
  numStrings++;
}
String::String(const String& s)
{
  len = strlen(s.str);
  str = new char[len + 1];
  strcpy(str, s.str);
  numStrings++;
}
String::String(const char* s)
{
  len = strlen(s);
  str = new char[len + 1];
  strcpy(str, s);
  numStrings++;
}
String::~String()
{
  delete [] str;
  --numStrings;
}

// 定义重载运算符函数
String& String::operator=(const String& s)
{
  if (this == &s) // 判定是否赋值给自身
    return *this;
  else
  {
    delete [] str;
    len = s.len;
    str = new char[len + 1];
    strcpy(str, s.str);
    return *this;
  }
}
String& String::operator=(const char* s)
{
  delete [] str;
  len = strlen(s);
  str = new char[len + 1];
  strcpy(str, s);
  return *this;
}
char& String::operator[](int i)
{
  return str[i];
}
const char& String::operator[](int i) const
{
  return str[i];
}

// 定义友元函数
bool operator<(const String& a, const String& b)
{
  return (std::strcmp(a.str, b.str) < 0);
}
bool operator>(const String& a, const String& b)
{
  return (std::strcmp(a.str, b.str) > 0);
}
bool operator==(const String& a, const String& b)
{
  return (std::strcmp(a.str, b.str) == 0);
}
ostream& operator<<(ostream& os, const String& b)
{
  os << b.str;
  return os;
}
istream& operator>>(istream& in, String& b)
{
  char temp[String::CINLIM];
  in.get(temp, String::CINLIM);
  if (in)
    b = temp;
  while (in && in.get() != '\n') // 输入后清空in缓存
    continue;
  return in;
}


// sayings1.cpp
#include "string1.h"

const int ArSize = 10;
const int MaxLen = 81;

int main()
{
  using std::cout;
  using std::cin;
  using std::endl;

  String name;
  cout << "Hi, what's your name?\n";
  cin >> name;

  cout << name << ", please enter up to "
      << ArSize << "short sayings <empty line to quit >:\n";
  String sayings[ArSize];
  char temp[MaxLen];
  int i;
  for (i = 0; i < ArSize; i++)
  {
    cout << i + 1 << ": ";
    cin.get(temp, MaxLen);
    while (cin && cin.get() != '\n')
      continue;
    if (!cin || temp[0] == '\0')
      break;
    else
      sayings[i] = temp;
  }
  int total = i;

  if (total > 0)
  {
    cout << "Here are your sayings:\n";
    for (i = 0; i < total; i++)
      cout << sayings[i][0] << ": " << sayings[i] << endl;
    int shortest = 0;
    int first = 0;
    for (i = 1; i < total; i++)
    {
      if (sayings[i].length() < sayings[shortest].length())
        shortest = i;
      if (sayings[i] < sayings[first])
        first = i;
    }
    cout << "Shortest sayings:\n" << sayings[shortest] << endl;
    cout << "First alphabetically:\n" << sayings[first] << endl;
    cout << "This program used " << String::HowMany() << " String objects. Bye.\n";
  }
  else
  cout << "No input! Bye.\n";

  system("pause>nul");
  return 0;
}


结束。

posted @ 2013-05-12 23:18  庄懂  阅读(181)  评论(0编辑  收藏  举报