OOP_zadanie3/MyString.cpp

72 lines
1.4 KiB
C++

#include "MyString.h"
#include <fstream>
MyString::MyString() {
cout << "<<Default constructor>>\n";
this->str = new char('\0');
};
MyString::MyString(const char* value) {
cout << "<<Constructor with parameter>>\n";
this->str = new char[strlen(value) + 1];
strcpy_s(str, strlen(value) + 1, value);
this->str[strlen(value)] = '\0';
};
MyString::MyString(const MyString& obj) {
cout << "<<Copy constructor>>\n";
this->str = new char[strlen(obj.str) + 1];
strcpy_s(str, strlen(obj.str) + 1, obj.str);
this->str[strlen(obj.str)] = '\0';
};
MyString::~MyString() {
cout << "<<Destructor>>\n";
delete[] this->str;
};
char* MyString::getString() {
cout << "<getString><\n";
return this->str;
};
void MyString::set(const char* value) {
cout << "<<set>>\n";
delete[] this->str;
this->str = new char[strlen(value) + 1];
strcpy_s(str, strlen(value) + 1, value);
this->str[strlen(value)] = '\0';
};
void MyString::print() {
cout << "<<print>>\n";
cout << this->str << endl;
};
void MyString::update() {
cout << "<<update>>\n";
writeToFile();
if (strlen(this->str) % 2 != 0)
{
int middleIndex = strlen(this->str) / 2;
for (size_t i = middleIndex; i < strlen(this->str); i++)
{
this->str[i] = this->str[i+1];
}
this->str[strlen(this->str)] = '\0';
writeToFile();
}
};
void MyString::writeToFile() {
cout << "<<writeToFile>>\n";
ofstream MyFile("filename.txt", ios_base::app);
MyFile << this->str << "\n";
MyFile.close();
};