在我的C++ OOP类别中接受了以下任务。
将计算系数的以下程序方案改为采用一个类别计算系数的方案。
#include <iostream.h>
int factorial(int);
int main(void) {
int number;
cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.
";
else
cout << number << " factorial is: " << factorial(number) << endl;
}
int factorial(int number) {
int temp;
if(number <= 1) return 1;
temp = number * factorial(number - 1);
return temp;
}
Use the Following Driver Program. A Driver program means that the int main() ... is already written for you. You only need to create the class and add it to the code.
HINT: 查阅以下法典所用类别的名称(要素目录),并研究下文所使用的方法/功能名称(FactNum)。 你们的新阶层
int main(void) {
factorialClass FactorialInstance; //
cout << "The factorial of 3 is: " << FactorialInstance.FactNum(3) << endl;
cout << "The factorial of 5 is: " << FactorialInstance.FactNum(5) << endl;
cout << "The factorial of 7 is: " << FactorialInstance.FactNum(7) << endl;
system("pause"); // Mac user comment out this line
}
我自己做了科索沃的工作,但我正在获得一股错误信息,我不敢肯定我所缺的东西。 我在网上找到了制定保理方案的其他空白,但我不敢确定如何将这种方案与他首选的驾驶代码结合起来。 下面是我所说的话。
#include <iostream>
using namespace std;
class factorialClass{
int f, n;
public:
void factorialClass::FactorialInstance();
{
f=1;
cout<<"
Enter a Number:";
cin>>n;
for(int i=1;i<=n;i++)
f=f*i;
}
}
int main(void) {
factorialClass FactorialInstance; //
FactorialInstance.FactNum();
cout << "The factorial of 3 is: " << FactorialInstance.FactNum(3) << endl;
cout << "The factorial of 5 is: " << FactorialInstance.FactNum(5) << endl;
cout << "The factorial of 7 is: " << FactorialInstance.FactNum(7) << endl;
system("pause"); // Mac user comment out this line
}