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:igital_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;
}
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:igital_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;
}