#include <string> //使用STL中的string模版類
using namespace std;
//把所有代碼拷貝到vc6或者visual studio 2005中編譯運行即可
//我已經編譯運行成功了
//虛基類
class Med
{
protected:
string title; //名稱
public:
virtual void print() const=0; //純虛函數
virtual string id() const=0; //純虛函數
};
//子類
class Book:public Med
{
private:
string author;//作者
string pub;//出版社
string isbn;//書號
public:
Book(){} //默認構造函數
Book(const char *pauthor,const char *ptitle,const char *ppub,const char *pisbn )
{
author=pauthor;
title=ptitle;
pub=ppub;
isbn=pisbn;
}
void print() const
{
cout<<title<<", Author:"<<author<<", Publisher:"<<pub<<endl;
}
string id() const
{
return isbn;
}
};
class CD:public Med
{
private:
string composer;//制作者
string make;//介質形式
string number;//編號
public:
CD(){};
CD(const char *ptitle,const char *pcomposer,const char *pmake,const char *pnumber)
{
title=ptitle;
make=pmake;
composer=pcomposer;
number=pnumber;
}
void print() const
{
cout<<title<<", CD composer:"<<composer<<endl;
}
string id() const
{
return make+number;
}
};
class Mag:public Med
{
private:
string issn;//雜誌編號
string pub;//出版者
int volume;//卷
int number;//號
public:
Mag(){}
Mag(const char *ptitle,const char *ppub,const char *pissn,const int ivolume,const int inumber)
{
title=ptitle;
pub=ppub;
issn=pissn;
volume=ivolume;
number=inumber;
}
void print() const
{
cout<<title<<", Magazine: Vol."<<volume<<", No."<<number<<endl;
}
string id() const
{
return issn;
}
};
int main()
{
Book book("張三", "C++ 語言程序設計", "清華大學出版社", "0-000-00000-1");
Mag mag("辨析C/C++編程模式", "清華大學出版社", "0000-000X", 020, 01);
CD cd("C++源代碼", "清華大學出版社", "ARCHIV", "000001");
book.print();
cout << "\tid: " << book.id() << endl;
mag.print();
cout << "\tid: " << mag.id() << endl;
cd.print();
cout << "\tid: " << cd.id() << endl;
return 0;
}