OOP mess.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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;

}
Last edited on
Please edit your post to put [code][/code] tags around your code (use the <> format icon).
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 ?

1
2
3
4
5
6
7
8
9
10
11
12

class MovieList : public StackList {
public:
    MovieList();
    ~MovieList();

    void CreateList(string inputFileName);
    void OutputList(string outputFileName) const;

    void ClassHeader();
private:
    string WordWrap(string plot) const;
> class MovieList : public StackList
your weird design says that `MovieList' is a `StackList' so you may treat it as such
1
2
MovieList billboard;
billboard.Push(kfcotr2);
Here are class definitions that I used to implement this. I've also included a simple main() program and input file. See if you can write the methods.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>

using namespace 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.

Topic archived. No new replies allowed.