Ask Question
19 March, 09:23

This function receives first_name and last_name, then prints a formatted string of "Name: last_name, first_name" if both names are not blank, or "Name: " with just one of the names, if the other one is blank, and nothing if both are blank.

+5
Answers (1)
  1. 19 March, 11:58
    0
    Following are the program in the C+ + Programming Language.

    //set header file

    #include

    //set namespace

    using namespace std;

    //define class

    class format

    {

    //set access modifier

    public:

    //set string type variable

    string res;

    //define function

    void names (string first_name, string last_name)

    {

    //set if-else if condition to check following conditions

    if (first_name. length () >0 && last_name. length () >0)

    {

    res="Name: "+last_name+", "+first_name;

    }

    else if (first_name. length () >0 and last_name. length () = =0)

    {

    res="Name: "+first_name;

    }

    else if (first_name. length () = =0 and last_name. length () = =0)

    {

    res="";

    }

    }

    //define function to print result

    void out () {

    cout<
    }

    };

    //define main method

    int main () {

    //set objects of the class

    format ob, ob1, ob2;

    //call functions through 1st object

    ob. names ("John","Morris");

    ob. out ();

    //call functions through 2nd object

    ob1. names ("Jhon","");

    ob1. out ();

    //call functions through 3rd object

    ob2. names ("", "");

    ob2. out ();

    }

    Output:

    Name: Morris, John

    Name: Jhon

    Explanation:

    Following are the description of the program:

    Define class "format" and inside the class we define two void data type function. Define void data type function "names () " and pass two string data type arguments in its parameter "first_name" and "last_name" then, set the if-else conditional statement to check that if the variable 'first_name' is greater than 0 and 'last_name' is also greater than 0 then, the string "Name" and the following variables added to the variable "res". Then, set else if to check that if the variable 'first_name' is greater than 0 and 'last_name' is equal to 0 then, the string "Name" and the following variable "first_name" added to the variable "res". Define void data type function "out () " to print the results of the variable "res". Finally, we define main method to pass values and call that functions.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “This function receives first_name and last_name, then prints a formatted string of "Name: last_name, first_name" if both names are not ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers