xTreme Cpp - Answers II

Xtreme CPP : Answers - Part II

6) Yes, we can make the constructor private. But then the onus is on you to write some way of creating an object of the class.

But private constructor are used practically. In singleton classes. A singleton pattern is a design pattern in which only one object of the class is created and no more.

Singleton is useful when a common resource is shared by many modules. Say you want to use a Logger in many modules. But there should be only one Logger object.

A simple singleton can be written like this

 #include<iostream>  
 using namespace std;  
 class singletondemo  
 {  
    private:  
     singletondemo(){}/*constrctor*/  
     static singletondemo *instance;  
   public:  
     static singletondemo * getInstance()  
     {  
        if(instance==NULL)  
        {  
            instance = new singletondemo();  
        }  
        return instance;  
    }  
 };  
 singletondemo* singletondemo::instance = NULL;   
 int main()  
 {  
     singletondemo *ob1,*ob2;  
     ob1 = singletondemo::getInstance();  
     ob2 = singletondemo::getInstance();  
 }  

Here only one object of singletondemo class is created and both ob1 and ob2 point to that object. Please note that these are pointers, not objects. If we have objects, it will be difficult to define them.

So the class has a private constructor.

7)

Static data of a class

A class can have a data member which is common to all objects of the class. This data will not be an instance member, but a class member.  Then it should be declared as static.

as 

static datatype  nameofvar;

The static data member has a unique property that it can be accessed using just the class name. 

Let us look at a complete example. 

 #include<iostream>  
 using namespace std;  
 class Point  
 {  
   float x;float y;  
  
 public:  
   Point(float x=0,float y=0);  
   void print_point();  
   int get_num_points(); 
   static int num_points;   
 };  
 int Point::num_points=0;  
 Point::Point(float x,float y)  
 {  
   this->x = x;  
   this->y = y;  
   num_points++;  
 }  
 void Point::print_point()  
 {  
   cout<<x<<","<<y<<"\n";  
 }  
 int Point::get_num_points()  
 {  
   return num_points;  
 }  
 int main()  
 {  
   Point p1,p2,p3;  
   p1.print_point();  
   p2.print_point();  
   cout<<"no of points is"<<Point::num_points;  
 }  
Here num_points is a static data member.

But one restriction with static data member is that it should be initialized outside the class as shown in
int Point::num_points=0;

Please remember that there is only one num_points which is shared between p1,p2,p3.

8)

Static member functions

If a member function is declared as static, it belongs to class, not to an object.

One advantage is it can be called without using an object and dot operator, but instead class name and :: (scope resolution operator)

Let us add a static function to the previous class. 


 class Point  
 {  
   float x;float y;  
 public:  
   Point(float x=0,float y=0);  
   void print_point();  
   static int get_num_points();  
   static int num_points;  
 };  
 int Point::num_points=0;  
 int Point::get_num_points()  
 {  
   return num_points;  
 }  
 int main()  
 {  
   cout<<"no of points is"<<Point::get_num_points();  
 }  
Here get_num_points() is a static function.

There are few restrictions for a static function.
  1. A static function can access only the static members of a class.
  2. It can not access this pointer
  3. A static member function can not be
    1. virtual
    2. volatile
    3. const
 A static function can also be called using an object.

Comments

Popular Posts