21 Replies - 445 Views - Last Post: 19 February 2013 - 04:27 AM
#1
storing actors in a movie class
Posted 14 February 2013 - 03:14 PM
Replies To: storing actors in a movie class
#2
Re: storing actors in a movie class
Posted 14 February 2013 - 03:20 PM
#3
Re: storing actors in a movie class
Posted 16 February 2013 - 04:20 PM
#4
Re: storing actors in a movie class
Posted 16 February 2013 - 04:59 PM
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.
#5
Re: storing actors in a movie class
Posted 16 February 2013 - 06:06 PM
#6
Re: storing actors in a movie class
Posted 16 February 2013 - 06:45 PM
Title; running time; genre; description; director; actor1; actor2;.....
#7
Re: storing actors in a movie class
Posted 16 February 2013 - 07:26 PM
jayburn00, on 16 February 2013 - 08:06 PM, said:
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.
jayburn00, on 16 February 2013 - 08:45 PM, said:
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
#8
Re: storing actors in a movie class
Posted 17 February 2013 - 11:11 AM
baavgai, on 16 February 2013 - 07:26 PM, said:
jayburn00, on 16 February 2013 - 08:06 PM, said:
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.
jayburn00, on 16 February 2013 - 08:45 PM, said:
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?
#9
Re: storing actors in a movie class
Posted 17 February 2013 - 11:46 AM
Quote
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
#10
Re: storing actors in a movie class
Posted 17 February 2013 - 11:52 AM
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.
#11
Re: storing actors in a movie class
Posted 17 February 2013 - 12:16 PM
#12
Re: storing actors in a movie class
Posted 17 February 2013 - 02:51 PM
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;
}
#13
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?
#14
Re: storing actors in a movie class
Posted 18 February 2013 - 04:29 PM
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.
#15
Re: storing actors in a movie class
Posted 18 February 2013 - 04:51 PM
Skydiver, on 18 February 2013 - 04:29 PM, said:
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?
|
|

New Topic/Question
Reply



MultiQuote





|