联合体中不能出现结构体

来源:互联网 发布:java语法快速入门 编辑:程序博客网 时间:2024/05/29 19:02

    这两天做了一道c++书的题,本想在公用体中使用结构体,最后发现不能使用。

    特此把代码发出来


#include <iostream>#include <cstdlib>#include <string>#include <fstream>using namespace std;class Books{public:Books(){}virtual string GetName() = 0;virtual void SetName(string) = 0;virtual int GetPrice() = 0;virtual void SetPrice(int) = 0;virtual void GetInfo() = 0;virtual ~Books(){cout<<"基类析构函数"<<endl;} };class Book : public Books{public:Book(string _name = "英国通史", int _price = 55, string _writer = "钱承旦 许洁明", string _publisher = "上海社会科学院出版社", string _ISBN = "978-7-5520-0179-2/K·181"):name(_name), price(_price), writer(_writer), publisher(_publisher), ISBN(_ISBN){}string GetName(){return name;}void SetName(string _name){name = _name;}int GetPrice(){return price;}void SetPrice(int _price){price = _price;}void GetInfo(){cout<<"    书名: "<<name<<endl<<"    作者: "<<writer<<endl<<"    价格: "<<price<<" 元"<<endl<<"  出版社: "<<publisher<<endl<<"    ISBN: "<<ISBN<<endl;}virtual ~Book(){cout<<"派生类Book析构函数"<<endl;}private:    string name;int price;string writer;string publisher;string ISBN;};class Magazine : public Books{public:Magazine(string _name = "格言", int _price = 5, string _ISSN = "1005-0124 ", int _period= 2, int _issue = 628):name(_name),price(_price),ISSN(_ISSN),period(_period),issue(_issue){}string GetName(){return name;}void SetName(string _name){name = _name;}int GetPrice(){return price;}void SetPrice(int _price){price = _price;}void GetInfo(){cout<<"杂志名称: "<<name<<endl<<"    价格: "<<price<<" 元"<<endl<<"  期刊号: "<<ISSN<<endl<<"出版周期: "<<period<<" 周"<<endl<<"    期号: "<<issue<<endl;}~Magazine(){cout<<"派生类Magazine析构函数"<<endl;};private:string name;int price;string ISSN;//国际刊号int period;//周期int issue;//期号};class CD : public Books{public:CD(string _name = "危险之旅 Dangerous", int _price = 66, string _singer = "迈克尔·杰克逊 (Michael Jackson)", string _style= "摇滚Rock", int _time = 181):name(_name),price(_price),singer(_singer),style(_style),time(_time){}string GetName(){return name;}void SetName(string _name){name = _name;}int GetPrice(){return price;}void SetPrice(int _price){price = _price;}void GetInfo(){cout<<"   CD 名: "<<name<<endl<<"    价格: "<<price<<" 元"<<endl<<"  演唱者: "<<singer<<endl<<"    风格: "<<style<<endl<<"  总时长: "<<time<<" 分"<<endl;}~CD(){cout<<"派生类CD析构函数"<<endl;};private:string name;int price;string singer;string style;int time;};class VCD_DVD : public Books{public:VCD_DVD(string _name = "歌声飘过30年", int _price = 66, int _time = 181):name(_name),price(_price),time(_time){}string GetName(){return name;}void SetName(string _name){name = _name;}int GetPrice(){return price;}void SetPrice(int _price){price = _price;}void GetInfo(){cout<<"VCD/DVD : "<<name<<endl<<"    价格: "<<price<<" 元"<<endl<<"  总时长: "<<time<<" 分"<<endl;}~VCD_DVD(){cout<<"派生类VCD_DVD析构函数"<<endl;};private:string name;int price;int time;};typedef struct _book{string writer;string publisher;string ISBN;}_book;typedef struct _magazine{string ISSN;//国际刊号int period;//周期int issue;//期号}_magazine;typedef struct _cd{string singer;string style;int time;}_cd;typedef struct _vcd_dvd{int time;}_vcd_dvd;typedef struct _data{                  /*本想在这里使用联合体省点地方,但是发现编译器报错,最后只能使用结构体了*/_book book;_magazine magazine;_cd cd;_vcd_dvd vcd_dvd;}_data;typedef struct Node{_data data;string name;int price;int type;}Node;int main(){    {    Node n;    n.type = 1;    n.name = "一千零一夜";    n.price = 21;    n.data.book.writer = "阿拉丁";    n.data.book.publisher = "长江文艺出版社";    n.data.book.ISBN = "12356790";    fstream f("E:\\test.txt");    f<<n.type<<" "<<n.name<<" "<<n.price<<" "<<n.data.book.writer<<" "<<n.data.book.publisher<<" "<<n.data.book.ISBN<<endl;    f.close();    Books *b = new Book(n.name,n.price,n.data.book.writer,n.data.book.publisher,n.data.book.ISBN);b->GetInfo();cout<<endl;        Books *c = new Magazine("科学",200,"B1993108",4,278);    c->GetInfo();    cout<<endl;        Books *d = new CD();    d->GetInfo();    cout<<endl;    Books *e = new VCD_DVD();    e->GetInfo();    cout<<endl;        e->~Books();    d->~Books();    b->~Books();    c->~Books();    }system("PAUSE");return 0;}


0 0
原创粉丝点击