Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In Movie.hpp

    #ifndef MOVIE_H
    #define  MOVIE_H
    
    class Movie
    {
      private:
        std::string title;
      public:
        std::string getTitle() const {return this->title;}   
        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

What are the changes will be there in the code if I add a separate class

    class Role {
    private:
       Actor* actor;
       Movie* movie;
    };

and store collection of such objects inside Movie and Actor

    std::vector<Role> movieList; //inside Actor

    std::vector<Role> actorList; //inside Movie

Someone please help me out by editing the code in Movie.hpp and Actor.hpp using the "Role" Object defined above. 


What I have tried:

I have implemented the code as above but I need to add separate class file "Role" as mentioned above to implement many-to-many relationship. Please help me out by editing the code in Movie.hpp and Actor.hpp after inserting "Role" object as mentioned.
Posted
Updated 9-Jun-21 14:17pm

Not sure why you want a Role inside an Actor. If you're looking at an Actor, the Actor field in Role would be redundant. Perhaps this would make more sense:
C++
class ActorRole  // added to a vector in Actor
{
   Person* part_;
   Movie* movie_;
};

class MovieRole  // added to a vector in Movie
{
   Person* part_;
   Actor* actor_;
};

auto gbu = new Movie("The Good, The Bad, and The Ugly");
auto clint = new Actor("Clint Eastwood");
auto eli = new Actor("Eli Wallach");
auto lee = new Actor("Lee Van Cleef");
AddRole(gbu, clint, "Blondie");
AddRole(gbu, eli, "Tuco");
AddRole(gbu, lee, "Angel Eyes");
where the free function AddRole creates an ActorRole and adds it to the specified Actor and also creates a MovieRole and adds it to the specified Movie.
 
Share this answer
 
v2
Comments
Richard MacCutchan 10-Jun-21 4:22am    
+5, especially for referring to such a brilliant film.
Greg Utas 10-Jun-21 5:35am    
:-D and thanks!
While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900