And here's my application file(which I did not write, the teacher did), however, the other overloaded functions( ==,>, < etc.) are written by me:
Code:
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdexcept>
#include "SodaCan.h"
//operator functions declaration
const SodaCan operator +( SodaCan &x1, SodaCan &x2);
const SodaCan operator -( SodaCan &x1, SodaCan &x2);
bool operator >( SodaCan &x1, SodaCan &x2);
bool operator >=( SodaCan &x1, SodaCan &x2);
bool operator <( SodaCan &x1, SodaCan &x2);
bool operator <=( SodaCan &x1, SodaCan &x2);
bool operator !=( SodaCan &x1, SodaCan &x2);
bool operator ==( SodaCan &x1, SodaCan &x2);
int main( ) {
SodaCan myPepsi;
SodaCan yourDietCoke;
SodaCan bigGulp;
SodaCan junk;
bigGulp.setBrand( "Pepsi" );
bigGulp.setSize( 128 );
myPepsi.setBrand( "Pepsi" );
myPepsi.setSize( 12 );
yourDietCoke.setBrand( "Coke" );
yourDietCoke.setSize( 12 );
myPepsi.pourInSoda( 2 );
yourDietCoke.pourInSoda( 3 );
bigGulp.pourInSoda( 64 );
/// junk should have 76 ounces
junk = bigGulp + myPepsi;
cout << junk;
if (junk > bigGulp) {
cout <<"junk’s bigger"<< endl;
}
if (junk >= bigGulp) {
cout <<"junk’s bigger or equal"<< endl;
}
if (myPepsi < junk) {
cout<<"myPepsi’s smaller"<<endl;
}
if (myPepsi <= junk) {
cout<<"myPepsi’s smaller or equal"<<endl;
}
if (myPepsi != junk) {
cout <<"myPepsi != junk!"<<endl;
}
if (myPepsi == myPepsi) {
cout <<"equal test works"<<endl;
}
SodaCan * ptrCan = new SodaCan( "Coke", 25, 3 );
SodaCan * nullCan = NULL;
// be careful
cout << nullCan << endl;
cout << ptrCan << endl;
cin >> ptrCan;
// notice the difference
cout << ptrCan << endl;
ptrCan->pourInSoda( 10 );
/// notice the difference
cout << ptrCan << endl;
delete (ptrCan);
// why can't I do this???
// delete( nullCan );
return( 0 );
}
//operator functions declaration
//every soda can needs three properties: brand, size and content.
//this funtion returns a SodaCan object, with these three properties
const SodaCan operator +( SodaCan &x1, SodaCan &x2){
string firstCan = x1.getBrand();
string secCan = x2.getBrand();
string sumBrand;
int sumSize=0;
int sumContents=0;
if(firstCan != secCan )
{
cout<<"Brands are not the same, it is not possible to do addition!"<<endl;
}
else
{
sumBrand = x1.getBrand(); //the return object's brand
if(x1.getSize() > x2.getSize()){
sumSize = x1.getSize();
}
else{
sumSize = x2.getSize(); //the retrun object's size
}
sumContents = x1.getContents() +x2.getContents(); //the return onject's content
}
return SodaCan(sumBrand, sumSize, sumContents); //object is returned
}
const SodaCan operator -( SodaCan &x1, SodaCan &x2)
{
string firstCan = x1.getBrand();
string secCan = x2.getBrand();
string sumBrand;
int sumSize=0;
int sumContents=0;
if(firstCan != secCan )
{
cout<<"Brands are not the same, it is not possible to do suntraction!"<<endl;
}
else
{
sumBrand = x1.getBrand(); //the return object's brand
if(x1.getSize() > x2.getSize()){
sumSize = x1.getSize();
}
else{
sumSize = x2.getSize(); //the retrun object's size
}
sumContents = x1.getContents() - x2.getContents(); //the return onject's content
}
return SodaCan(sumBrand, sumSize, sumContents);
}
bool operator >( SodaCan &x1, SodaCan &x2)
{
return x1.getContents() > x2.getContents();
}
bool operator >=( SodaCan &x1, SodaCan &x2)
{
return x1.getContents() >= x2.getContents();
}
bool operator <( SodaCan &x1, SodaCan &x2)
{
return x1.getContents() < x2.getContents();
}
bool operator <=( SodaCan &x1, SodaCan &x2)
{
return x1.getContents() <= x2.getContents();
}
bool operator !=( SodaCan &x1, SodaCan &x2)
{
return (x1.getContents() != x2.getContents() || x1.getBrand() != x2.getBrand() || x1.getSize() != x2.getSize());
}
bool operator ==( SodaCan &x1, SodaCan &x2)
{
return (x1.getContents() == x2.getContents() && x1.getBrand() == x2.getBrand() && x1.getSize() == x2.getSize());
}