help developing function

J

Johno

I have written the two associated base classes below (Digital_camera and
Review) to manage digital camera and review objects. They are base classes
for which other derived classes can be written to provide more detail.
You'll notice that I've also declared the display functions as "virtual" to
allow for polymorphism. I now need to write a function to display all the
contents of an STL "deque" of pointers to Digital_camera objects. It needs
to call display functions to print out the details of the objects pointed
to.

I'm confused on how to do this and I'd greatly appreciate some help with
developing the function with an explanation of any code provided.

The function will go into an implementation file which will also set up
camera a review objects for testing.

Thanks for any help.

//Digital_camera.h



#ifndef DIGITAL_CAMERA_H

#define DIGITAL_CAMERA_H



#include <string>

#include <iostream>

using namespace std;



#include "Review.h"



const int MAX_REVIEWS = 20;



class Digital_camera

{

friend ostream& operator<< (ostream&, const Digital_camera&);



public :

//constructor

Digital_camera ( string ma="", string mo="", string se="", double
me=0.0);



//member functions

void add_review (Review*);

string getMake () const;

string getModel () const;

string getSensor () const;

double getMegapixels() const;

virtual void display (ostream& out=cout) const;

void display_reviews (ostream& out=cout) const;





protected :

//member data

string make; //make of camera

string model; //model of camera

string sensor; //sensor type

double megapixels; //amount of pixels



//attributes for association

Review* camera_review[MAX_REVIEWS];

int nReviews;



};



ostream& operator<< (ostream&, const Digital_camera&);



#endif



//Digital_camera.cpp



#include "Digital_camera.h"

#include "Review.h"

#include <string>

#include <iostream>

using namespace std;



//constructor

Digital_camera::Digital_camera (string ma, string mo, string se, double me)

{

make = ma;

model = mo;

sensor = se;

megapixels = me;



nReviews = 0;

for (int j=0; j<MAX_REVIEWS;j++)

camera_review[j] = NULL;

}



//add a review to a camera object

void Digital_camera::add_review (Review* r)

{

if ( r == NULL)

{

cerr << "incorrectly entered information, NO review added" <<
endl;

}

else if (nReviews<MAX_REVIEWS)

{

camera_review[nReviews++] = r;

}

else cout << "This camera has reached the maximum allowable amount of
reviews" << endl;

}



//access to attributes of a review

string Digital_camera::getMake (void) const

{

return make;

}



string Digital_camera::getModel (void) const

{

return model;

}



string Digital_camera::getSensor (void) const

{

return sensor;

}



double Digital_camera::getMegapixels (void) const

{

return megapixels;

}



//display review details

void Digital_camera::display (ostream& out ) const

{

cout << "Digital Camera details as follows : " << endl << endl;

out << "Make : " << make
<<endl

<< "Model : " << model
<<endl

<< "Sensor type : " << sensor <<endl

<< "Megapixels : " << megapixels <<endl

<< endl;

}



//display review objects

void Digital_camera::display_reviews (ostream& out) const

{

if (nReviews==0)

{

out << "No review information :" << endl;

}

else

{

for (int i=0;i<nReviews;i++)

out << *(camera_review);

}

}



//overload of operator<<

ostream& operator<< (ostream& out, const Digital_camera& d)

{

d.display(out);

d.display_reviews(out);

return out;

}





//Review.h



#ifndef REVIEW_H

#define REVIEW_H



#include <string>

#include <iostream>

using namespace std;



class Digital_camera;



class Review

{

friend ostream& operator<< (ostream&, const Review&);



public :

//constructor

Review (Digital_camera* d=NULL, string="", string="", double=0.0,
string="");



//member functions

string getDate() const;

string getAuthor() const;

double getRating() const;

string getComment() const;

Digital_camera* getCamera() const;

virtual void display (ostream& out=cout) const;



private :

//attributes for association

Digital_camera* what; //what camera



//member data

string date; //Date of review

string author; //The author of the review

double rating; //The overall rating

string comment; //Free flow comments



};



//overload of operator<<

ostream& operator<< (ostream&, const Review&);



#endif



