現在の出力はガベージ 8 桁の値です。加算関数が1と2を加算しない理由がよくわかりません。また、次の関数をどのように実装しますか?
TrashCan other = combined – myCan;
cout << "the other cup's filled to " << other.getUsed( ) << endl;
コードは次のとおりです。
主要:
int main()
{
cout << "Welcome to Howie's TrashCan Program!" << endl;
TrashCan myCan;
TrashCan yourCan;
yourCan.setSize( 12 );
myCan.setSize( 12 );
yourCan.addItem( );
yourCan.addItem( );
myCan.addItem( );
myCan.printCan();
yourCan.printCan();
TrashCan combined = yourCan + myCan;
cout << "this drive's filled to " << combined.getUsed( ) << endl;...
クラス:
class TrashCan {
public:
TrashCan( );
TrashCan( int size );
TrashCan( int size, int contents );
TrashCan operator+(TrashCan);
TrashCan operator-(TrashCan);
void setSize( int size );
void addItem( );
void empty( );
void cover( );
void uncover( );
void printCan( );
int getUsed();
private:
bool myIsCovered;
int my_Size;
int my_Contents;
};
実装: (次の関数のいずれかを台無しにしたと仮定しています)
TrashCan TrashCan::operator+ (TrashCan A)
{
TrashCan combined;
combined.my_Contents= my_Contents + A.my_Contents;
}
int TrashCan::getUsed()
{
return my_Contents;
}