板凳——————————————————c++(25)

//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" << 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值