I have this homework assignment that requires the creation of two instances of a bottle class in C++. We are to have a copy constructor, any necesarry get/set functions, and overload these operations: >>, <<, ==, !=. I have everything complete except for the operator overloading, which I am having trouble getting to work properly. I tried first with the == operation, because part of the program is about comparing two instances of the bottle. I have tried a variety of tactics, but none of them seem to work. Here is what I have so far:
bottle.cpp
bottle.h:
The part of the operator overloading where I commented part of the parameters out is like that because I decided to try to get the operator overloading to work on one parameter first to see what the problem was. What exactly am i doing wrong with the operator overloading?
bottle.cpp
/**
* @description Implementation of the Bottle class representing a bottle containing some liquid
* with simple operation such as open/close bottle cap,
* fill and pour out content, and empty check
* @written 2/2/2013
* @author Jason Burn
* */
#include <iostream>
#include "Bottle.h"
/**
*@description constructor to create a Bottle object
* with default max volume and actual volume
*/
Bottle::Bottle(){
this->maxVolume = 500;
this->contentType = "";
this->actualVolume = 0;
this->lidIsOpen = false;
}
/**
* @description constructor to create a Bottle object
* with given max volume and actual volume
* @param maxVolum - the given max volume
* @param actualVolume - the given actual volume
* @param contentType - the given type of content
*/
Bottle::Bottle(double maxVolume, double actualVolume, string contentType){
this->maxVolume = maxVolume;
this->contentType = contentType;
this->actualVolume = actualVolume;
this->lidIsOpen = false;
}
/**
* @description close bottle cap
*
*/
void Bottle::close(){
this->lidIsOpen = false;
}
/**
* @description open bottle cap
*
*/
void Bottle::open(){
this->lidIsOpen = true;
}
/**
* @description fill the bottle with the given amount
* @param amount
* @return the actual content amount after filling
*/
double Bottle::fill(double amount){
if(this->lidIsOpen && amount > 0){ //add in the volume
this->actualVolume += amount;
}
//can not overfilled the bottle
if(this->actualVolume > this->maxVolume){
this->actualVolume = this->maxVolume;
}
return this->actualVolume;
}
/**
* @description pour out the request amount from the bottle
* @param amount
* @return the actual amount of content left in the bottle
*/
double Bottle::pour(double amount){
if(this->lidIsOpen && amount > 0){ //remove the amount
this->actualVolume -= amount;
}
if(this->actualVolume < 0){ //it's empty now
this->actualVolume = 0;
}
return this->actualVolume;
}
double Bottle::getMaxVolume(){
return this->maxVolume;
}
double Bottle::getActualVolume()
{
return this->actualVolume;
}
string Bottle::getContents()
{
return this->contentType;
}
bool Bottle::getCapStatus()
{
return this->lidIsOpen;
}
/**
* @description represent the bottle as a string
* @return the representation string
*/
void Bottle::print(){
cout<<"\nContent: " << this->contentType <<
"\nMax Volume: " << this->maxVolume <<
"\nActualVolume: " << this->actualVolume ;
}
//operator overloading of ==
bool Bottle::operator==(Bottle B)/>
{
if (maxVolume == b.getMaxVolume /*&& actualVolume == b.getActualVolume && contentType == b.getContents && lidIsOpen == b.getCapStatus*/)
{
return true;
}
return false;
}
//the following statement will prompt the user for max volume, actual volume and
// content type and fill variable a with the users inputs
int main()
{
Bottle a(500,50,"water");
a.print();
//cin>>a;
//the following statement will give the same output as a.print()
//cout<<a<<endl;
// invoking the copy constructor
Bottle b(a); //make a new object b with identical data from a
b.print();
b.open();
b.fill(20.0);
b.close();//fill b with something
//expect true for the following
if(a == B)/> {
cout << "true";
}else {
cout << "false";
}
//expect false for the following
/*if(a == B)/> {
cout <<"true";
}else {
cout <<"false";
}*/
b.print();
//the following statement will give the same output as a.print(), cout<<endl,
// and b.print()
//cout << a<<endl;
//cout << b<<endl;
return 0;
}
bottle.h:
/**
* @description A class interface representing a bottle containing some liquid
* with simple operation such as open/close bottle cap,
* fill and pour out content, and empty check
* @written 1/19/2013
* @author Ken Nguyen
* */
#ifndef __BOTTLE__H__
#define __BOTTLE__H__
using namespace std;
#include <string>
class Bottle {
public:
//constructors
/**
*@description constructor to create a Bottle object
* with default max volume and actual volume
*/
Bottle();
/**
* @description constructor to create a Bottle object
* with given max volume and actual volume
* @param maxVolum - the given max volume
* @param actualVolume - the given actual volume
* @param contentType - the given type of content
*/
Bottle(double maxVol, double actVol, string content);
//public methods
/**
* @description open bottle cap
*
*/
void open();
/**
* @description close bottle cap
*
*/
void close();
/**
* @description fill the bottle with the given amount
* @param amount
* @return the actual content amount after filling
*/
double fill(double amount);
/**
* @description pour out the request amount from the bottle
* @param amount
* @return the actual amount of content left in the bottle
*/
double pour(double amount);
/**
* @description represent the bottle as a string
* @return the representation string
*/
void print();
double getMaxVolume();
double getActualVolume();
string getContents();
bool getCapStatus();
bool operator==(Bottle B)/>;
//class attributes
private:
double maxVolume,
actualVolume;
string contentType;
bool lidIsOpen; //status of the cap
};
#endif
The part of the operator overloading where I commented part of the parameters out is like that because I decided to try to get the operator overloading to work on one parameter first to see what the problem was. What exactly am i doing wrong with the operator overloading?