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; } ***********************************************************************************
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;
}
***********************************************************************************
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)