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[i]);
}
}
//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;
} 8 1400
"Johno" <...@...> wrote in message
news:lf******************@news-server.bigpond.net.au... 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
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;
}
"Johno" <...@...> wrote in message
news:cc*******************@news-server.bigpond.net.au... 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<Digital_camera*>& 1st) {
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
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;
}
"Johno" <...@...> wrote in message
news:Y_*******************@news-server.bigpond.net.au... 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<Digital_camera*> my_cameras; 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
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);
}
}
"Johno" <...@...> wrote in message
news:IZ*******************@news-server.bigpond.net.au... 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
Johno wrote: 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: PHPkemon |
last post by:
Hi there,
A few weeks ago I made a post and got an answer which seemed very logical.
Here's part of the post:
PHPkemon wrote:
> I think I've figured out how to do the main things like...
|
by: DKM |
last post by:
Here are the source code files to a Java applet that utilizes
LiveConnect to communicate with Javascript, and the HTML file.
The thing works both in IE 6.0 and FireFox 1.4. but with some...
|
by: baustin75 |
last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie
only when debugging in php designer 2005
--------------------------------------------------------------------------------
...
|
by: Phil IU Guy |
last post by:
I am having 2 issues, both acting very randomly, and for the most part
I dont get this message on most computers, but I have had a couple
computers get either issue 1, or issue 2.
Issue #1: I...
|
by: Tigger |
last post by:
Dear Experts,
I am working on ASP.NET. I have got a problem related to the usage of
Javascript in ASP.NET. Please help. The story is the following:
1) I am developing an ASP.NET application. I...
|
by: ItsMe |
last post by:
Hi Guyz,
I'm developing some application using third party api. They have given me
sample project which is in VB6.0. I'm a newbie in vb.net. and the classic vb
works perfectly alright. My job is...
|
by: Jamiil |
last post by:
This is the first time I am ever using JavaScript, I am not even
elocuent in HTML, all I am is someone who has Microsoft/Netscape
Webpage developers. What I am trying to say is that I am not an...
|
by: rwise5 |
last post by:
I have been tasked with finishing the following C program. I need to develop the sort by birthday function and the print grade function. I have been working on the print function for the last week...
|
by: lintolawrance |
last post by:
hai friends,
i need some help from u.i am trying to work with Ajax.i done some codes for developing dropdownlist using ajax.but it doesn't work.please help me to develop it.
...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |