//professional C++ 4th edition p 285-
#include
#include
#include
#include <string_view>
#include
#include <string_view>
class Muffin{
public:
virtual ~Muffin() = default;
std::string_view getDescription() const;
void setDescription(std::string_view description);
int getSize() const;
void setSize(int size);
void output() const;
bool hasChocolateChips() const;
void setHasChocolateChips(bool hasChips);
private:
std::string mDescription;
int mSize = 0;
bool mHasChocolateChips = false;
};
std::string_view Muffin::getDescription() const {return mDescription;}
void Muffin::setDescription(std::string_view description){
mDescription = description;
}
int Muffin::getSize() const {return mSize;}
void Muffin::setSize(int size) { mSize = size;}
bool Muffin::hasChocolateChips() const { return mHasChocolateChips;}
void Muffin::setHasChocolateChips(bool hasChips){
mHasChocolateChips = hasChips;
}
void Muffin::output() const{
printf("%s, Size is %d, %s\n", getDescription().data(), getSize(),
(hasChocolateChips() ? " has chips " : " no chips "));
}
Muffin createMuffin(std::istringstream& inStream);
bool changeNumberForID(std::string_view filename, int id, std::string_view newNumber);
int main(){
std::cout << “First, let’s create a muffin in code and output it.” << std::endl;
Muffin myMuffin;
myMuffin.setDescription(“Giant_Blueberry_Muffin”);
myMuffin.setSize(42);
myMuffin.setHasChocolateChips(false);
printf(“The muffin is “);
myMuffin.output();
printf(” – yummy!\n”);
std::cout << “Now we’ll create a muffin from a string stream” << std::endl;
std::istringstream instream(“My_Muffin 2 true”);
Muffin m2 = createMuffin(instream);
m2.output();
// std::cout << “Enter tokens.Control+D(Unix) or Control+Z(Windows) to end” << std::endl;
// std::ostringstream outStream; //(2)
// while(std::cin){
// std::string nextToken;
// std::cout << "Next token: ";
// std::cin >> nextToken;
// if(!std::cin || nextToken == “done”)
// break;
// outStream << nextToken << “\t”;
// }
// std::cout << "The end result is: " << outStream.str() << std::endl;
std::ofstream fout ("test.out"); //p289
if(!fout){
std::cerr << "Error opening test.out for writing" << std::endl;
}
//1. Output the string "12345"
fout << "12345";
//2. verify that the marker is at position 5
std::streampos curPos = fout.tellp();
if(5 == curPos){
std::cout << "Test passed: Currently at position 5" <<