storing actors in a movie class

  • (2 Pages)
  • +
  • 1
  • 2

21 Replies - 1015 Views - Last Post: 19 February 2013 - 04:27 AM Rate Topic: -----

#1 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

storing actors in a movie class

Posted 14 February 2013 - 03:14 PM

I am writing a program that reads from a file called movies.db that has a list of movies and each movie is a record containing information about each movie. It reads the records into a vector. The problem I have is how do I make it so that it can store any number of actors in each record? eg, one movie may have one actor, while another may have five. The program than allows movies to be searched by actor. How do I make it so that a record can have any number of actors stored in it plus the movie title, director, running time, etc. etc.

Is This A Good Question/Topic? 0
  • +

Replies To: storing actors in a movie class

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,999
  • Joined: 05-May 12

Re: storing actors in a movie class

Posted 14 February 2013 - 03:20 PM

If you are asking about C/C++, then you simply have a vector of pointers to actors. If you are asking about database schema design, then if you have an object oriented database (like db4o), then you mere save your object and all things will just work. If on the other hand, you have a relational database (like SQL), then you'll have create a table of actors, a table of movies, and a join table the links movies to actors.
Was This Post Helpful? 0
  • +
  • -

#3 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 16 February 2013 - 04:20 PM

Its the vector of pointers to actors I believe. Though it probably could be just a vector of actors I imagine also, without the pointers. Not sure which would be more complicated. Would this end up being multiple actor vectors inside of a movie vector? How does that get implemented? Friend of mine called it Vectorception, referring to the dream within a dream from Inception, but instead its vector within a vector. Anyway, I have a vector containing movies, with each movie object(I guess I declare a movie class) containing string title, int runningTime, string genre, string description, int year, string director, and then however I declare the actor part (will obviously still use string, but again, not sure how to implement this part).
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: storing actors in a movie class

Posted 16 February 2013 - 04:59 PM

In OOP world, you'll have something like:
struct Movie {
	string getName();
	ActorList getActors();
};

struct Actor {
	string getName();
	MovieList getMovies();
};



So, yes, they reference each other ad nauseum. Sort of.

In a relational database, this is pretty standard:
table: movie
	movie_id (PK)
	name
	...

table: actor
	actor_id (PK)
	name
	...

table: movie_actor
	movie_id (PK, FK)
	actor_id (PK, FK)



With this in mind, an OOP solution is often to have all the objects in one collection and object methods leveraging them. e.g.
MovieList getMoviesForActor(const Actor &);
//...

struct AllMovies public MovieList {
	//...
}

static AllMovies allMoviesInstance;

struct MovieActor {
	Movie *movie;
	Actor *actor;
};

MovieList Actor::getMovies() { return getMoviesForActor(*this); }



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#5 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 16 February 2013 - 06:06 PM

I'm not following sorry. That looks more like C than C++, because of the use of struct as opposed to class (I know you can use struct in C++, but it seems it would be easier to use class). I think I have how to implement a vector of movies, but don't know how to do the actors within. Right now, I am utilizing a movie class, and will have code to read from a file into the vector, with each line of the text file being the record for another movie. Again, I think I know how to do the main vector, but not how to implement multiple actors within, since each actor will be on the same line.
Was This Post Helpful? 0
  • +
  • -

#6 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 16 February 2013 - 06:45 PM

Oh, just realized, each item in a record is separated by ;. Has a format like this:
Title; running time; genre; description; director; actor1; actor2;.....
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: storing actors in a movie class

Posted 16 February 2013 - 07:26 PM

View Postjayburn00, on 16 February 2013 - 08:06 PM, said:

I know you can use struct in C++, but it seems it would be easier to use class


In C++, struct and class are the same thing. One defaults to public, the other to private, and since I was just looking for public...


Edit: slept on this, crappy answer.

View Postjayburn00, on 16 February 2013 - 08:45 PM, said:

Has a format like this:
Title; running time; genre; description; director; actor1; actor2;.....


That's just a parsing issue.

