English 中文(简体)
C++使用班级的工厂编码
原标题:Coding for Factorials in C++ using classes
  • 时间:2015-06-28 22:29:29
  •  标签:
  • c++

在我的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
}
问题回答

这是sil的派任。 一项保理职能不是物体的适当成员,而是满足贵重要求,例如:

struct factorialClass{
  int FactNum(int number = 1) {  // default argument helps with the first call
    if(number <= 1) return 1;
    return number * FactNum(number - 1);
}

Functions don t do input, they do arguments.

创建<条形码> 页: 1

class factorialClass
{
};

现在增加计算要素的职能。 <>FactNum(int)

class factorialClass
{
public:
     int FactNum(int x)
     {
        //code to compute factorial
        //return result
     }
};

用司机班进行测试。

    #include<iostream>
        using namespace std;
    
    //create a class named factorial
    
        class factorial{
            private:
            int num,n=1;
            public:
    // create function getnum for getting the number you want factorial of.
            void getnum();
    // create function getfactorial and use any of your desired loop. 
            void getfactorial();
    //create function display for couting the value of the factorial.
            void display();
        };
    //you can access the function outside the class as well.
        void factorial::getnum()
        {
            cout<<"Enter the number you want to get factorial of";
            cin>>num;
        }
        void factorial::getfactorial()
        {
            for ( int i=num; i>0; --i){
                n=n*i; 
            }
        }
        void factorial::display()
        {
             cout<<n<<endl;
        }
        int main()
        {
            factorial obj;
//recall the functions here.
            obj.getnum();
            obj.getfactorial();
            obj.display();
        }




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签