1

Basically I have to store different movies in a heterogenous collection dynamically, I already have the different movie types (documentary and family) and the class which "masks" the difference (movie) so it can be stored in one place (record). I'm having trouble with my addMovie(Movie *mov) function. I don't know how to start.

My Record class:

class Record {
    String name;              
    size_t siz;                     
    Movie* movies;         
    Record(const Record&);
    Record& operator=(const Record&);
public:
    Record(String n): name(n), siz(0) {movies = new Movie[siz+1];}
    void addMovie (Movie *mov);
    void removeMovie (Movie *mov);
    void listMovies();
    void searchMovie (const char* title);
    void emptyRecord();
    void writeFile();
    void readFile();
    virtual ~Record() {emptyRecord();}
};

Movie class:

class Movie {
protected:
    String name;        
    String release;     
    String genre;       
public:
    Movie(){}
    Movie(String n, String r, String g): name(n), release(r), genre(g) {}
    virtual void write() {}
    virtual ~Movie() {}
};

Documentary class: (the family class is similar, it stores an int age_restriction and the write function writes that)

class Documentary: public Movie {
    String description;
public:
    Documentary(String n, String r, String d = "Add description up to 50 characters!"): Movie(n,r,"Documentary"), description(d) {}
    String getDescription () const {return description;}                                                                    
    void setDescription (String newdescr);
    void write();                                                                                                                                           
    virtual ~Documentary(){}
};

(PS: if you have any good sources for dynamic heterogenous stores I'm all ears)

Athmos
  • 35
  • 5
  • Use [boost::any](https://www.boost.org/doc/libs/1_73_0/doc/html/any.html) – PaulMcKenzie May 03 '20 at 10:40
  • Not using std/STL as requirement, becomes mostly as how to implement (simple) std classes equivalent. You already does for `String`. It seems you also need `std::vector` and maybe smart pointers equivalent. – Jarod42 May 03 '20 at 11:24

1 Answers1

0

Not so clear requirements, but for storing heterogeneous data in one single container, I would advise std::tuple (https://en.cppreference.com/w/cpp/utility/tuple). Let me know if it's what you were looking for ;)

EDIT: a possible solution without STL.

#include <iostream>
using namespace std;

class Base {

};

class A : public Base {

};

class B : public Base {

};

int main(){
    const size_t arraySize = 10;
    Base* array[arraySize];

    //allocate
    array[0] = new A();
    array[1] = new B();
//  ...some more stuff here

    //dispose
    for (Base* ptr : array) {
        if (ptr != nullptr) {
            delete ptr;
            ptr = nullptr;
        }
    }
}
Hack06
  • 963
  • 1
  • 12
  • 20
  • I need to write it without using any STL :/. Basically my record class should store movies dynamically, while movies can be different types (my example is the documentary class). My addMovie function gets a pointer to a movie and then adds it to the record. – Athmos May 03 '20 at 10:26
  • Oh, sorry I missed the edit! Altho the problem is that I need to increase the size of the container as I add in new stuff. – Athmos May 03 '20 at 10:44
  • 1
    *I need to increase the size of the container* -- Create your own vector class. Since you can't use STL, then you have taken on the responsibility of creating any of the classes you would have been able to use. – PaulMcKenzie May 03 '20 at 10:56
  • 1
    If you are a student and your task is to mimic the functionality of the STL-vector class, then you have 3 options: 1.(the easiest solution) copy-paste the actual vector implementation :D 2. implement the array-reallocation upon insertions and 3. implement the linked-list implementation. In either case you are risking to get a bad grade, since there's always a speed/memory tradeoff that your teacher will definitely point out. Yea, there's the 4th option, you can try the deque-implementation, which is kind of a mix of linked-list and vector, has benefits and disadvantages of both. Good luck! – Hack06 May 03 '20 at 11:12