Write a Object Oriented C++ program using Inheritance to check whether a given number is perfect or not ?
#include<iostream>
using namespace std;
class perfect
{
public:
void setnum (int digit)
{
number = digit;
}
protected:
int number;
};
class chknumber : public perfect
{
public:
int sum=0;
void getperfect ( )
{
for ( int i=1; i<number; i++ )
{
if ( number%i = =0 )
{
sum += i;
}
}
}
int chksum( )
{
if (number = = sum)
cout << "Yes!!" << endl<< "the given number is perfect number" << endl << endl;
else
cout << "Not!!"<< endl << "the given number is not a perfect number" << endl;
}
};
int main( )
{
chknumber per;
int digit;
cout << "Enter a number to check perfect or not : ";
cin >> digit;
per.setnum(digit);
per.getperfect();
per.chksum();
return 0;
}