如何使readNo方法私有

时间:2016-07-10 04:47:59

标签: c++ c++11

这是我的代码。 wriiten for to to armstrong,factorial等现在我想readNo方法私有我该怎么办..?

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class Num_Demo
{
   public:
      int num;
      void readNo(int no)
      {
         num=no;
      }
      int Factorial (int a)
      {
         if(a!=0)
         {
            int f=1;
            for (int i=1;i<=a;i++)
            {
               f=f*i;
            }
            return f;
         }
         else
         {
            return 0;
         }
      }

      int Reverse(int b)
      {
         int rev=0,rem;
         while(b!=0)
         {
            rem=b%10;
            rev=(rev*10)+rem;
            b=b/10;
         }
         return rev;
      }

      void Palindrome (int c)
      {
         int num;
         int rev=0,rem;
         num=c;
         while(c!=0)
         {
            rem=c%10;
            rev=(rev*10)+rem;
            c=c/10;
         }
         if(num==rev)
         {
            cout<<"   Number Is Palindrome";
         }
         else
            cout<<"   Number is Not Plaindrome";
      }

      void Armstrong (int d)
      {
         int sum=0,n1,copy;
         copy=d;
         while(d!=0)
         {
            n1=d%10;
            sum=sum+n1*n1*n1;
            d=d/10;
         }
         if(sum==copy)
         {
            cout<<"   Number Is Armstrong";
         }
         else
            cout<<"   Number is Not Armstrong";
      }

};

int main()
{   clrscr();

   Num_Demo nd1,nd2,nd3,nd4;
   int n1,n2,n3,n4;
   cout<<"\n\nEnter The Number To Find Factorial\t";
   cin>>n1;
   nd1.readNo(n1);
   cout<<"   The Factorial Is\t"<<nd1.Factorial(n1);

   cout<<"\n\nEnter The Number To Find Reverse Number\t";
   cin>>n2;
   nd2.readNo(n2);
   cout<<"   The Reverse Is\t"<<nd2.Reverse(n2);

   cout<<"\n\nEnter The Number To Find Palindrome\t";
   cin>>n3;
   nd3.readNo(n3);
   nd3.Palindrome(n3);

   cout<<"\n\nEnter The Number To Find Armstrong\t";
   cin>>n4;
   nd4.readNo(n4);
   nd3.Armstrong(n4);
   getch();
   return 0;
}

现在我想将readNo方法设为私有。我该怎么办..?我把readNo放在公共错误之外&#34; readNo不可访问&#34;弹出。请帮帮我。

2 个答案:

答案 0 :(得分:1)

您不在main函数中使用私有函数。这是不可接受的。

答案 1 :(得分:1)

您在类定义中将函数放在private:标记下,但是您无法在类外部调用它,因此您需要公共函数,它将在类中调用私有函数。