fork download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. class Genre {
  6. struct Genres {
  7. string name;
  8. string description;
  9. };
  10.  
  11. using pGenres = Genres*;
  12.  
  13. int index = 0;
  14. public:
  15. pGenres* genres = nullptr;
  16.  
  17. Genre() {}
  18. ~Genre() {
  19. if (index == 0) return;
  20.  
  21. for(int i = 0; i<index; i++){
  22. delete genres[i];
  23. }
  24. delete[] genres;
  25. }
  26. void addGenre(string name, string desc){
  27. // string good_name = this->goodLetters(name);
  28. string good_name = name;
  29.  
  30. auto new_genres = new pGenres[index+1];
  31. if (genres){
  32. memcpy(new_genres, genres, index * sizeof(pGenres));
  33. }
  34.  
  35. new_genres[index] = new Genres;
  36. new_genres[index]->name = good_name;
  37. new_genres[index]->description = desc;
  38.  
  39. if (genres){
  40. delete[] genres;
  41. }
  42.  
  43. genres = new_genres;
  44. index++;
  45. }
  46. void print(){
  47. cout << "print:" << endl;
  48. for(int i=0; i<index; i++){
  49. cout << i << " :: " << genres[i]->name << " :: " << genres[i]->description << endl;
  50. }
  51. }
  52. };
  53.  
  54. int main() {
  55. // your code goes here
  56. Genre genre;
  57. genre.addGenre("Name1", "Desc1");
  58. genre.addGenre("Name2", "Desc2");
  59. genre.print();
  60. return 0;
  61. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
print:
0 :: Name1 :: Desc1
1 :: Name2 :: Desc2