Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C++, many of the keyboard symbols that are used between two variables can be

ID: 662365 • Letter: I

Question

In C++, many of the keyboard symbols that are used between two variables can be given a new meaning. This feature is called operator overloading and it is one of the most popular C++ features. Any class can choose to provide a new meaning to a keyboard symbol. Not every keyboard letter is redefinable in this way, but many of the ones we have encounted so far are, like + and - and * and / and > and <, for example. It is a class' choice to do this, so mostly it is viewed as a driver code convenience to support them. But because so many of us have an assumption that + should do addition but also perform string concatenation when working with textual data. Each operator becomes a friend function in C++ that your class can implement. The arguments to most of the operator overloads are defined as const SodaCan &. This syntax is a new kind of parameter passing called const-reference parameters. For a classtype, like SodaCan, const & signifies a read-only argument that cannot be changed by the function that receives this object. The compiler enforces this restriction and, for classes, const & simulates a pass-by-value mechanism without the overhead of copying the object, which might take a tremendous amount of time away from our program. Read the book and demo source examples carefully to see how this is done. Trust me, the first time you work with operators in C++, it is a very error-prone process. My best advice to you is to complete one operator at a time.

Project 1: Fuller SodaCan
I have provided you with a sample class named SodaCan which has been diagrammed below. You can acquire the source to the SodaCan class by clicking on the link for your development environment ( VS1010 VS2012 XCode5 ). Using the SodaCan class provided earlier, upgrade the class so that it supports various operators. Make operator+ combine together the contents of two SodaCan, as long as the contents does not exceed the size. Make operator- subtract one SodaCan contents from another, as long as the size or contents don't go negative. Support the >> and << operators to allow instances to be read from cin or written to cout. Make the boolean operators <, >, <= and >= test SodaCan's contents. Make the boolean operator == and != test all of the data members of a SodaCan for an exact match.

SodaCan

bool isEmpty();
void pourInSoda( int amount );
void drink( int amount );
void setSize( int size );
int getSize();
string getBrand();
void setBrand( string brand );
int getContents();

Implementation Details Sample Driver

SodaCan

SodaCan();
SodaCan( string brand, int size, int contents );

bool isEmpty();
void pourInSoda( int amount );
void drink( int amount );
void setSize( int size );
int getSize();
string getBrand();
void setBrand( string brand );
int getContents();

string my_Brand;
int my_Size;
int my_Contents; 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 );

/// brands are not the same...
/// this should print an error!
junk = myPepsi + yourDietCoke;
/// brands are not the same...
/// this should print an error!
junk = myPepsi - yourDietCoke;


/// junk should have 66 ounces
junk = bigGulp + myPepsi;
cout << junk;

if (junk > bigGulp) {
cout <<"junk

Explanation / Answer

#include <iostream>

#include <string>

#include <cstdlib>

using namespace std;

class SodaCan {

public:

SodaCan();

SodaCan(string brand, int size, int contents);

bool isEmpty();

void pourInSoda(int amount);

void drink(int amount);

void setSize(int size);

int getSize() const;

string getBrand() const;

void setBrand(string brand);

int getContents() const ;

friend SodaCan operator+ (const SodaCan& left, const SodaCan& right);

friend SodaCan operator- (const SodaCan& left, const SodaCan& right);

friend bool operator> (const SodaCan& left, const SodaCan& right);

friend bool operator< (const SodaCan& left, const SodaCan& right);

friend bool operator>= (const SodaCan& left, const SodaCan& right);

friend bool operator<= (const SodaCan& left, const SodaCan& right);

friend bool operator== (const SodaCan& left, const SodaCan& right);

friend bool operator!= (const SodaCan& left, const SodaCan& right);

friend std::ostream& operator <<(std::ostream& os, const SodaCan* my_can);

friend std::istream& operator>>(std::istream& ins, SodaCan *&can);

private:

string my_Brand;

int my_Size;

int my_Contents;

};

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