//Review.cpp



#include "Review.h"

#include "Digital_camera.h"

#include <string>

#include <iostream>

using namespace std;



//constructor

Review::Review (Digital_camera* dc, string da, string au, double ra, string
co)

{

what = dc;

date = da;

author = au;

rating = ra;

comment = co;



dc->add_review (this); //pointer to the add_review function in the
Digital_camera class

}



//access to attributes of a review

Digital_camera* Review::getCamera () const

{

return what;

}



string Review::getDate (void) const

{

return date;

}



string Review::getAuthor (void) const

{

return author;

}



double Review::getRating (void) const

{

return rating;

}



string Review::getComment (void) const

{

return comment;

}



//display review details

void Review::display(ostream& out) const

{

cout << "Review details :" << endl;

out << "Date of review : " << date << endl

<< "Author : " << author << endl

<< "Overall rating : " << rating << endl

<< "Comments : " << comment<< endl <<
endl;

}



//overload of operator

ostream& operator<< (ostream& out, const Review& r)

{

r.display(out);

return out;

}
 
J

John Harrison

Johno said:
I have written

Really have you? Or has your tutor written this for you?
the two associated base classes below (Digital_camera and
Review) to manage digital camera and review objects. They are base classes
for which other derived classes can be written to provide more detail.
You'll notice that I've also declared the display functions as "virtual" to
allow for polymorphism. I now need to write a function to display all the
contents of an STL "deque" of pointers to Digital_camera objects. It needs
to call display functions to print out the details of the objects pointed
to.

I'm confused on how to do this and I'd greatly appreciate some help with
developing the function with an explanation of any code provided.

OK well if you explain exactly what is confusing you then maybe some one can
help with your confusion.

It seems very straightforward, you already have an operator<< which will
display a Digital_camera and its reviews. Now if you have a deque of cameras
and you want to display all of the cameras then you need a loop (a for loop
for instance) to loop though all of the cameras calling operator<< for each
camera.

That's about five lines of code and you're done. Here's a start

void display_all_cameras(ostream& out, const deque<Digital_camera*>&
cameras)
{
// loop goes here
}

Why not try for yourself, and post the code back here if you get stuck.
No-one will do your homwork for you unless you are prepared to have a go
yourself.

john
 
J

Johno

Yes, I wrote it as well as a number of derived classes as part of a previous
assignment. Considering you believed it was developed by a tutor I'm
guessing it can't be too bad. Strange thing about c++, some things I seem to
get a handle on real easy and others take considerably longer.

Anyway, here is the code I've developed so far; however, it doesn't compile.

Thanks

#include "Digital_camera.h"

#include "Review.h"

#include <deque>

using namespace std;

void display (deque<Digital_camera*>& 1st)

{

deque<Digital_camera*>::const_iterator p;

for (p = 1st.begin(); p != 1st.end(); ++p)

cout << *p << " ";

cout << endl;

}

int main (void)

