⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 C++ Source code from a tutorial
💻 CPP
字号:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>

using namespace std;

class MicrowaveOven {
public:
    int HighVoltageRadiation;
    int RadioactiveFoodCount;
    int LeakLevel;
    string OvenName;
    void WriteToStream(ostream &out);
    void ReadFromStream(istream &in);
    ostream &operator <<(ostream &out);
};

void MicrowaveOven::WriteToStream(ostream &out) {
    out << HighVoltageRadiation << ";";
    out << RadioactiveFoodCount << ";";
    out << LeakLevel << ";";
    out << OvenName << ";";
}

void MicrowaveOven::ReadFromStream(istream &in) {
    char buf[1024];
    in.getline(&(buf[0]), 1024, ';');
    HighVoltageRadiation = atoi(buf);
    in.getline(&(buf[0]), 1024, ';');
    RadioactiveFoodCount = atoi(buf);
    in.getline(&(buf[0]), 1024, ';');
    LeakLevel = atoi(buf);
    in.getline(&(buf[0]), 1024, ';');
    OvenName = buf;
}

/*
// Non-member version
ostream &operator <<(ostream &out, MicrowaveOven &oven) {
    out << "High Voltage Radiation: ";
    out << oven.HighVoltageRadiation << endl;
    out << "Radioactive Food Count: ";
    out << oven.RadioactiveFoodCount << endl;
    out << "Leak Level: ";
    out << oven.LeakLevel << endl;
    out << "Oven Name: ";
    out << oven.OvenName << endl;
    return out;
}
*/

// Member version!
ostream & MicrowaveOven::operator <<(ostream &out) {
    out << "High Voltage Radiation: ";
    out << HighVoltageRadiation << endl;
    out << "Radioactive Food Count: ";
    out << RadioactiveFoodCount << endl;
    out << "Leak Level: ";
    out << LeakLevel << endl;
    out << "Oven Name: ";
    out << OvenName << endl;
    return out;
}

istream &operator >>(istream &in, MicrowaveOven &oven) {
    in >> oven.HighVoltageRadiation;
    in >> oven.RadioactiveFoodCount;
    in >> oven.LeakLevel;
    in >> oven.OvenName;
}

int main(int argc, char *argv[])
{
    MicrowaveOven myoven;
    myoven.HighVoltageRadiation = 2584;
    myoven.RadioactiveFoodCount = 1025;
    myoven.LeakLevel = 4782;
    myoven.OvenName = "Kingzapper";

    ofstream ovenfile("/ovenfile.dat");
    myoven.WriteToStream(ovenfile);
    cout << myoven << endl;
    
    system("PAUSE");
    return 0;
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -