for the code below do the following: Convert the class to use std::optional instead of the pair of std::string and a flag. ***************************************** Demo.cpp /* Name_u.cpp Example use program for Name */ #include #include "Name.hpp" int main() { Name fred("Fréd", "Smith"); fred.output(std::cout) << '\n'; Name jane("Jane", "Morris", "Goodall"); jane.output(std::cout) << '\n'; Name lovejoy("Sting"); lovejoy.output(std::cout) << '\n'; return 0; } ********************************************** name.hpp   /* Name.hpp Declarations for multi-part name class */ #ifndef INCLUDED_NAME_HPP #define INCLUDED_NAME_HPP #include #include #include class Name { public: // first, middle, and last name constructor Name(std::string_view firstName, std::string_view middleName, std::string_view lastName); // first and last name constructor Name(std::string_view firstName, std::string_view lastName); // last name constructor Name(std::string_view lastName); // first name std::string first() const; // middle name std::string middle() const; // last name std::string last() const; // output the name, single space between the parts of the name std::ostream& output(std::ostream& out) const; private: std::string firstName; bool hasFirstName = false; std::string middleName; bool hasMiddleName = false; std::string lastName; bool hasLastName = false; }; #endif   ******************************************** name.cpp /* Name.cpp Definitions for multi-part name class */ #include "Name.hpp" // first, middle, and last name constructor Name::Name(std::string_view firstName, std::string_view middleName, std::string_view lastName) : firstName(firstName), hasFirstName(true), middleName(middleName), hasMiddleName(true), lastName(lastName), hasLastName(true) {} // first and last name constructor Name::Name(std::string_view firstName, std::string_view lastName) : firstName(firstName), hasFirstName(true), lastName(lastName), hasLastName(true) {} // last name constructor Name::Name(std::string_view lastName) : lastName(lastName), hasLastName(true) {} // first name std::string Name::first() const { return firstName; } // middle name std::string Name::middle() const { return middleName; } // last name std::string Name::last() const { return lastName; } // output the name, single space between the parts of the name std::ostream& Name::output(std::ostream& out) const { // output first name if it exists bool prevName = false; if (hasFirstName) { out << firstName; prevName = true; } // output middle name if it exists if (hasMiddleName) { if (prevName) out << " "; out << middleName; prevName = true; } // output last name if it exists if (hasLastName) { if (prevName) out << " "; out << lastName; } return out; } ***********************************************************************************

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter13: Overloading And Templates
Section: Chapter Questions
Problem 38SA
icon
Related questions
Question

for the code below do the following:

Convert the class to use std::optional<std:string> instead of the pair of std::string and a flag.

*****************************************

Demo.cpp

/*
Name_u.cpp

Example use program for Name
*/

#include <iostream>
#include "Name.hpp"

int main() {

Name fred("Fréd", "Smith");
fred.output(std::cout) << '\n';

Name jane("Jane", "Morris", "Goodall");
jane.output(std::cout) << '\n';

Name lovejoy("Sting");
lovejoy.output(std::cout) << '\n';

return 0;
}

**********************************************

name.hpp

 

/*
Name.hpp

Declarations for multi-part name class
*/

#ifndef INCLUDED_NAME_HPP
#define INCLUDED_NAME_HPP

#include <ostream>
#include <string>
#include <string_view>

class Name {
public:

// first, middle, and last name constructor
Name(std::string_view firstName, std::string_view middleName, std::string_view lastName);

// first and last name constructor
Name(std::string_view firstName, std::string_view lastName);

// last name constructor
Name(std::string_view lastName);

// first name
std::string first() const;

// middle name
std::string middle() const;

// last name
std::string last() const;

// output the name, single space between the parts of the name
std::ostream& output(std::ostream& out) const;

private:
std::string firstName;
bool hasFirstName = false;
std::string middleName;
bool hasMiddleName = false;
std::string lastName;
bool hasLastName = false;
};

#endif

 

********************************************

name.cpp

/*
Name.cpp

Definitions for multi-part name class
*/

#include "Name.hpp"

// first, middle, and last name constructor
Name::Name(std::string_view firstName, std::string_view middleName, std::string_view lastName)
: firstName(firstName), hasFirstName(true),
middleName(middleName), hasMiddleName(true),
lastName(lastName), hasLastName(true)
{}

// first and last name constructor
Name::Name(std::string_view firstName, std::string_view lastName)
: firstName(firstName), hasFirstName(true),
lastName(lastName), hasLastName(true)
{}

// last name constructor
Name::Name(std::string_view lastName)
: lastName(lastName), hasLastName(true)
{}

// first name
std::string Name::first() const {

return firstName;
}

// middle name
std::string Name::middle() const {

return middleName;
}

// last name
std::string Name::last() const {

return lastName;
}

// output the name, single space between the parts of the name
std::ostream& Name::output(std::ostream& out) const {

// output first name if it exists
bool prevName = false;
if (hasFirstName) {
out << firstName;
prevName = true;
}

// output middle name if it exists
if (hasMiddleName) {
if (prevName)
out << " ";
out << middleName;
prevName = true;
}

// output last name if it exists
if (hasLastName) {
if (prevName)
out << " ";
out << lastName;
}

return out;
}
***********************************************************************************

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning