This commit is contained in:
Andrey Kassaev 2024-03-03 22:15:25 +04:00
commit be6b34bd22
5 changed files with 115 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*/*
OOP*

17
Main.cpp Normal file
View File

@ -0,0 +1,17 @@
#include <iostream>
#include "MyString.h"
int main() {
MyString myString1;
myString1.set("hello");
myString1.print();
MyString myString2(myString1);
myString2.print();
myString2.update();
system("pause");
return 0;
};

72
MyString.cpp Normal file
View File

@ -0,0 +1,72 @@
#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();
};

22
MyString.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <cstring>
#include <iostream>
using namespace std;
class MyString
{
private:
char* str;
public:
MyString();
MyString(const char* value);
MyString(const MyString&);
~MyString();
char* getString();
void set(const char* value);
void print();
void update();
void writeToFile();
};

2
filename.txt Normal file
View File

@ -0,0 +1,2 @@
hello
helo