Untitled
unknown
c_cpp
3 years ago
2.3 kB
9
Indexable
#include "Header.h"
class Notebook {
private:
string* plan;
int size;
public:
//Constructor
Notebook();
//Constructor of initialization
Notebook(string* plan, int size);
//Destructor
~Notebook();
//Display notebook
void Display();
//Add note
void Add(const string& note);
//Remove note
void Remove();
//Edit note
void Edit(int idxOfNote, string newNote);
};
Notebook::Notebook() {
this->plan = nullptr;
this->size = 0;
}
Notebook::~Notebook() {
delete[] this->plan;
}
// TYPE* name = new TYPE[SIZE];
void Notebook::Add(const string& note) {
string* temp = this->plan; //Тимчасовий масив для зберігання поточного
this->plan = new string[this->size + 1];
for (int i = 0; i < this->size; ++i) {
this->plan[i] = temp[i];
}
this->plan[this->size] = note; //Додаємо в кінець поточного масиву потрібне значення
this->size++;
delete[] temp;
}
Notebook::Notebook(string* plan, int size) {
if (size > 7) {
cout << "Oops, to much notes =(" << endl;
exit(-1);
}
this->size = size;
this->plan = new string[size];
for (int i = 0; i < size; ++i) {
this->plan[i] = plan[i];
}
}
void Notebook::Display() {
cout << "Your Notebook:" << endl;
for (int i = 0; i < size; ++i) {
cout << i+1 << ". " << plan[i] << endl;
}
}
void Notebook::Remove() {
size--;
auto* temp = new string[size];;
for (int i = 0; i < size; i++)
temp[i] = plan[i];
delete[] plan;
plan = temp;
}
void Notebook::Edit(int idxOfNote, string newNote) {
this->plan[idxOfNote] = newNote;
}
int main() {
int size = 3;
auto* plan = new string[size]{
"Time to Eat",
"Sleep and go play game",
"Rest after school day"
};
Notebook notebook(plan, size);
notebook.Display();
notebook.Add("Do homework");
notebook.Display();
notebook.Edit(0, "Cook some food");
notebook.Display();
notebook.Remove();
notebook.Display();
return 0;
}Editor is loading...