Bài tập char và string C++

Đề:

char_string.txt 
Xây dựng class CHAR (kí tự) và STRING với việc:

STRING được xây dựng với ý tưởng: 1 STRING là một chuỗi của các CHAR. 
Test với hàm main như sau:

int main()
{
};
CHAR c1, c2('c');

STRING s1, s2("s2"), s3('a'), s4(c1),s5(s4); 

s1.expand(c2).expand('a').expand(s2).expand("abc");//s1: "cas2abc"

s1.remove(c2).remove('d');//remove all character c2 in s1 -- s1: "as2ab"

cout<<s1.getContent();
cout<<c1.getContent();
return 0;
}

//Biết rằng 2 class CHAR, STRING có thuộc tính như sau.
class CHAR
{
private:
        char content;
};

class STRING
{
private:
CHAR * content;
int length;
Page 1

Bài giải:

//
//  CHAR.hpp
//  CharString
//
//  Created by Toan Nguyen on 10/8/20.
//

#ifndef CHAR_hpp
#define CHAR_hpp

class CHAR{
private:
    char content;
public:
    CHAR();
    CHAR(char);
    
    char getContent()const;
};

#endif /* CHAR_hpp */

//
//  CHAR.cpp
//  CharString
//
//  Created by Toan Nguyen on 10/8/20.
//

#include "CHAR.hpp"
CHAR::CHAR():content('\0'){}
CHAR::CHAR(char c):content(c){}

char CHAR::getContent() const { 
    return this->content;
}

//
//  STRING.hpp
//  CharString
//
//  Created by Toan Nguyen on 10/8/20.
//

#ifndef STRING_hpp
#define STRING_hpp
#include "CHAR.hpp"
class STRING{
private:
    CHAR*content;
    int length;
public:
    STRING();
    STRING(char);
    STRING(const char*);
    STRING(const CHAR&);
    STRING(const STRING&);
    
    ~STRING();
    
    
    STRING& expand(const STRING&);
    const char* getContent() const;
    STRING& remove(const CHAR&);
};
#endif /* STRING_hpp */

//
//  STRING.cpp
//  CharString
//
//  Created by Toan Nguyen on 10/8/20.
//

#include "STRING.hpp"
#include <vector>
#include <iostream>
using namespace std;
STRING::STRING():content(NULL),length(0){}
STRING::STRING(const char* str){
    this->length = (int)strlen(str);
    this->content = new CHAR[this->length ];
    for (int i=0; i<this->length; i++) {
        this->content[i]=CHAR(str[i]);
    }
}
STRING::STRING(char c):STRING(CHAR(c)){
    
}
STRING::STRING(const CHAR& c){
    this->content = new CHAR[1];
    this->content[0] = c;
    this->length = 1;
}
STRING::STRING(const STRING& str){
    this->length = str.length;
    this->content = new CHAR[this->length];
    for (int i=0; i<str.length; i++) {
        this->content[i] = str.content[i];
    }
}
STRING::~STRING(){
    if(this->content!=NULL){
        delete [] this->content;
    }
}
STRING& STRING::expand(const STRING &s){
    if(this->length+s.length>=0){
        CHAR* newContent = new CHAR[this->length+s.length];
        for(int i=0;i<this->length;i++){
            newContent[i]=this->content[i];
        }
        for (int i=0; i<s.length; i++) {
            newContent[this->length+i] = s.content[i];
        }
        if(this->length>0){
            delete [] this->content;
        }
        this->length+=s.length;
        this->content = newContent;
    }
    return *this;
}

const char *STRING::getContent() const {
    char * str = new char[this->length+1];
    for (int i=0; i< this->length; i++) {
        str[i] = this->content[i].getContent();
    }
    str[this->length]='\0';
    return str;
}

STRING &STRING::remove(const CHAR &c) {
    vector<int> indices;
    for (int i=0;i<this->length; i++) {
        if(this->content[i].getContent()==c.getContent()){
            indices.push_back(i);
            
        }
    }
    CHAR* newContent = new CHAR[this->length - (int)indices.size()];
    int index = 0;
    for (int i=0; i<this->length; i++) {
        bool isRemoved = false;
        for (int j=0; j<indices.size(); j++) {
            if(i == indices[j]){
                isRemoved = true;
                break;
            }
        }
        if(!isRemoved){
            newContent[index++]=this->content[i];
        }
    }
    this->length -=indices.size();
    if(this->content!=NULL)
        delete  [] this->content;
    this->content = newContent;
    return *this;
}

//
//  main.cpp
//  CharString
//
//  Created by Toan Nguyen on 10/8/20.
//

#include <iostream>
#include "CHAR.hpp"
#include "STRING.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
    CHAR c1, c2('c');
    STRING s1, s2("s2"), s3('a'), s4(c1),s5(s4); s1.expand(c2).expand('a').expand(s2).expand("abc");
    s1.remove(c2).remove('d');
    cout<<s1.getContent();
    cout<<c1.getContent();
    return 0;
}