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;
}
***********************************************************************************
Step by step
Solved in 2 steps