PDA

View Full Version : Modification of a question



R.ANGEL
25th August 2013, 12:19
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

R.ANGEL
26th August 2013, 14:32
does anyone have any knowledge on how to do this, I feel helpless

wysota
26th August 2013, 15:34
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.

R.ANGEL
27th August 2013, 07:13
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.

wysota
27th August 2013, 07:29
Do you understand what are the main differences between a list and a map?

R.ANGEL
27th August 2013, 08:17
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.

wysota
27th August 2013, 08:32
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/

R.ANGEL
29th August 2013, 17:37
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:



#ifndef FILM_H
#define FILM_H

#include <QObject>
#include <QString>
#include <QMap>

class Film : public QObject
{
Q_OBJECT

public:

Film (QString id, QString title, QString dir, QString length, QDate relDate);
Film (QStringList propList);
QString virtual toString (bool labelled, QString sepChar);

private:

QString m_FilmId;
QString m_Title;
QString m_Director;
QString m_FilmLength;
QDate m_ReleaseDate;

};

//Managed collection of pointers

class FilmMap : public QMap<QString, Film*>
{
public:

QString toString() const;
Film* findFilm (QString id);
QString getId (QString title);
void addFilm (Film* film);
void removeFilm (QString filmId);

};

#endif




Below is my implementation file:



#include <QMap>
#include <QMapIterator>
#include "film.h"

using namespace std;

QString FilmMap :: toString () const
{
QString retval;
QTextStream os(&retval);
ConstIterator itr = constBegin();
for (; itr != constEnd(); ++itr)
os << '[' <<itr.key() << ']' << ":"
<< itr.value() -> toString() << endl;
return retval;
}

Film* FilmMap :: findFilm (QString id)
{

}

QString FilmMap :: getId (QString title)
{
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.

wysota
29th August 2013, 20:20
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*>).

R.ANGEL
30th August 2013, 07:46
good morning

I need the map to hold pointers to film instances such as:

QMap<int, Film*>

in my opinion from reading the question.

wysota
30th August 2013, 07:48
Why do you need pointers?

R.ANGEL
31st August 2013, 18:07
I thought that since inheritance was occurring we would need to use pointers.

wysota
2nd September 2013, 21:51
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.

R.ANGEL
3rd September 2013, 06:45
so I guess I have been attempting this solution the incorrect way by inheriting QObject and using pointers

wysota
3rd September 2013, 10:50
It is not that it is incorrect, it's just more difficult and simply not required. I suggest you KISS.

R.ANGEL
4th September 2013, 07:35
I am unsure on how to proceed next in completing my solution though

wysota
4th September 2013, 08:31
Remove QObject legacy, change the map definition and implement functions for adding film instances to the map, removing them, etc.

R.ANGEL
5th September 2013, 06:49
ok thank you Sir, I will attempt that and repost my solution