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; } ***********************************************************************************

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education