Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
Your Ad Here
Your Ad Here
Private  Constructors

If  a  class  constructor  is  private  or  protected,  the  object  cannot  be  created  in  a scope  where  its  constructor  is  not  visible.  In  the  following  example,   Distance :: Distance ( )  is  declared  private.


 
                   class  Distance
                      {

                                               friend  class  CC;  //  a  friend  class

                                               friend  int  Display ( ) ;  //  a  friend  function

                                 public :

                                              Distance ( float ) ;

                                               // . . . .
                               
                                 private :

                                               Distance ( ) ;

                                               // . . . .
                      } ;




                      class  CC
                      {

                                 public :

                                               void  Compute ( )
                                               {
                                                         Distance  d1 ;    //  ok
                                               }
                      } ;



                      int  Display ( )
                      {
                                        Distance  d2 ;  //  ok
                      }



                     int   main ( )
                     {
                                 Distance  d3 ;  //   error
                                 // . . . .
                     }



Within  the  program,  only  the  Distance  member  functions,  friend  class  CC  and  friend function  Display ( )  may  declare  Distance  objects  that  take  no  argument.  There  is no restriction  on  the  declaration  of  Distance  objects  that  take  an  argument  of  type float.