C++ Program (The TrashCan class is provided at the bottom of this post.) Driver
ID: 3777509 • Letter: C
Question
C++ Program
(The TrashCan class is provided at the bottom of this post.)
Driver Code
TrashCan yours;
TrashCan mine;
TrashCan test;
yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );
yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
cout << test << endl;
try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
TrashCan * ptrCan = new TrashCan( 10, 1 );
TrashCan * nullCan = NULL;
// be careful...
cout << nullCan << endl;
cout << ptrCan << endl;
cin >> ptrCan;
// notice the difference
cout << ptrCan << endl;
ptrCan->addItem( );
// notice the difference
cout << ptrCan << endl;
delete( ptrCan );
// do you realize why I can't say:
// delete( nullCan );
TrashCan class provided:
#include "TrashCan.h"
TrashCan::TrashCan( ) {
myIsCovered = false;
my_Size = 0;
my_Contents = 0;
}
TrashCan::TrashCan( int size ) {
myIsCovered = false;
my_Size = size;
my_Contents = 0;
}
TrashCan::TrashCan( int size, int contents ) {
myIsCovered = false;
my_Size = size;
my_Contents = contents;
}
void TrashCan::setSize( int size ) {
my_Size = size;
}
int TrashCan::getSize( ) {
return( my_Size );
}
int TrashCan::getContents( ) {
return( my_Contents );
}
void TrashCan::addItem( ) {
my_Contents = my_Contents + 1;
}
void TrashCan::empty( ) {
my_Contents = 0;
}
void TrashCan::cover( ) {
myIsCovered = true;
}
void TrashCan::uncover( ) {
myIsCovered = false;
}
void TrashCan::printCan( ) {
cout << "A TrashCan with a size=" << my_Size << " and containing " << my_Contents << " piece";
if (my_Contents != 1) {
cout << "s";
}
cout << " of trash" << endl;
}
TrashCan header for the TrashCan class provided above:
#ifndef TRASHCAN_H
#define TRASHCAN_H
#include
using namespace std;
class TrashCan {
public:
TrashCan( );
TrashCan( int size );
TrashCan( int size, int contents );
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
private:
bool myIsCovered;
int my_Size;
int my_Contents;
};
#endif
Driver Code
TrashCan yours;
TrashCan mine;
TrashCan test;
yours.setSize( 10 );
mine.setSize( 10 );
test.setSize( 5 );
yours.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
mine.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
test.addItem( );
cout << test << endl;
try {
// contents will become negative...
// an exception should get thrown
test = yours - mine;
} catch( std::logic_error le ) {
cout << "subtraction failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative size
// an exception should get thrown
test = TrashCan( -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have 5 pieces of
// trash in a can that
// can only hold 3??
// an exception should get thrown
test.setSize( 3 );
} catch( std::logic_error le ) {
cout << "setSize failed" << endl;
}
/// test should be unchanged
cout << test << endl;
try {
// how can you have a negative
// contents value??
test = TrashCan( 10, -100 );
} catch( std::logic_error le ) {
cout << "constructor failed" << endl;
}
TrashCan * ptrCan = new TrashCan( 10, 1 );
TrashCan * nullCan = NULL;
// be careful...
cout << nullCan << endl;
cout << ptrCan << endl;
cin >> ptrCan;
// notice the difference
cout << ptrCan << endl;
ptrCan->addItem( );
// notice the difference
cout << ptrCan << endl;
delete( ptrCan );
// do you realize why I can't say:
// delete( nullCan );
Explanation / Answer
PROGRAM CODE:
TrashCan.h
/*
* TrashCan.h
*
* Created on: 26-Nov-2016
* Author: kasturi
*/
#ifndef TRASHCAN_H
#define TRASHCAN_H
#include <iostream>
using namespace std;
class TrashCan {
public:
TrashCan( );
TrashCan( int size );
TrashCan( int size, int contents );
void setSize( int size );
int getSize( );
int getContents( );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
friend ostream& operator<<(ostream &outs, const TrashCan can);
friend ostream& operator<<(ostream &out, const TrashCan *can);
friend istream& operator>>(istream &ins, TrashCan &can);
friend istream& operator>>(istream &in, TrashCan* &can);
private:
bool myIsCovered;
int my_Size;
int my_Contents;
};
#endif
TrashCan.cpp
/*
* TrashCan.cpp
*
* Created on: 26-Nov-2016
* Author: kasturi
*/
#include "TrashCan.h"
#include <iostream>
using namespace std;
TrashCan::TrashCan( ) {
myIsCovered = false;
my_Size = 0;
my_Contents = 0;
}
TrashCan::TrashCan( int size ) {
myIsCovered = false;
my_Size = size;
my_Contents = 0;
}
TrashCan::TrashCan( int size, int contents ) {
myIsCovered = false;
my_Size = size;
my_Contents = contents;
}
void TrashCan::setSize( int size ) {
my_Size = size;
}
int TrashCan::getSize( ) {
return( my_Size );
}
int TrashCan::getContents( ) {
return( my_Contents );
}
void TrashCan::addItem( ) {
my_Contents = my_Contents + 1;
}
void TrashCan::empty( ) {
my_Contents = 0;
}
void TrashCan::cover( ) {
myIsCovered = true;
}
void TrashCan::uncover( ) {
myIsCovered = false;
}
void TrashCan::printCan( ) {
cout << "A TrashCan with a size=" << my_Size << " and containing " << my_Contents << " piece";
if (my_Contents != 1) {
cout << "s";
}
cout << " of trash" << endl;
}
std::ostream& operator<<(ostream &outs, const TrashCan can) {
outs << "A TrashCan with a size=" << can.my_Size << " and containing " << can.my_Contents << " piece";
if (can.my_Contents != 1) {
outs << "s";
}
outs << " of trash" << endl;
return outs;
}
std::ostream& operator<<(ostream &out, const TrashCan *can) {
out << "A TrashCan with a size=" << can->my_Size << " and containing " << can->my_Contents << " piece";
if (can->my_Contents != 1) {
out << "s";
}
out << " of trash" << endl;
return out;
}
std::istream& operator>>(istream &ins, TrashCan &can)
{
ins>>can.my_Size>>can.my_Contents;
return ins;
}
std::istream& operator>>(istream &in, TrashCan* &can)
{
in>>can->my_Size>>can->my_Contents;
return in;
}
main.cpp
#include <iostream>
#include "Trashcan.h"
int main()
{
TrashCan * ptrCan = new TrashCan( 10, 1 );
TrashCan * nullCan = NULL;
// be careful...
cout << nullCan << endl;
cout << ptrCan << endl;
cin >> ptrCan;
// notice the difference
cout << ptrCan << endl;
ptrCan->addItem( );
// notice the difference
cout << ptrCan << endl;
delete( ptrCan );
// do you realize why I can't say:
//delete( nullCan );
return 0;
}
OUTPUT:
A TrashCan with a size=5 and containing 5 pieces of trash
A TrashCan with a size=0 and containing 0 pieces of trash
A TrashCan with a size=-100 and containing 0 pieces of trash
A TrashCan with a size=3 and containing 0 pieces of trash
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.