为什么在主类中没有调用类的函数?

时间:2019-12-19 23:49:15

标签: c++ class

我想创建一个计算整数的阶乘的类,我的代码混乱了,我需要一些方向信息。您能帮我发现问题吗?

#include <iostream>

class fact {
private:
    int a;
public:
    fact(){};
    fact(int a){this->a=a;}
    void setfact(int a){this->a=a;}
    int getfact(){return a;}

    int Fact(){
        int i;
        if (a>0){
          for(i=2;i<=a;i++){
            return a=a*i;
          }
        }
        else if (a=0)
            return 1;
        else
            return 0;
     }
};

using namespace std;

int main()
{
    fact b;
    int j;
    cout << "entrer un nombre pour calculer sa factorielle" << endl;
    cin >> j;

    Fact b(j);

    cout << "la factorielle de" << j << "est:" << b.Fact(j);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

感谢您的帮助,现在我的代码可以正常工作了……不管变量和声明的内容如何。

#include <iostream>
using namespace std;

class fact{
private:
    int a;
public:
    fact(){};
    fact(int a){this->a=a;}
    void setfact(int a){this->a=a;}
    int getfact(){return a;}

    int Fact(int a){
        int i;
        int res=1;
        if (a>0){
            for(i=2;i<=a;i++){
                res=res*i;
            }
          return res;
            }

        else if (a==0)
            return 1;

        else
            return 0;
            }
};


int main()
{

    int j;
    std::cout << "entrer un nombre pour calculer sa factorielle" << endl;
    std::cin >> j;
    fact b(j);

    cout << "la factorielle de " << j << " est: " << b.Fact(j);
    return 0;
}
相关问题