2. #include<bits/stdc++.h> using namespace std; class animal { public: animal(){} animal(int x):a(x){} ~animal(){} protected: int a; }; class cat1 : virtual public animal { public: cat1(int x,int y):b(x),animal(y){} cat1(){} ~cat1(){} protected: int b; }; class cat2 : virtual public animal { public: cat2(){} cat2(int x,int y):c(x),animal(y){} ~cat2(){} protected: int c; }; class cat3: public cat1,public cat2 { public: cat3(){} cat3(int x,int y,int z,int k):d(x),cat1(y,k),cat2(z,k),animal(k){} ~cat3(){} display() { cout << a << " " << b << " " << c << " " << d << endl; } protected: int d; }; int main() { cat3 x(1,2,3,4); x.display(); animal y(1); return 0; } 3. #include<bits/stdc++.h> using namespace std; namespace d1 { class is_error//自定义异常类 { public: is_error():s("y == 0 can't be divide !"){} string s; }; void work(double x,double y)//x / y º¯Êý { try { if(!y) throw is_error(); cout << " x / y = " << x/y << endl; } catch(is_error & x) { cout << x.s << endl; } } } namespace d2 { class is_error { public: is_error():s("y == 0 can't be divide !"){} string s; }; void work(double x,double y)//x / y º¯Êý { try { if(!y) throw is_error(); cout << " x / y = " << x/y << endl; } catch(is_error & x) { cout << x.s << endl; } } } int main() { int x,y; scanf("%d%d",&x,&y); d1 :: work(x,y); d2 :: work(y,x); return 0; } 4. #include<bits/stdc++.h> using namespace std; class student { public: student(){} student(int x,int y):a_grade(x),b_grade(y){} ~ student(){} int a_grade,b_grade; }; bool cmp(const student &a,const student &b) { if(a.a_grade == b.a_grade) return a.b_grade < b.b_grade; return a.a_grade < b.a_grade; } int main() { vector<int>v; for(int i = 1; i <= 10; i ++) { int x; scanf("%d",&x); v.push_back(x); } sort(v.begin(),v.end()); for(int i = 0; i < v.size(); i ++) printf("%d%c",v[i],i < v.size() - 1 ? ' ':'\n'); vector<student>vs; for(int i = 1; i <= 5; i ++) { int x,y; scanf("%d%d",&x,&y); vs.push_back(student(x,y)); } sort(vs.begin(),vs.end(),cmp); ofstream outfile; outfile.open("out.txt",ios::out); if(!outfile) cout << " open failed !" << endl; for(int i = 0; i < vs.size(); i ++) outfile << vs[i].a_grade << " " << vs[i].b_grade << endl; outfile.close(); ifstream infile; infile.open("out.txt",ios::in); for(int i = 0; i < vs.size(); i ++) infile >> vs[i].a_grade >> vs[i].b_grade; for(int i = 0; i < vs.size(); i ++) cout << vs[i].a_grade << " " << vs[i].b_grade << endl; infile.close(); return 0; }
