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

📄 main.cpp

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

using namespace std;

class Peripheral {
public:
    string Name;
    int Price;
    int SerialNumber;
    Peripheral(string aname, int aprice, int aserial) :
        Name(aname), Price(aprice),
        SerialNumber(aserial) {}
};

class Printer : public Peripheral {
public:
    enum PrinterType {laser, inkjet};
    PrinterType Type;
    Printer(string aname, PrinterType atype, int aprice,
        int aserial) : Type(atype), 
        Peripheral(aname, aprice, aserial) {}
};

typedef map<string, Peripheral *> PeripheralMap;
class PeripheralList {
public:
    PeripheralMap list;
    virtual Peripheral *GetPeripheralByName(string name);
    virtual void AddPeripheral(string name, Peripheral *per);
};

class PrinterList : public PeripheralList {
public:
    Printer *GetPeripheralByName(string name);
    void AddPeripheral(string name, Printer *per);
};


Peripheral *PeripheralList::GetPeripheralByName
(string name){
    return list[name];
}

void PeripheralList::AddPeripheral(
string name, Peripheral *per) {
    list[name] = per;
}

Printer *PrinterList::GetPeripheralByName(string name) {
    return static_cast<Printer *>(
        PeripheralList::GetPeripheralByName(name));
}

void PrinterList::AddPeripheral(string name, Printer *per) {
    cout << "adding" << endl;
    list[name] = per;
}

int main(int argc, char *argv[])
{
    PrinterList list;
    list.AddPeripheral(string("Koala"),
        new Printer("Koala", Printer::laser, 
        150, 105483932)
    );
    list.AddPeripheral(string("Bear"),
        new Printer("Bear", Printer::inkjet, 
        80, 5427892)
    );
    
    Printer *myprinter = list.GetPeripheralByName("Bear");
    if (myprinter != 0) {
        cout << myprinter->Price << endl;
    }
    system("PAUSE");	
    return 0;
}



⌨️ 快捷键说明

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