I am fairly new to C++ Programming and I'd like some input on this program.
This is a simple program that calculates Factorial of a given integer.
With this program, I attempted to demonstrate the following C++ Concepts:
1. Multi-File
2. Class
3. User-Defined Constructor
4. Recursion
5. << Operator Overloading / friend function
6. Exception Handling
There is a total of 3 files:
1. .cpp that contains the main function
2. .cpp file that contains implementation
3. .h file that contains class definition
Here are the codes.
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include "factorial.h"
int main ()
{
long long number;
bool passed = false;
do
{
try
{
std::cout << "Enter the number (Accurate up to 20): ";
std::cin >> number;
std::cout << std::endl;
if ( number < 0 )
throw number;
if ( !std::cin )
throw number;
passed = true; // If no exception found, end the while-loop
}
catch ( long long x )
{
std::cout << "Non-Positive or Invalid Input: " << x << std::endl;
std::cout << "Restoring the Input Stream...\n\n";
std::cin.clear ();
std::cin.ignore ( 100 , '\n' );
}
}
while ( !passed );
Factorial factorial ( number );
std::cout << factorial;
return 0;
}
|
factorial.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef FACTORIAL_H_INCLUDED
#define FACTORIAL_H_INCLUDED
#include <iostream>
class Factorial
{
long long factorial;
long long number; // Store Original Input
// Demonstrates Direct Recursion
long long calculateFactorial( long long number );
public:
// Default Constructor
Factorial ( long long num );
// Demonstrates << OverLoading
friend std::ostream& operator<< ( std::ostream& os , Factorial factorial );
};
#endif // FACTORIAL_H_INCLUDED
|
factorial.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
#include <iostream>
#include "factorial.h"
// Constructor that Requires long long type variable
Factorial::Factorial ( long long num )
{
number = num;
factorial = calculateFactorial ( number );
}
// Member Function that Utilizes Recursion to calculate Factorial
long long Factorial::calculateFactorial( long long number )
{
if ( number == 0 )
return 1;
else
return number * calculateFactorial ( number - 1 );
}
// Overloaded << Operator that returns the calculated factorial to Standard Output
std::ostream& operator<< ( std::ostream& os , Factorial factorial )
{
os << "The Factorial of " << factorial.number << " is " << factorial.factorial << std::endl;
return os;
}
|
I'd like some feedback on code readability, use of all the C++ concepts, and possibly any tips on how I can get this program to calculate the factorial of a number higher than 20.
Thanks In Advance!