Yurttas/PL/OOL/Cplusplus/F/03/02/01/00/07/S.cpp

  1/*
  2   Copyright(C) 1998
  3   All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
  4
  5   Permission to use, copy, modify, and distribute this
  6   software and its documentation for EDUCATIONAL purposes
  7   and without fee is hereby granted provided that this
  8   copyright notice appears in all copies.
  9
 10   date   : January 1, 1998.
 11   authorĀ : Salih Yurttas.
 12
 13   S.cpp
 14*/
 15
 16
 17#include <cstring>
 18
 19using namespace std;
 20
 21
 22#include "S.h"
 23
 24S::
 25S(const char *a) {
 26  if(a) {
 27    l = strlen(a);
 28    s = new char[l+1];
 29    strcpy(s,a);
 30  }
 31  else {
 32    l = 0;
 33    s = new char[l+1];
 34    *s = '\0';
 35  }
 36}
 37
 38S::
 39S(const S& a) {
 40  if(a.s) {
 41    l = strlen(a.s);
 42    s = new char[l+1];
 43    strcpy(s,a.s);
 44  }
 45  else {
 46    l = 0;
 47    s = new char[l+1];
 48    *s = '\0';
 49  }
 50}
 51
 52S::
 53~S() {
 54  delete [] s;
 55}
 56
 57S&
 58S::
 59operator=(const S& a) {
 60  if(this==&a) return *this;
 61
 62  delete [] s;
 63
 64  l = strlen(a.s);
 65  s = new char[l+1];
 66
 67  strcpy(s, a.s);
 68}
 69
 70S&
 71S::
 72operator=(const char* a) {
 73  if(s==a) return *this;
 74
 75  delete [] s;
 76  s = 0;
 77
 78  l = strlen(a);
 79  char* t = new char[l+1];
 80
 81  strcpy(t, a);
 82
 83  s = t;
 84
 85  return *this;
 86}
 87
 88S&
 89S::
 90operator+(const S& a) {
 91  l += a.l;
 92  char *t = new char[l+1];
 93
 94  strcpy(t, s);
 95
 96  delete [] s;
 97  s = 0;
 98
 99  strcat(t, a.s);
100
101  s = t;
102
103  return *this;
104}
105
106bool
107S::
108operator==(const S& a) {
109  return (strcmp(s, a.s)==0);
110}
111
112ostream&
113operator<<(ostream& os,
114           const S& a) {
115  os << a.s << endl;
116
117  return os;
118}