• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

constructor

if constructor declar in private part
0
vipinkumar
Asked:
vipinkumar
1 Solution
 
kulinaCommented:
No it is supposed to be declared in public: part. Constructor is a bridge between private variables and other data in a program.
0
 
kishore_joshiCommented:
You can put the Constroctor in the private domain. As a result only the member functions wil be able to create the objects of that class. But u cannot call a member function without the object.

So, usually if u want to create the object thru a member function, then it should be static function.

If u put the Copy-constructor in the private, then u cannot pass the object of this class by value to any function (member or non-member functions).

Copy constructor can be put in the private domain to impliment Singleton class. i.e. you can place a restriction on the number of objects begin created
of that class.
 
0
 
pagladasuCommented:
>>if constructor declar in private part
Is that a question?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
pagladasuCommented:
>>if constructor declar in private part
Is that a question?
0
 
vipinkumarAuthor Commented:
if a constructor declare in private or procted part
then how many object is created . if not why or if yes why? if possible give an example.
0
 
abesoftCommented:
Making a c'tor private (or protected) will mean that only member functions can use this particular c'tor.  Usually this means that the world at large will not be able to call that c'tor, unless the class re-publishes it through a static function or something like that.

Usually, c'tors are made private so that others can't use them: a copy constructor for a huge object might be a good candidate for this treatment, since it restricts accidental (or intentional) copying of the object.

class HugeDatabase{
private:
    HugeDatabase( const HugeDatabase &copy);
public:
    // Put in a public c'tor so that people can still use the class....
    HugeDatabase();
.....
};

(There are other, more obscure uses of private c'tors, but this is the most common, IMHO)

Gene
0
 
kishore_joshiCommented:
When I refered to the Singleton class, I meant the same thing as Gene's example .

Here is an example of Singleton class .....

class A
{
private:
           static int count;

           A()
           {            count++;
           }

public:
         
      static A* CreateObj()
      {
      A *a = new A;

      if(count > 1)
      {
            cout<<" Crossed the limit........ ";
            return NULL;
      }
      else
      {
            return a;
      }

      } // End of the function CreateObj
};


int A::count; // defining the static variable

void main()
{

      A *r1,*r2 ,*r3;

      r1 = A::CreateObj();
      r2 = A::CreateObj();
      r3 = A::CreateObj();

} // End of main()

Here the Copy Constructor is in private.So u can restrict the no. of objects getting created and alos the object of class A cannot be passed to any function by Value.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now