C++学习(20)

  1 //图书馆信息-公有继承举例
  2 //图书馆有两种类型的资料:一种是图书,一种是杂志.
  3 //图书和杂志有一些共同的地方,因此可以设计一个资料类作为它们的基类.
  4 //资料类的成员变量包括名字和条码.
  5 //图书类的成员变量除继承基类的名字和条码外,还包括作者和内部分类号
  6 //杂志类的成员变量除继承基类的名字和条码外,还包括卷号
  7 #include<iostream.h>
  8 #include<string.h>
  9 
 10 //资料类
 11 class Retrieval{
 12     protected:
 13         char Title[40];//名字
 14         long Code;//条码
 15     public:
 16         Retrieval();
 17         Retrieval(char *title,long code);
 18         long GetCode(){
 19             return this->Code;
 20         }
 21         void Show();
 22 };
 23 
 24 Retrieval::Retrieval(){
 25     strcpy(Title,"");
 26     Code=0;
 27 }
 28 
 29 Retrieval::Retrieval(char *title,long code){
 30     strcpy(Title,title);
 31     Code=code;
 32 }
 33 
 34 void Retrieval::Show(){
 35     cout<<"资料名字:"<<Title<<'\t'<<Code<<endl;
 36 }
 37 
 38 //图书类
 39 class Book:public Retrieval{
 40     private:
 41         char Author[20];//作者名
 42         char IndexCode[10];//内部分类号
 43     public:
 44         Book();
 45         Book(char *title,long code,char *author,char *indexcode);
 46         void Show();
 47 };
 48 
 49 Book::Book(){
 50     strcpy(Author,"");
 51     strcpy(IndexCode,"");
 52 }
 53 
 54 Book::Book(char *title,long code,char *author,char *indexcode):Retrieval(title,code){
 55     strcpy(Author,author);
 56     strcpy(IndexCode,indexcode);
 57 }
 58 void Book::Show(){
 59     cout<<"图书:"<<endl;
 60     cout<<"名字:"<<Title<<endl;
 61     cout<<"条码:"<<Code<<endl;
 62     cout<<"作者:"<<Author<<endl;
 63     cout<<"内部分类号:"<<IndexCode<<endl;
 64 }
 65 
 66 //杂志类
 67 class Magazine:public Retrieval{
 68     private:
 69         int Volume;//卷号
 70     public:
 71         Magazine();
 72         Magazine(char *title,long code,int vol);
 73         void Show();
 74 };
 75 
 76 Magazine::Magazine(){
 77     Volume=0;
 78 }
 79 
 80 Magazine::Magazine(char *title,long code,int vol){
 81     strcpy(Title,title);
 82     Code=code;
 83     Volume=vol;
 84 }
 85 
 86 void Magazine::Show(){
 87     cout<<"杂志:"<<endl;
 88     cout<<"名字:"<<Title<<endl;
 89     cout<<"条码:"<<Code<<endl;
 90     cout<<"卷号:"<<Volume<<endl;
 91 }
 92 
 93 
 94 //读者类
 95 class Reader{
 96     private:
 97         char Name[20];//读者姓名
 98         long Code;//读者编号
 99 
100         Book *books;//所借图书
101         int CounterB;//所借图书的数量
102         Magazine *magazines;//所借杂志
103         int CounterM;//所借杂志的数量
104     public:
105         Reader();
106         Reader(char *name,long code);
107         ~Reader();
108         
109         void Show();//显示读者的信息
110         void ShowBooks();//显示所有借的图书
111         void AddBook(Book it);//借图书
112         void DelBook(Book it);//还图书
113         void ShowMagazines();//显示所有借的杂志
114         void AddMagazine(Magazine it);//借杂志
115         void DelMagazine(Magazine it);//还杂志
116 };
117 
118 Reader::Reader(){
119     strcpy(Name,"");
120     Code=0;
121     books=new Book[5];
122     CounterB=0;
123     magazines=new Magazine[10];
124     CounterM=0;
125 }
126 
127 Reader::Reader(char *name,long code){
128     strcpy(Name,name);
129     Code=code;
130     books=new Book[5];//默认最多只能借5本图书
131     CounterB=0;
132     magazines=new Magazine[10];//默认最多只能借10本杂志
133     CounterM=0;
134 }
135 
136 Reader::~Reader(){
137     delete books;
138     delete magazines;
139 }
140 
141 //显示读者的信息
142 void Reader::Show(){
143     cout<<"读者:"<<endl;
144     cout<<"姓名:"<<Name<<endl;
145     cout<<"编号:"<<Code<<endl;
146 }
147 
148 //显示所有借的图书
149 void Reader::ShowBooks(){
150     if(CounterB==0){
151         cout<<"图书已经还清"<<endl;
152     }
153     for(int i=0;i<CounterB;i++){
154         books[i].Show();
155     }
156 }
157 
158 //借图书
159 void Reader::AddBook(Book it){
160     if(CounterB<5){
161         books[CounterB]=it;
162         CounterB++;
163     }else{
164         cout<<"你已经借满5本图书"<<endl;
165     }
166 }
167 
168 //还图书
169 void Reader::DelBook(Book it){
170     for(int i=0;i<CounterB;i++){
171         if( books[i].GetCode()==it.GetCode() ){
172             break;
173         }
174     }
175     for(int j=i;j<CounterB;j++){
176         books[j]=books[j+1];
177     }
178     CounterB--;
179 }
180 
181 //显示所有借的杂志
182 void Reader::ShowMagazines(){
183     if(CounterM==0){
184         cout<<"杂志已经还清"<<endl;
185     }
186     for(int i=0;i<CounterM;i++){
187         magazines[i].Show();
188     }
189 }
190 
191 //借杂志
192 void Reader::AddMagazine(Magazine it){
193     if(CounterM<10){
194         magazines[CounterM]=it;
195         CounterM++;
196     }else{
197         cout<<"你已经借满10本杂志"<<endl;
198     }
199 }
200 
201 //还杂志
202 void Reader::DelMagazine(Magazine it){
203     for(int i=0;i<CounterM;i++){
204         if( magazines[i].GetCode()==it.GetCode() ){
205             break;
206         }
207     }
208     for(int j=i;j<CounterM;j++){
209         magazines[j]=magazines[j+1];
210     }
211     CounterM--;
212 }
213 //char *title,long code,char *author,char *indexcode
214 //char *title,long code,int vol
215 int main(){
216     Book b1("c++面向对象程序设计",10001,"朱战立","P306/5");
217     Book b2("数据结构--使用C语言",10002,"朱战立","P306/6");
218 
219     Magazine m1("计算机学报",20001,13);
220     Magazine m2("计算机应用",20002,12);
221     
222     Reader r1("张三",30001);
223     Reader r2("李四",30002);
224     r1.Show();
225     r2.Show();
226 
227     r1.AddBook(b1);
228     r1.AddBook(b2);
229     r1.ShowBooks();
230     r1.DelBook(b1);
231     r1.ShowBooks();
232 
233     r2.AddMagazine(m1);
234     r2.AddMagazine(m2);
235 
236     r2.DelMagazine(m1);
237     r2.ShowMagazines();
238     return 0;
239 }

猜你喜欢

转载自www.cnblogs.com/Tobi/p/9250554.html