init
This commit is contained in:
commit
be6b34bd22
|
|
@ -0,0 +1,2 @@
|
|||
*/*
|
||||
OOP*
|
||||
|
|
@ -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;
|
||||
};
|
||||
|
|
@ -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();
|
||||
};
|
||||
|
|
@ -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();
|
||||
};
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
hello
|
||||
helo
|
||||
Loading…
Reference in New Issue