Perhaps something like:
class Movie {
public:
	Movie(const string &title, const string &genre, const string &description);
	string getTitle() const;
	string getDescription() const;
	string getGenre() const;
	vector<string> getActors();
	void addActor(const string &);
	
private:
	vector<string> actors;
	string title, description, genre;
};

void parse(const std::string &, Movie &);


This post has been edited by baavgai: 17 February 2013 - 03:45 AM

Was This Post Helpful? 0
  • +
  • -

#8 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 17 February 2013 - 11:11 AM

View Postbaavgai, on 16 February 2013 - 07:26 PM, said:

View Postjayburn00, on 16 February 2013 - 08:06 PM, said:

I know you can use struct in C++, but it seems it would be easier to use class


In C++, struct and class are the same thing. One defaults to public, the other to private, and since I was just looking for public...


Edit: slept on this, crappy answer.

View Postjayburn00, on 16 February 2013 - 08:45 PM, said:

Has a format like this:
Title; running time; genre; description; director; actor1; actor2;.....


That's just a parsing issue.

Perhaps something like:
class Movie {
public:
	Movie(const string &title, const string &genre, const string &description);
	string getTitle() const;
	string getDescription() const;
	string getGenre() const;
	vector<string> getActors();
	void addActor(const string &);
	
private:
	vector<string> actors;
	string title, description, genre;
};

void parse(const std::string &, Movie &);



To clarify, why did you use the & in your code? I have been using only this->title and passed title as just title, not as the memory address of title. Why do you have it passing the memory address?
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is online

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: storing actors in a movie class

Posted 17 February 2013 - 11:46 AM

Quote

To clarify, why did you use the & in your code? I have been using only this->title and passed title as just title, not as the memory address of title. Why do you have it passing the memory address?


Do you mean the following snippet?

void parse(const std::string &, Movie &); 


If so the ampersand in this case indicates passing by reference, not the address of? You may want to study the function tutorials contained in my signature.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: storing actors in a movie class

Posted 17 February 2013 - 11:52 AM

Now you're the one who's thinking C. :P

The & is pass by reference in C++ function signatures. It's still "address of" if used in a variable evaluation.

When you pass a string ( or any object ), you have several choices:
void Foo(string s);
// this is a copy of a string.
// Function Foo can change it, but it's only a local copy and nothing will be passed back

void Foo(string &s);
// a reference to s, changes to s will be passed back.
// No special symbols are needed when calling or using in function

void Foo(string *s);
// a pointer to s.
// Same level of access as above, but special symbols needed for both calling and using in function.
// Some other subtle difference.  Avoided in C++.  Pointers can allow memory leaks where a reference doesn't.

void Foo(const string &s);
// a reference to s, but no changes can be made
// This does not make a copy, but is immutable
// it is the preferred syntax for passing a value where no changes are to be passed back



If you're unfamiliar with this syntax, or const correctness, you should read up on these concepts before moving on to classes.
Was This Post Helpful? 0
  • +
  • -

#11 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 17 February 2013 - 12:16 PM

No I get it now. I simply had code that had a similar purpose but didn't use pass by reference. Couldn't you use operator overloading to make the get functions unnecessary by creating an operator that takes all of the parameters as input?
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,636
  • Joined: 16-October 07

Re: storing actors in a movie class

Posted 17 February 2013 - 02:51 PM

It's your program, your design. There are near infinite permutations for even trivial programs. Most of programming is organization.

Ideally, your object is initialized via the constructor. With as few mutators as possible. This makes your object easier to maintain.

The point of parse was blurred because I forgot the return type. bool parse(const std::string &, Movie &); If you want to parse a line and create an object there's the potential for failure. So you return if you actually got what you want.

The guts would look something like:
bool parse(const std::string &line, Movie &movie) {
	// ..
	if (ss >> title >> genre) {
		moved = Movie(title, genre);
		return true;
	}
	return false;
}


Was This Post Helpful? 0
  • +
  • -

#13 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 18 February 2013 - 03:56 PM

class movie
{
public:
	//constructor
	movie()	//default
	{
		this->title = "";
		this->runtime = 0;//runtime can be in mintues or hour depending on the file
		this->genre = "";
		this->description = "";
		this->year = 0;
		this->director = "";
	}

