SVN / public / code / vorlesung-06 / String.cpp

Revision 11340
Date
Committerhb1003
Download
// Copyright 2016, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Hannah Bast <bast@cs.uni-freiburg.de>.

#include <stdio.h>
#include "./String.h"

// _____________________________________________________________________________
int objectCount = 0;

// _____________________________________________________________________________
String::String() {
  _id = ++objectCount;
  printf("Inside normal constructor ... creating String #%d\n", _id);
  _contents = NULL;
  _length = 0;
}

// _____________________________________________________________________________
String::String(const String& s) {
  _id = ++objectCount;
  printf("Inside copy constructor ... creating String #%d\n", _id);
  _length = s._length;
  _contents = new char[_length];
  for (int i = 0; i < _length; i++) { _contents[i] = s._contents[i]; }
}


// _____________________________________________________________________________
String::~String() {
  printf("Inside destructor ... destroying String #%d\n", _id);
  delete[] _contents;
}

// _____________________________________________________________________________
void String::set(const char* s) {
  _length = strlen(s);
  delete[] _contents;
  _contents = new char[_length];
  for (int i = 0; i < _length; i++) { _contents[i] = s[i]; }
}

// _____________________________________________________________________________
int String::length() const {
  return _length;
}