Im just starting to get into OOP and i'm struggling with this program.
I need to Create link list with inheritance. Once list is created calling a push from the base class to add each record to the top of stack then output records to an .txt file.
I'm not understanding how the StackList Class can see the created created list from the movie list class. In my program iv only been able to create the entire list within my CreatList method(derived class). Each record should added with the Push method from StackList(base class).
I don't understand how to create an instances of the list and use the push method to add each record.
struct DVDNode{
string title;
string leadingActor;
string supportingActor;
string genre;
string alternateGenre;
int year;
int rating;
string synopsis;
DVDNode *next;
DVDNode *tail;
};
class StackList{ // Base class using linked list implementation
public:
StackList();
~StackList();
void Push(DVDNode newDVD); //create a dvdnode add a dvdnode in the stack
// by adding to the front of the linked
// list return the dvdnode in the top of the
// stack remove the dvdnode from the stack delete
// the dvdnode
DVDNode Pop();
bool IsEmpty() const; //Check if stack is empty
DVDNode Peek() const; //return the dvdNode at the top of the stack
int Size() const;
// return the number of people in the stack
protected:
DVDNode *head; //head pointer for stack
int StackCount; // total number of persons in the stack
};
/////////////
MovieList::MovieList() {
head = NULL;
}
MovieList::~MovieList() {}
void MovieList::CreateList(string inputFileName) {
const string MOVIEFILE = "Default.txt";
ifstream DvdStream;
int count = 0;
DvdStream.open(MOVIEFILE);
StackList List;
DVDNode *head;
DVDNode *Ptr;
if (DvdStream.is_open()) // checks if file is open correctly
cout << "Input File open" << endl;
else {
cout << "Unable to open file " << inputFileName << endl;
}
Ptr = new DVDNode;
while(!DvdStream.eof()){
getline(DvdStream, Ptr-> title);
cout << Ptr -> title<<endl;
getline(DvdStream, Ptr->leadingActor);
getline(DvdStream, Ptr->supportingActor);
getline(DvdStream, Ptr->genre);
getline(DvdStream, Ptr->alternateGenre);
DvdStream >> Ptr->year;
DvdStream.ignore(1000, '\n');
DvdStream>> Ptr->rating;
DvdStream.ignore(1000, '\n');
getline(DvdStream, Ptr->synopsis);
DvdStream.ignore(1000, '\n');
Ptr->next = head;
Ptr = new DVDNode;
}
delete Ptr;
Ptr = NULL;
DvdStream.close();
}
void MovieList::OutputList(string outputFileName) const {
StackList list;
ofstream outFile;
if(outputFileName == "d")
outputFileName = "OutputFile.txt";
outFile.open(outputFileName);
if (outFile.is_open()) { // checks if Output file is open correctly
cout << "Output File open" << endl;
}
else {
cout << "Unable to open output file " << outputFileName << endl;
cout << "here " << endl;
}
DVDNode *Ptr;
DVDNode *head;
head = NULL;
Ptr = head;
Ptr = new DVDNode;
while(Ptr != NULL){
outFile << " " << Ptr->title;
cout << Ptr->genre;
Ptr = Ptr->next;
}
delete Ptr;
}
MovieList and StackList should be the same thing, or MovieList should derive from StackList.
MovieList::CreateList() should call StackList::Add() to add the DVDs to the list.
I'd add DVDNode::read(istream &) and DVDNode::write(istream&) methods. Then CreateList can call the read method and OutputList() can call the write method.
So I have mine set up like you said I have MovieList derived from StackList I just didn't post the MovieList decelerations here they are sorry.
So If I had the read and write methods in the derived class (MovieList::). I still don't understand how to access the struct (DVDNode) and add from within the StackList::Add() method if its in the base class. Is there a particular access member Im supposed to use in the StackList::Add() method ?
#include <iostream>
usingnamespace std;
// A single DVD
struct DVD {
// read and write methods.
bool read(istream &is);
bool write(ostream &os) const;
string title;
string leadingActor;
string supportingActor;
string genre;
string alternateGenre;
int year;
int rating;
string synopsis;
};
// A stack of DVDs, implemented as a linked list.
class MovieStack{
public:
// A node in the list is a DVD with a next pointer
struct Node : public DVD {
Node(const DVD &d) :
DVD(d), next(nullptr) {}
Node *next;
};
MovieStack();
~MovieStack();
// Add a dvd to the front of the list
void Push(const DVD &newDVD);
// Pop a DVD off the front of the list and return it
DVD Pop();
void clear(); // empty the list
bool IsEmpty() const; //Check if stack is empty
const DVD& Peek() const; // return a reference to the top of the list.
// The behavior is undefined if the list is empty
int Size() const;
// Read DVDs from the stream and insert them into the list.
//
protected:
Node *head; //head pointer for stack
int count; // total number of persons in the stack
};
// A MovieList is a MovieStack, with I/O functions to read and write
// the entire list
class MovieList: public MovieStack {
public:
void Create(istream &is);
void Output(ostream &os) const ;
};
int main()
{
MovieList movies; // create a movie list
movies.Create(cin); // read movies from cin
movies.Output(cout); // and write them to cout
}
Input:
Gone with the Wind
Vivian Lee
Clark Gable
Drama
Romance
1939
5
One woman faces the impossible for the cause she believes and man she loves.
Grease
John Travolta
Olivia Newton John
Musical
Comedy
1980
2
One man faces the impossible for the cause he believes and the woman he loves.
Output:
Grease
John Travolta
Olivia Newton John
Musical
Comedy
1980
2
One man faces the impossible for the cause he believes and the woman he loves.
Gone with the Wind
Vivian Lee
Clark Gable
Drama
Romance
1939
5
One woman faces the impossible for the cause she believes and man she loves.