好的,所以我正在尝试让我的程序正常工作,但似乎出了点问题,但我不知道为什么。请各位大神看看,告诉我有什么问题吗?
问题是:减法不能正常工作:c
这是 Main 函数 (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(类):
#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();
}
编号.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