1 Attachment(s)
Modification of a question
HI THERE,
I AM 46 AND PRETTY NEW TO LEARNING ABOUT QT USING C++ SO PARDON ME IF IM A LITTLE SLOW IN PROGRESSING
I HAVE PURCHASED THE TEXTBOOK CALLED "AN INTRODUCTION TO DESIGN PATTERNS IN C++ WITH Qt" AND I WANTED TO STUDY THIS BOOK PROPERLY AND UNDERSTAND AND PROGRAM THE EXERCISES BEFORE TAKING UP A COURSE NEXT YEAR WHICH CAN TEACH ME THE MORE COMPLEX STUFF ABOUT THIS PROGRAMMING CONCEPTS. I HAVE BEEN COPING WELL WITH THE EXERCISES THUS FAR AND HAVE BEEN PRODUCING THE REQUIRED OUTPUTS BUT I WOULD LIKE TO KNOW HOW I CAN MODIFY THE ATTACHED PDF QUESTION(FROM THE TEXTBOOK) :
SO THAT FilmList is replaced by the class FilmMap, which inherits from QMap<QString, Film*> where the film id is the key. I want to re-implement all the functions in FilmList in FilmMap.
how do I do this and how do I write my code to test FilmMap and all its functions.
Please assist as I am very new to this and am studying this book completely for my own indulgement without any help.
I have attached the textbook question on which I base my question
Re: Modification of a question
does anyone have any knowledge on how to do this, I feel helpless
Re: Modification of a question
What exactly are you having problems with? All you need to do is to adjust the code from operating on QList to operating on QMap.
Re: Modification of a question
I don't know how to do that though. I have been doing very simple programs so far and I am now moving on to these more complex program, or rather complex for me since I am doing this without guidance. a start would be good please. I am in no rush since I am just playing around with the programs and trying to complete all the textbook examples without looking at the manual answers.
Re: Modification of a question
Do you understand what are the main differences between a list and a map?
Re: Modification of a question
to be honest I don't, but I am using google and other sites to gain as much knowledge on this. any help is greatly appreciated please.
Re: Modification of a question
Quote:
Originally Posted by
R.ANGEL
to be honest I don't, but I am using google and other sites to gain as much knowledge on this.
Great. Come back when you have gained this knowledge. Without it there is no way to progress.
Here is something to get you going:
http://www.cplusplus.com/reference/map/map/
http://www.cplusplus.com/reference/list/list/
Re: Modification of a question
Hi there once again, thank you for the info from those two sites on QMaps and QLists. I understand the differences and concepts much better now after reading that info together with stuff on google. I didn't want to come back and post without attempting any solution to original posted question because that would mean I did not learn anything from the stuff I read. below is some code I wrote, consisting of my understanding of what the header and source file should look like, please check it and correct me if I misunderstood or am doing this incorrectly.
below is my header file:
Code:
#ifndef FILM_H
#define FILM_H
#include <QObject>
#include <QString>
#include <QMap>
{
Q_OBJECT
public:
private:
};
//Managed collection of pointers
class FilmMap : public QMap<QString, Film*>
{
public:
void addFilm (Film* film);
};
#endif
Below is my implementation file:
Code:
#include <QMap>
#include <QMapIterator>
#include "film.h"
using namespace std;
QString FilmMap
:: toString () const {
ConstIterator itr = constBegin();
for (; itr != constEnd(); ++itr)
os << '[' <<itr.key() << ']' << ":"
<< itr.value() -> toString() << endl;
return retval;
}
Film
* FilmMap
:: findFilm (QString id
){
}
{
return Id;
}
void FilmMap :: addFilm(Film* film)
{
insert (film -> getId(), film);
}
void FilmMap
:: removeFilm (QString filmId
) {
}
I have coded what I understood of the question but I do not know how to implement the 'findFilm' and 'removeFilm' iterators, kindly advise me on this please and tell me if I have done the above correctly. I would also appreciate if you could tell me hoe I code the main function in order to test the functions in FilmMap. Thank you so much for all your assistance.
Re: Modification of a question
Quote:
Originally Posted by
R.ANGEL
I didn't want to come back and post without attempting any solution to original posted question because that would mean I did not learn anything from the stuff I read.
Now that's a great attitude! Congratulations!
Before we get into lists and maps, please try to think whether you really need Film to inherit QObject and if not, whether you need the map to hold pointers to Film instances instead of just Film instances (i.e. QMap<int,Film> and not QMap<int,Film*>).
Re: Modification of a question
good morning
I need the map to hold pointers to film instances such as:
QMap<int, Film*>
in my opinion from reading the question.
Re: Modification of a question
Why do you need pointers?
Re: Modification of a question
I thought that since inheritance was occurring we would need to use pointers.
Re: Modification of a question
Quote:
Originally Posted by
R.ANGEL
I thought that since inheritance was occurring we would need to use pointers.
Inheritance itself has nothing to do with this. Only if you inherit QObject you need to use pointers because QObject instances can't be copied. But your class does not require QObject legacy, thus my original question.
Re: Modification of a question
so I guess I have been attempting this solution the incorrect way by inheriting QObject and using pointers
Re: Modification of a question
It is not that it is incorrect, it's just more difficult and simply not required. I suggest you KISS.
Re: Modification of a question
I am unsure on how to proceed next in completing my solution though
Re: Modification of a question
Remove QObject legacy, change the map definition and implement functions for adding film instances to the map, removing them, etc.
Re: Modification of a question
ok thank you Sir, I will attempt that and repost my solution