简体   繁体   中英

Number addition and subtraction in c++ classes

Ok, so I'm trying to get my program work, and it seems like something is wrong, but I don't know why. Could you guys please look at it and tell me whats wrong?

The problem is: Subtracting won't work properly :c

Here's code for Main function (NumberProject.cpp):

//  NumberProject.cpp : Defines the entry point for the console application.

   #include "stdafx.h"
   #include <iostream>
   #include "Number.h"

    using namespace std;

    int main( ) {
//      Variable and Object Declarations
        char answer;
        bool goAgain = true;

        int  integerValue;

        Number numberA;      // Invoke the default constructor.
        Number numberB;      // Invoke the default constructor.

//      User loop
        while ( goAgain ) {
            cout << "Enter an integer:  ";
            cin >> integerValue;
            numberA.Set( integerValue );

            cout << "Enter an integer:  ";
            cin >> integerValue;
            numberB.Set( integerValue );
            cout << endl;

//          Addition
            numberA.Add( numberB );
            cout << "Addition: ";
            numberA.Output( );

//          Subtraction
            numberA.Subtract( numberB );
            cout << "Subtraction: ";
            numberB.Output( );

//          User loop termination code.
            cout << "Would you like to continue? (y/n)" << endl;
            cin >> answer;
            if ( answer == 'n' )
                goAgain = false;
        } // while
        return 0;
    } // Function main( )

Number.cpp (class):

#include "StdAfx.h"
#include "math.h"     // Needed for sqrt and pow.
#include <string>
#include <iostream>
#include "Number.h"

using namespace std;

Number::Number( void ) {
    number = 0;
}

Number::Number( int integerValue ) {
    number = integerValue;
}

Number::~Number( void ) {
}

int Number::Get( ) {
    return number;
}

void Number::Set( int integerValue ) {
    number = integerValue;
}

void Number::Output( ) {
    cout << number << endl;
    cout << endl;
}

void Number::Add( Number otherNumber ) {
    number = number + otherNumber.Get();
}

void Number::Subtract( Number otherNumber ) {
    number = number - otherNumber.Get();
}

Number.h:

#pragma once

class Number {
    public:
        Number( void );
        Number( int );
       ~Number( void );

        int  Number::Get( );       // Accessor
        void Number::Set( int );   // Mutator
        void Number::Output();
        void Number::Add( Number );
        void Number::Subtract( Number );

    private:
        int number;
}; // Class Number
numberA.Subtract( numberB );
cout << "Subtraction: ";
numberB.Output( );

You're outputting the wrong number. You want to print numberA .

numberA.Subtract( numberB );
cout << "Subtraction: ";
numberA.Output( );       // <-- here

Also, when you do the initial addition, you're updating the in-memory value of numberA ; when you go to do the subtraction, numberA will already have the sum of numberA + numberB .

Therefore, when you do numberA - numberB , you're going to have to keep a copy of the value of numberA , not save it's value. Try this:

int Number::Add(Number otherNumber) {
  return number + otherNumber.Get();
}

int Number::Subtract(Number otherNumber) {
  return number - otherNumber.Get();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM