Lưu ý: trong đoạn cuối video mình có cố thực hiện destructor nhưng không được, mình đã tìm hiểu ra nguyên nhân là do mình delete con trỏ NULL nên sinh ra lỗi, đơn giản chỉ cần thêm dòng lệnh kiểm tra con trỏ khác NULL thì delete là được. Mình khuyên các bạn nên dùng kiểu vector vì nó sẽ tự động quản lý bộ nhớ và sẽ tự giải phóng sau khi kết thúc chương trình. Nhưng do đề yêu cầu nên mình đành dùng mảng động vậy, các bạn có thể tham khảo bài viết về vector của mình tại đây.
Đề bài
Xây dựng class CHAR (ký tự) và STRING (chuỗi) 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"
s1.input();//nhập chuỗi mới từ bàn phím
cout << s1.getContent();
cout << c1.getContent();
return 0;
}
Mẫu class CHAR và STRING được cho sẵn như sau:
class CHAR
{
private:
char content;
};
class STRING
{
private:
CHAR* content;
int length;
};
Source code
#include <iostream>
using namespace std;
#include "CHAR.h"
#include "STRING.h"
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"
s1.input();//nhập chuỗi mới từ bàn phím
cout << s1.getContent();
cout << c2.getContent();
return 0;
}
#ifndef CHAR_h
#define CHAR_h
class CHAR
{
private:
char content;
public:
CHAR();
CHAR(char);
char getContent() const;
};
#endif
#include "CHAR.h"
CHAR::CHAR()
{
}
CHAR::CHAR(char ch)
{
this->content = ch;
}
char CHAR::getContent() const
{
return this->content;
}
#ifndef STRING_h
#define STRING_h
#include <string>
#include "CHAR.h"
class STRING
{
private:
CHAR* content;
int length;
public:
STRING();
STRING(std::string);
STRING(char);
STRING(CHAR);
~STRING();
std::string getContent() const;
STRING& expand(CHAR);
STRING& expand(char);
STRING& expand(std::string);
STRING& expand(STRING);
STRING& remove(CHAR);
STRING& remove(char);
void input();
};
#endif
#include "STRING.h"
#include <iostream>
#include <string>
STRING::STRING()
{
this->content = nullptr;
this->length = 0;
}
STRING::STRING(std::string str)
{
this->length = str.length();
this->content = new CHAR[str.length()];
for (int i = 0; i < this->length; i++)
this->content[i] = str[i];
}
STRING::STRING(char ch)
{
this->length = 1;
this->content = new CHAR[1];
this->content[0] = CHAR(ch);
}
STRING::STRING(CHAR ch)
{
this->length = 1;
this->content = new CHAR[1];
this->content[0] = ch;
}
STRING::~STRING()
{
if (this->content != NULL)
delete[] this->content;
this->content = nullptr;
this->length = 0;
}
std::string STRING::getContent() const
{
std::string str;
for (int i = 0; i < this->length; i++)
str.push_back(this->content[i].getContent());
return str;
}
STRING& STRING::expand(CHAR ch)
{
std::string str = this->getContent();
str.push_back(ch.getContent());
if (this->content != NULL)
delete[] this->content;
this->STRING::STRING(str);
return *this;
}
STRING& STRING::expand(STRING str)
{
std::string finalStr = this->getContent();
finalStr.append(str.getContent());
if (this->content != NULL)
delete[] this->content;
this->STRING::STRING(finalStr);
return *this;
}
STRING& STRING::expand(char ch)
{
std::string str = this->getContent();
str.push_back(ch);
if (this->content != NULL)
delete[] this->content;
this->STRING::STRING(str);
return *this;
}
STRING& STRING::expand(std::string str)
{
std::string finalStr = this->getContent();
finalStr.append(str);
if (this->content != NULL)
delete[] this->content;
this->STRING::STRING(finalStr);
return *this;
}
STRING& STRING::remove(CHAR ch)
{
std::string str;
for (int i = 0; i < this->length; i++)
if (this->content[i].getContent() != ch.getContent())
str.push_back(this->content[i].getContent());
this->STRING::STRING(str);
return *this;
}
STRING& STRING::remove(char ch)
{
std::string str;
for (int i = 0; i < this->length; i++)
if (this->content[i].getContent() != ch)
str.push_back(this->content[i].getContent());
this->STRING::STRING(str);
return *this;
}
void STRING::input()
{
std::string str;
getline(std::cin, str);
if (this->content != NULL)
delete[] this->content;
this->STRING::STRING(str);
}