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

From ZCubes Wiki
Jump to navigation Jump to search
  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#include "S.h"
 22
 23S::
 24S(const char *a) {
 25  if(a) {
 26    l = strlen(a);
 27    s = new char[l+1];
 28    strcpy(s,a);
 29  }
 30  else {
 31    l = 0;
 32    s = new char[l+1];
 33    *s = '\0';
 34  }
 35}
 36
 37S::
 38S(const S& a) {
 39  if(a.s) {
 40    l = strlen(a.s);
 41    s = new char[l+1];
 42    strcpy(s,a.s);
 43  }
 44  else {
 45    l = 0;
 46    s = new char[l+1];
 47    *s = '\0';
 48  }
 49}
 50
 51S::
 52~S() {
 53  delete [] s;
 54}
 55
 56S&
 57S::
 58operator=(const S& a) {
 59  if(this==&a) return *this;
 60
 61  delete [] s;
 62
 63  l = strlen(a.s);
 64  s = new char[l+1];
 65
 66  strcpy(s,
 67         a.s);
 68
 69  return *this;
 70}
 71
 72S&
 73S::
 74operator=(const char* a) {
 75  if(*this==a) return *this;
 76
 77  delete [] s;
 78
 79  l = strlen(a);
 80  s = new char[l+1];
 81
 82  strcpy(s,
 83         a);
 84
 85  return *this;
 86}
 87
 88S
 89operator+(const S& a,
 90          const S& b) {
 91  int n = a.l + b.l;
 92  char *t = new char[n+1];
 93
 94  strcpy(t,
 95         a.s);
 96
 97  strcat(t,
 98         b.s);
 99
100  return S(t);
101}
102
103bool
104operator==(const S& a,
105           const S& b) {
106  return (strcmp(a.s, b.s)==0);
107}
108
109ostream&
110operator<<(ostream& os,
111           const S& a) {
112  os << a.s << endl;
113
114  return os;
115}