I have a derived class called CaseString that is supposed to capitalize and decapitalize the characters of a string. What is wrong with the code?
Code:
//case.h
#ifndef CaseString_H
#define CaseString_H
#include <iostream>
#include "strdrv.h"
#include "string.h"
class CaseString: public String
{
private:
char* lower;
char* upper;
String name;
public:
CaseString();
CaseString(const char *);
CaseString(const CaseString &rhs);
~CaseString();
CaseString operator =(const CaseString &rhs);
void print();
};
#endif
// case.cpp
#define _CRT_SECURE_NO_DEPRECATE 1
#include <iostream>
#include <assert.h>
#include <cctype>
#include <algorithm>
#include "case.h"
using namespace std;
CaseString::CaseString()
{
name = NULL;
stringlength = 0;
size = stringlength + 1;
buf = new char[size];
lower = new char[size];
upper = new char[size];
memset(buf, 0, size);
memset(lower, 0, size);
memset(upper, 0, size);
}
CaseString::CaseString(const CaseString &rhs)
{
name = NULL;
stringlength = rhs.stringlength;
size = stringlength + 1;
buf = new char[size];
lower = new char[size];
upper = new char[size];
strcpy(buf, rhs.buf);
strcpy(lower, rhs.lower);
strcpy(upper, rhs.upper);
for(int i = 0; i<=stringlength ; i++)
{
lower[i] = tolower(lower[i]);
upper[i] = toupper(upper[i]);
}
}
CaseString::CaseString(const char *str)
{
name = NULL;
stringlength = strlen(str);
size = stringlength + 1;
buf = new char[size];
lower = new char[size];
upper = new char[size];
strcpy(buf, str);
strcpy(lower, str);
strcpy(upper, str);
for(int i = 0; i <= stringlength; i++)
{
lower[i] = tolower(lower[i]);
upper[i] = toupper(upper[i]);
}
}
CaseString CaseString::operator =(const CaseString &rhs)
{
if(this != &rhs)
{
delete []buf;
stringlength = rhs.stringlength;
size = stringlength + 1;
buf = new char[size];
lower = new char[size];
upper = new char[size];
strcpy(buf, rhs.buf);
strcpy(lower, rhs.lower);
strcpy(upper, rhs.upper);
for(int i= 0; i<stringlength; i++)
{
lower[i] = tolower(lower[i]);
upper[i] = toupper(upper[i]);
}
}
return *this;
}
CaseString::~CaseString()
{
delete []buf;
delete []lower;
delete []upper;
}
void CaseString::print()
{
printf("%s: %s(%d)\n", name, buf, stringlength);
printf("%s: %s(%d)\n", name, lower, stringlength);
printf("%s: %s(%d)\n", name, upper, stringlength);
}