Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <time.h>
- #include <cstring>
- using namespace std;
- class block_books {
- int type;
- public:
- block_books(){ type=0;};
- block_books(int);
- void set_type(int);
- int get_type();
- friend ostream& operator <<(ostream &,block_books&);
- friend istream& operator >>(istream &,block_books&);
- };
- block_books::block_books(int type):type(type){};
- void block_books::set_type(int type){this->type=type;};
- int block_books::get_type(){ return type;};
- istream& operator >>(istream& in,block_books& obj){
- int temp;
- cout<<"Dati tipul cartii(0-intoarsa , 1-deschisa): ";
- in>>temp;
- if((temp==0)||(temp==1)){
- obj.set_type(temp);
- }
- else {cout<<"A fost introdusa cifra incorecta! ";}
- return in;
- }
- ostream& operator <<(ostream& out,block_books&obj){
- if((obj.type=0)){
- out<<"Tipul cartii: intoarsa"<<endl;
- }
- if((obj.type=1)){
- out<<"Tipul cartii: deschisa"<<endl;
- }
- return out;
- }
- class book:public block_books {
- char* grade;
- char* color;
- public:
- book(){grade=0; color=0;};
- ~book(){delete[]grade; delete[]color;}
- void set_grade(char*);
- void set_color(char*);
- char* get_grade();
- char* get_color();
- friend ostream& operator <<(ostream &,book&);
- friend istream& operator >>(istream &,book&);
- };
- void book::set_grade(char *grade){
- if(this->grade!=NULL)delete[]this->grade;
- this->grade=new char[strlen(grade)+1];
- strcpy(this->grade,grade);
- };
- void book::set_color(char *color){
- if(this->color!=NULL)delete[]this->color;
- this->color=new char[strlen(color)+1];
- strcpy(this->color,color);
- };
- char* book::get_grade(){ return grade;};
- char* book::get_color(){ return color;};
- istream& operator >>(istream& in,book& obj){
- char temp1[100],temp2[100];
- cout<<"Dati gradul cartii: "<<endl;
- in>>temp1;
- cout<<"Dati culoarea cartii: "<<endl;
- in>>temp2;
- obj.set_grade(temp1);
- obj.set_color(temp2);
- return in;
- }
- ostream& operator <<(ostream& out,book&obj){
- out<<"Gradul cartii: "<<obj.get_grade()<<endl;
- out<<"Culoarea cartii: "<<obj.get_color()<<endl;
- return out;
- }
- class post_order:public book {
- int n_order;
- public:
- friend ostream& operator <<(ostream &,post_order&);
- friend istream& operator >>(istream &,post_order&);
- };
- istream& operator >>(istream& in,post_order& obj){
- char temp2[100],temp3[100];
- int temp1;
- cout<<"Dati tipul cartii(0-intoarsa , 1-deschisa): ";
- in>>temp1;
- if((temp1==0)||(temp1==1)){
- obj.set_type(temp1);
- }
- else {cout<<"A fost introdusa cifra incorecta! "; obj.set_type(1);}
- cout<<"Dati gradul cartii: "<<endl;
- in>>temp2;
- obj.set_grade(temp2);
- cout<<"Dati culoarea cartii: "<<endl;
- in>>temp3;
- obj.set_color(temp3);
- return in;
- }
- ostream& operator <<(ostream& out,post_order&obj){
- if((obj.get_type()==0)){
- out<<"Tipul cartii: intoarsa"<<endl;
- }
- if((obj.get_type()==1)){
- out<<"Tipul cartii: deschisa"<<endl;
- }
- out<<"Gradul cartii: "<<obj.get_grade()<<endl;
- out<<"Culoarea cartii: "<<obj.get_color()<<endl;
- return out;
- }
- class post_random:public book {
- public:
- friend ostream& operator <<(ostream &,post_random&);
- friend istream& operator >>(istream &,post_random&);
- };
- ostream& operator <<(ostream& out,post_random&obj){
- out<<"Tipul cartii: "<<obj.get_type()<<endl;
- out<<"Gradul cartii: "<<obj.get_grade()<<endl;
- out<<"Culoarea cartii: "<<obj.get_color()<<endl;
- return out;
- }
- int main(){
- book b;
- post_order p1,p2;
- cout<<"Dati datele pentru cartea 1"<<endl;
- cin>>p1;
- cout<<"Dati datele pentru cartea 2"<<endl;
- cin>>p2;
- post_order p[2]={p1,p2};
- cout<<endl<<"Afisare in ordine:"<<endl;
- cout<<"Cartea 1"<<endl;
- cout<<p[0];
- cout<<"Cartea 2"<<endl;
- cout<<p[1];
- srand(time(0));
- int i=rand()%1;
- cout<<endl<<"Afisare random:"<<endl;
- cout<<"Cartea "<<i+1<<endl;
- cout<<p[i];
- int k;
- if (i==0) k=1;
- else if (i==1) k=0;
- cout<<"Cartea "<<k+1<<endl;
- cout<<p[k];
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement