dimanche 27 octobre 2019

Constructor overloading for two types

I am making a class User. I have done some constructor overloading below:

class User
{
    public:
    explicit User(string firstInit, string secondInit){
        this->firstInit = firstInit;
        this->secondInit = secondInit;    
    }//end constructor

    explicit User(char firstInit, char secondInit){
        this->firstInit = firstInit;
        this->secondInit = secondInit;    
    }//end constructor

    explicit User(string firstInit, char secondInit){
        this->firstInit = firstInit;
        this->secondInit = secondInit;    
    }//end constructor    

    explicit User(char firstInit, string secondInit){
        this->firstInit = firstInit;
        this->secondInit = secondInit;    
    }//end constructor        
};

My question relates to constructor overloading. Do I need to provide all 4 examples of a constructor in this case to meet the requirement that:

An initial can be a string or character. It does not matter. But account for all cases where a user may enter one or the other.

For "best practice", did I satisfy the requirement? Or is there a way to declare an enum with only string and char and each variable must be one of those enum?

Aucun commentaire:

Enregistrer un commentaire