	movie(string title, int runtime, string genre, string description, int year, string director)
	{
		this->title = title;
		this->runtime = runtime;
		this->genre = genre;
		this->description = description;
		this->year = year;
		this->director = director;
	}

	void Display() //displayment of the movie object
	{
		cout<<"Title: "<<this->title<<endl;
		cout<<"Run Time: "<<this->runtime<<endl;
		cout<<"Genre: "<<this->genre<<endl;
		cout<<"Description: "<<this->description<<endl;
		cout<<"Year: "<<this->year<<endl;
		cout<<"Director: "<<this->director<<endl;
	}

private:
	string title, genre, description, director;


	int runtime, year;
};

bool createmovie(string m, movie & Movie)
{
	int position1 = 0;
	int position2 = m.find(";");
		
	string title = m.substr(position1, position2);

	position1 = position2 + 1;
	position2 = m.find(";", position1);

	int runtime = atoi(m.substr(position1, position2-position1).c_str());

	position1 = position2 + 1;
	position2 = m.find(";", position1);

	string genre = m.substr(position1, position2-position1);

	position1 = position2 + 1;
	position2 = m.find(";", position1);

	string description = m.substr(position1, position2-position1);

	position1 = position2 + 1;
	position2 = m.find(";", position1);

	int year = atoi(m.substr(position1, position2-position1).c_str());

	position1 = position2 + 1;
	position2 = m.find(";", position1);

	string director = m.substr(position1, position2-position1);

if(m.size() > 0)
	{
		Movie = movie(title, runtime, genre, description, year, director);
		return true;
	}
	else
	{
		return false;
	}
}

int loadmovie(string filname, vector<movie> & Movie)
{
	fstream file(filname.c_str());

	string line = "";
	int count = 0;
	
	while(getline(file,line))
	{
		movie tempmovie;
		if (createmovie(line, tempmovie))
		{
			Movie.push_back(tempmovie);
			count++;
		}
	}
	file.close();

	return count;
}

int displayMenu()
{
	cout<<" 1: Search Movies"<<endl;
	cout<<" 2: Show Movies"<<endl;
	cout<<" 3: Sort Records"<<endl;
	cout<<" 4: Exit"<<endl;

	int input;
	cin>>input;
	return input;
}

int main()
{
	vector<movie> Movie;

	string filname = "../movies.db";

	int count = loadmovie(filname, Movie);
	cout<<" Number of Movies: "<<count<<endl;

	int choice = 0;

	while(true)
	{
		choice = displayMenu();
		if(choice == 4)
		{
			cout<<"Thank You for using our Movie database"<<endl;
			break;
		}
		else
		{
			switch(choice)
			{
			case 1:
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				break;
			}
		}
	}

	return 0;
}



That is what I have so far. As you can see I, still don't have actors working. Based on this code, how would I set up actors?
Was This Post Helpful? 0
  • +
  • -

#14 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3551
  • View blog
  • Posts: 10,999
  • Joined: 05-May 12

Re: storing actors in a movie class

Posted 18 February 2013 - 04:29 PM

If you knew that you could declare:
vector<movie> movies;



Then you can also have member in your movie class:
class movie
{
    :

    vector<actor> actors;
};



I don't understand what all the hand wringing is about. My only point of concern is it seems that your input file is semi-colon delimited. I think you'll need to figure out how to delimit when the actors start and end.
Was This Post Helpful? 0
  • +
  • -

#15 jayburn00  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 118
  • Joined: 05-July 12

Re: storing actors in a movie class

Posted 18 February 2013 - 04:51 PM

View PostSkydiver, on 18 February 2013 - 04:29 PM, said:

If you knew that you could declare:
vector<movie> movies;



Then you can also have member in your movie class:
class movie
{
    :

    vector<actor> actors;
};



I don't understand what all the hand wringing is about. My only point of concern is it seems that your input file is semi-colon delimited. I think you'll need to figure out how to delimit when the actors start and end.


All I know is that everything is delimitted by semicolon, that is simply how the file is. The end of the actors is the end of the record (actors are always the last entities listed in the file. Title is first, then so and so forth.) So, with that in mind, how would I set it up?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2