Here is the code In Movie.hpp
#ifndef MOVIE_H
#define MOVIE_H
class Movie
{
private:
std::string title;
public:
std::string getTitle() const {return this->title;} // const added
void setTitle(std::string newTitle){this->title = newTitle;}
};
#endif
In Actor.hpp
#ifndef ACTOR_H
#define ACTOR_H
#include "Person.hpp"
#include "Movie.hpp"
class Actor: public Person
{
private:
std::vector<Movie> movieList;
public:
void addMovie(Movie newMovie){this->movieList.push_back(newMovie);}
void printMovies()
{
for(Movie movie: this->movieList)
{
std::cout << movie.getTitle() << '\n';
}
}
};
#endif
In Main.cpp I'm doing something like
Movie movie1, movie2, movie3;
Actor actor1, actor2;
movie1.setTitle("Transformers");
movie2.setTitle("Interstellar");
movie3.setTitle("The Matrix");
actor1.setName("Brad");
actor1.setAge(57);
actor1.addMovie(movie1); //here
actor1.addMovie(movie2);
actor2.setName("Justin");
actor2.setAge(30);
actor2.addMovie(movie3);
I have been instructed that I'm not following many-to-many relationship. In other words, if actor1 and actor2 have a connection with the same movie, they should share the same 'Movie' object. In my case, they have different movie objects. The proper way is to store pointers to Movie inside Actor:
std::vector<Movie*> movieList;
(collection of pointers, not of objects)
and inside Movie:
std::vector<Actor*> actorList;
Another proper way, I prefer this one, is to add a separate class
class Role {
private:
Actor* actor;
Movie* movie;
};
and store collection of such objects inside Movie and Actor, here values (not pointers) are OK
std::vector<Role> movieList; //inside Actor
std::vector<Role> actorList; //inside Movie
I have the idea how to solve above problem but I'm unable to make the changes in above code while using pointer. Can someone please help me out what changes will be there when I use pointers ?