C++面向对象类的实例题目五
题目描述:
编写一个程序,采用一个类求n!,并输出5!的值。
程序代码:
#include<iostream> using namespace std; class CFactorial { public: CFactorial(int n) { num = n; total = 1; } void calculate() { int n = num; while(n>0) { total *= n--; } } void display() { cout<<num<<"! = "<<total<<endl; } private: int num; long total; }; int main() { CFactorial f(5); f.calculate(); f.display(); return 0; }
程序输出:
5! = 120