{

deque<Digital_camera*>;

Digital_camera d1("Sony", "DSC-S85", "CCD", 4.1);

Digital_camera d2("Canon", "EOS-D60", "CMOS", 6.3);

Digital_camera d3("Kodak", "DC-260", "CCD", 1.5);

Review r1(&d1, "4 Dec 03", "Some Bloke",5.4,"Good clean images produced");

Review r2(&d2, "6 Dec 03", "Another Bloke", 7.5, "A great all around
camera");

Review r3(&d2, "8 Dec 03", "George Someone", 3.5, "Great camera");

Review r4(&d3, "9 Dec 03", "Some Dood", 4.5, "Poor quality images");

cin.ignore();

return 0;

}
 
J

John Harrison

Johno said:
Yes, I wrote it as well as a number of derived classes as part of a previous
assignment. Considering you believed it was developed by a tutor I'm
guessing it can't be too bad.

I guess not, its very clean and correct looking code. Most newbies couldn't
write code like that.
Strange thing about c++, some things I seem to
get a handle on real easy and others take considerably longer.

Anyway, here is the code I've developed so far; however, it doesn't compile.

Thanks

#include "Digital_camera.h"

#include "Review.h"

#include <deque>

using namespace std;

void display (deque<Digital_camera*>& 1st)

1st is not a legal variable name. Variables must begin with a letter or
underscore. Also const woul be better, display doesn't not modify the deque,
so you should use const.

void display (const deque said:
{

deque<Digital_camera*>::const_iterator p;

for (p = 1st.begin(); p != 1st.end(); ++p)

cout << *p << " ";

You are missing a level of indirection, p is an iterator to
deque<Digital_camera*>, so *p is a Digital_camera*, but operator << takes
Digital_camera& so you need an extra *

cout << **p << " ";

john
cout << endl;

}

john
 
J

Johno

John,

I've now got code that compiles, I had no idea that figures could not be
used for variable names.

The next step is to store pointers to the Digital_camera objects in a deque
so that I can call the display function. I'm not sure how to do this. Could
you please give me an example/explanation on what I need to do.

The revised code is below,

Thanks,


#include "Digital_camera.h"
#include "Review.h"
#include <deque>
using namespace std;

void display (deque<Digital_camera*>& deq)
{
deque<Digital_camera*>::const_iterator p;
for (p = deq.begin(); p != deq.end(); ++p)
cout << **p << " ";
cout << endl;
}

int main (void)
{

deque<Digital_camera*>;

Digital_camera d1("Sony", "DSC-S85", "CCD", 4.1);
Digital_camera d2("Canon", "EOS-D60", "CMOS", 6.3);
Digital_camera d3("Kodak", "DC-260", "CCD", 1.5);

Review r1(&d1, "4 Dec 03", "Some Bloke",5.4,"Good clean images produced");
Review r2(&d2, "6 Dec 03", "Another Bloke", 7.5, "A great all around
camera");
Review r3(&d2, "8 Dec 03", "George Someone", 3.5, "Great camera");
Review r4(&d3, "9 Dec 03", "Some Dood", 4.5, "Poor quality images");

cin.ignore();
return 0;
}
 
J

John Harrison

Johno said:
John,

I've now got code that compiles, I had no idea that figures could not be
used for variable names.

The next step is to store pointers to the Digital_camera objects in a deque
so that I can call the display function. I'm not sure how to do this. Could
you please give me an example/explanation on what I need to do.

The revised code is below,

Thanks,


#include "Digital_camera.h"
#include "Review.h"
#include <deque>
using namespace std;

void display (deque<Digital_camera*>& deq)
{
deque<Digital_camera*>::const_iterator p;
for (p = deq.begin(); p != deq.end(); ++p)
cout << **p << " ";
cout << endl;
}

int main (void)
{

deque<Digital_camera*>;

You need a variable name here

deque said:
Digital_camera d1("Sony", "DSC-S85", "CCD", 4.1);
Digital_camera d2("Canon", "EOS-D60", "CMOS", 6.3);
Digital_camera d3("Kodak", "DC-260", "CCD", 1.5);

You need to add each camera to the deck, simplest way is to use push_back

my_cameras.push_back(&d1);

What documentation do you have on the STL? You aren't going to get very far
with deque etc. unless you get familiar with looking up this sort of
information. Try this web site if you don't have access to your own
documentation

http://www.dinkumware.com/refxcpp.html
Review r1(&d1, "4 Dec 03", "Some Bloke",5.4,"Good clean images produced");
Review r2(&d2, "6 Dec 03", "Another Bloke", 7.5, "A great all around
camera");
Review r3(&d2, "8 Dec 03", "George Someone", 3.5, "Great camera");
Review r4(&d3, "9 Dec 03", "Some Dood", 4.5, "Poor quality images");

Presumably you need to add some reviews to some cameras. I'll leave you to
figure that one out.
cin.ignore();
return 0;
}

john
 
J

Johno

Thanks John,

I've got it working. I now have to write a comparison function that when
given two pointers to Digital_camera objects decides which of the two should
come first in sorted order. The sorting order is in decreasing order of
megapixels. Where megapixels are the same, in increasing alphabetical order
of make. Where megapixels and make are the same, in increasing alphabetical
order of model. When the function is complete I'm required to use it with
STL's sort() algorithm.

The code for the function is as follows; however, it's not working. Can I
please ask you to have a look at it and tell me what I need to do to get it
working correctly.

Thanks,

bool compare operator() (Digital_camera* a, Digital_camera* b)

{

double first = a.getMegapixels();

double second = b.getMegapixels();

string third = a.getMake();

string fourth = b.getMake();

string fifth = a.getModel();

string sixth = b.getModel();


if (first != second)

{

return (first < second);

}

else if (third != fourth);

{

return (third < fourth);

}

else

{

return (fifth < sixth);

}

}
 
J

John Harrison

Johno said:
Thanks John,

I've got it working. I now have to write a comparison function that when
given two pointers to Digital_camera objects decides which of the two should
come first in sorted order. The sorting order is in decreasing order of
megapixels. Where megapixels are the same, in increasing alphabetical order
of make. Where megapixels and make are the same, in increasing alphabetical
order of model. When the function is complete I'm required to use it with
STL's sort() algorithm.

The code for the function is as follows; however, it's not working. Can I
please ask you to have a look at it and tell me what I need to do to get it
working correctly.

Thanks,

bool compare operator() (Digital_camera* a, Digital_camera* b)

{

double first = a.getMegapixels();

double second = b.getMegapixels();

string third = a.getMake();

string fourth = b.getMake();

string fifth = a.getModel();

string sixth = b.getModel();


if (first != second)

{

return (first < second);

}

else if (third != fourth);

{

return (third < fourth);

}

else

{

return (fifth < sixth);

}

}

The logic looks fine but because a and b are pointers you must say

double first = a->getMegapixels();
double second = b->getMegapixels();

etc. etc.

Also I'm not sure why you have written

bool compare operator() (Digital_camera* a, Digital_camera* b)

just

bool compare(Digital_camera* a, Digital_camera* b)

looks fine to me.

john
 
T

Thomas Matthews

Johno said:
Thanks John,

I've got it working. I now have to write a comparison function that when
given two pointers to Digital_camera objects decides which of the two should
come first in sorted order. The sorting order is in decreasing order of
megapixels. Where megapixels are the same, in increasing alphabetical order
of make. Where megapixels and make are the same, in increasing alphabetical
order of model. When the function is complete I'm required to use it with
STL's sort() algorithm.

The code for the function is as follows; however, it's not working. Can I
please ask you to have a look at it and tell me what I need to do to get it
working correctly.

Thanks,

bool compare operator() (Digital_camera* a, Digital_camera* b)
bool compare operator(const Digital_camera * a_ptr,
const Digital_camera * b_ptr)
Compare functions generally don't change the data members of
the objects, thus the pointers should be declared to point
to constant data.

{

double first = a.getMegapixels();
double second = b.getMegapixels();
Placing the comparison after the variable declaration will
make the code a bit faster if the condition is favorable.
The remaining function calls would never be executed:
if (first != second)
{
return first < second;
}
Also note that you are using pointers. You need to use
the indirection "->" operator and ALSO CHECK THE POINTERS
FOR NULL BEFORE YOU DEREFERENCE THEM. Don't trust the
clients; make your code robust.


string third = a.getMake();

string fourth = b.getMake();

string fifth = a.getModel();

string sixth = b.getModel();


if (first != second)

{

return (first < second);

}

else if (third != fourth);

{

return (third < fourth);

}

else

{

return (fifth < sixth);

}

}

Another option is to define comparison operators
for Digital_camera class:

class Digital_camera
{
public:
bool operator==(const Digital_camera& dc) const
/* The ending "const" is applied because
* this method does not alter the data
* members.
*/
{
return (model == dc.model)
&& (make == dc.make)
&& (megaPixels == dc.megaPixels);
}
bool operator!=(const Digital_camera& dc) const
{ return !(*this == dc); }
bool operator< (const Digital_camera& dc) const
{
if (megaPixels != dc.megaPixels)
{
return megaPixels < dc.megaPixels;
}
/* ... */
};
};

Usage:
if (camera_a < camera_b)
{
}


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top