How to get classes to work with each other

This is for my final project. The last chapter we have read is an introductory to classes. Instruction for the project: This program will allow the user to keep track of a DVD collection. The data will be stored in a text file as records. Each DVD in the collection will be represented as a class, so you will have one class that is the DVD. The DVD class will have data members for the title, length, the year, and the names of two of the main actors. (I have this class created...default constructor that sets all member variables to "", and member functions that set and retrieve the variables.) There will be a class that maintains the list of DVDs. This list provides methods to add, remove, and update a DVD. The program should provide a menu for the user to add, delete, update and display the info in DVD.

My question is how do I get the classes to work with each other? Or do I even need to do that? If the List class is going to update or add or delete a record, and a record is a Dvd class object, how do I get them to work with each other?
The easy way out is creating a DVDManager class that contains a std::vector of DVDs. To add and remove elements you will have to provide functions that call the vector's methods to insert and remove
http://cplusplus.com/reference/vector/vector/
http://cplusplus.com/reference/vector/vector/push_back/
http://cplusplus.com/reference/vector/vector/pop_back/

The hard way out would be implementing a linked list. But if you just started working with classes it's probably a bad idea
In this case, what you would like is "containment". This means that your list class "owns" a collection of dvd objects. Just like you can have a class which contains members, you can have a class which contains other classes. In this case, you would have a list class which "has-a" DVD. The list class could contain the DVDs in an array, a vector, a list, however for your purpose I'd probably recommend a vector. maeriden has posted some good references to read through.
I thought about doing vectors, and we've learned about vectors, but I'm having a problem creating my list class. If each record in the vector is a DVD object, wouldn't it be a DVD (class) vector? How do I pull that into the list class. For the list class to be able to pull that in and manipulate that data, wont I have to include it somehow? Else, how will the header file recognize it? Do I put something like #include "Dvd.h" at the beginning of the list.h file? Am I making sense? Am I making this too hard?
I really think I'm making this too hard, because we haven't gotten to the part of the book that tells us how to use inheritence in that way. For the list class to "have a " DVD. But in my mind, my thought processes, that's how you would have to do it. If I create a vector in main and use the list class to manipulate it, that if fine. But then what is the point of the Dvd class? The Dvd class is used to create instances of that class. And if I can't manipulate (add, delete, update) Dvd objects and instead have to move them to a standard vector in main and then send them send them to list member functions, it just seems stupid to me.
But then what is the point of the Dvd class?

The DVD class will hold the info about each dvd you create. The purpose of the manager class (the vector should be a member of the class, not created in main) is to provide a simple interface for the operations of adding, removing and modifying the DVDs.

I'm not exactly sure of what your concern is. Do you find stupid creating a class only to manage the vector?
really think I'm making this too hard, because we haven't gotten to the part of the book that tells us how to use inheritence in that way. For the list class to "have a " DVD. But in my mind, my thought processes, that's how you would have to do it.


No. You don't use inheritance to model a "has-a" relationship - you use composition. In other words, for the list class to "have a" DVD, the DVD object should be a member of the List class. If the List class is supposed to own many DVD objects, then you're best off using a container such as a vector, so the vector of DVD objects should be a member of the List class.

Yes, you should use a header file called "DVD.h" containing the definition of the DVD class. This should include the method prototypes, but doesn't need to include the full method definitions - these are better off in a separate DVD.cpp file.

(Inheritance is used to model "is-a" relationships. So, for example, a car is a vehicle, a truck is a vehicle, and a motorbike is a vehicle, so if you had classes Car, Truck and Motorbike, you might have them all inherit from a Vehicle class.
Last edited on
Topic archived. No new replies allowed.