Saturday, January 11, 2014

Chapter:08 In Class Initializers

In class initializer is a basic and relatively easy concept to use. The fundamental logic behind this is to write default common values of your member variables which are same  with all other possible combination of member variables. We should override value of the only members variables which has been again initialized with new value inside your constructor. Otherwise the object would have default in class initializer value for a particular member variable.


This has some great advantage over older ways of doing this. Suppose that we have written a particular class and all constructors. In future if we add any new member variable we would have to go to each possible constructor and put the new member variables with its appropriate default value. However if we use in class initializer to initialize that particular member variable then we would not have to do any changes in the any of the  constructor. That particular member variable would contain the values which we have used while doing in class initializer for all objects of this class.


This is important as constructor initializes the class invariants with the appropriate values. So here there must not be any mistake otherwise the result
might be different. As I said I found it very simple and very useful concept. Hence I uses this whereever I found it is applicable and it should be used.



Whenever we write any class, we would like to initialize all the member variables with some appropriate or default values. This is very very  important as once constructor gets called for an object, potentially it can be used by any member function. So to ensure that we normally  write a default constructor which simply initialize all member variable with their default values.


The same thing can be done in more elegant way  if we uses C++11 functionality. We can use in class initializer for all member variable and then we can use the 'default' version of constructor. Now when client code create object of this class, all member variable would have their default values. In addition to that it would be useful while writing parametrized constructor as well. In those function, we simply initialize the member variable for which we have get value from the client code. All other member variables except the one which had received the new values from the constructor argument, would still have default value. To understand the real benfit of this method, reader should start using this.




//Code Begins

#include<iostream>
#include<string>

struct information {
    information()=default;
    ~information()=default;
    void display_info(void){
        std::cout<<height<<"\t"<<age<<"\t"<<money<<std::endl;
    }
    int height{};
    int age{};
    float money{};
};


void learn_inclass_initializer(void){
    information oi_x;
    oi_x.display_info();
}

int main(int argc, const char* argv[]) {
    learn_inclass_initializer();
    return 0;
}

//Code Ends



When I run the above program, I get the following output.



//Console Output Begins
$ ./test
0    0    0
//Console Output Ends




The below example is very much similar to the above. However this class has five member variables hence there would be potentially  many possible combination by which these variables can be  initialized. With in class initialize we initialize all member variable with some default values. After this we just write the class to update any variables of a class, other variable would  have default values. Hopefully with this program, reader can understand the usefulness of this concept.




//Code Begins

#include<iostream>
#include<string>

class x {
public:
    x()=default;
    x(std::string n):name(n){};
    x(std::string n,bool pflag):name(n),printflag(pflag){}
    x(int a,int h,std::string n,std::string c,bool pflag):
        age(a),height(h),name(n),country(c),printflag(pflag) {}
    ~x()=default;

    void display_info(void){
        if(printflag) {
            std::cout<<age<<"\t"<<height<<"\t"
                    <<name<<"\t"<<country<<"\t"<<printflag<<std::endl;
        }
    }
private:
    int age{};
    int height{};
    std::string name;
    std::string country{"India"};
    bool printflag{true};
};


void learn_inclass_initializer(void){
    x o_a;
    o_a.display_info();
    std::string name{"Mantosh"};
    x o_b(name);
    o_b.display_info();

    int a{62};
    int h{184};
    std::string n{"Bjarne"};
    std::string c{"USA"};
    bool pflag{true};
    x o_c(a,h,n,c,pflag);
    o_c.display_info();
}

int main(int argc, const char* argv[]) {
    learn_inclass_initializer();
    return 0;
}

//Code Ends




When I run the above program, I get the following output.



//Console Output Begins
$ ./test
0    0           India    1
0    0    Mantosh    India    1
62    184    Bjarne    USA        1
//Console Output Ends




No comments:

Post a Comment