实验6


 1 #include<iostream>
 2 using namespace std;
 3 class vehicle{
 4     public:
 5         vehicle(int m,int w):maxspeed(m),weight(w){
 6         }
 7         void run(){
 8             cout<<"run"<<endl; 
 9         } 
10         void stop(){
11             cout<<"stop"<<endl;
12         }
13     private:
14         int maxspeed, weight;
15 };
16 class bicycle:virtual public vehicle{
17     public:
18         bicycle(int m,int w,int h):height(h),vehicle(m,w){
19         }
20     private:
21         int height;
22 };
23 class motorcar:virtual public vehicle{
24     public:
25         motorcar(int m,int w,int s):seatnum(s),vehicle(m,w){
26         }
27     private:
28         int seatnum;
29 };
30 class motorcycle:public bicycle,public motorcar{
31     public:
32         motorcycle(int m,int w,int h,int s):bicycle(m,w,h),motorcar(m,w,s),vehicle(m,w){
33         }
34 };
35 
36 
37 
38 #include "vehicle.h"
39 #include<iostream>
40 using namespace std;
41 int main(){
42     motorcycle m(1,1,1,1);
43     m.run();
44     m.stop();
45 } 

 

 

 







1
#include<iostream> 2 using namespace std; 3 class T{ 4 public: 5 void init(int x,int y){ 6 this->x=x; 7 this->y=y; 8 } 9 int plus(){ 10 return x+y; 11 } 12 protected: 13 int x,y; 14 }; 15 class A:public T{ 16 public: 17 void Ainit(int n,int m){ 18 init(n,m); 19 this->n=n; 20 this->m=m; 21 } 22 int min(){ 23 return n-m; 24 } 25 private: 26 int n,m; 27 }; 28 class B:public T{ 29 public: 30 void Binit(int n,int m){ 31 init(n,m); 32 this->n=n; 33 this->m=m; 34 } 35 int chen(){ 36 return n*m; 37 } 38 private: 39 int n,m; 40 }; 41 class C:public T{ 42 public: 43 44 void Cinit(int n,int m){ 45 init(n,m); 46 this->n=n; 47 this->m=m; 48 } 49 int chu(){ 50 return n/m; 51 } 52 private: 53 int n,m; 54 }; 55 int main(){ 56 A a; 57 a.Ainit(1,2); 58 cout<<a.plus()<<" "<<a.min()<<endl; 59 B b; 60 b.Binit(3,4); 61 cout<<b.plus()<<" "<<b.chen()<<endl; 62 C c; 63 c.Cinit(12,6); 64 cout<<c.plus()<<" "<<c.chu()<<endl; 65 }

 

 

 1 #include <iostream>
 2 using namespace std;
 3 class Franction{
 4     public:
 5         Franction(int t,int b);
 6         void draw();
 7         int gettop(){
 8             return top;
 9         }
10         int getbottom(){
11             return bottom;
12         }
13     private:
14         int top;
15         int bottom;
16 };
17 Franction::Franction(int t,int b):top(t),bottom(b){
18 }
19 void Franction::draw(){
20     int t1,t2,n,b1;
21     if(bottom<0){
22         bottom=-bottom;
23         top=-top;
24     }
25     if(top==0){
26         cout<<0<<endl;
27         return;
28     }
29     else if(top<0)
30         t1=-top;
31     else
32         t1=top;
33     b1=bottom;
34     t2=t1;
35     while(t1%b1>0){
36         n=t1%b1;
37         t1=b1;
38         b1=n;
39     }
40     if(t2%bottom==0)
41         cout<<top/bottom<<endl;
42     else
43         cout<<top/b1<<"/"<<bottom/b1<<endl;
44 }
45 Franction operator+(Franction& f,Franction& g){
46     int m,n;
47     m=f.gettop()*g.getbottom()+g.gettop()*f.getbottom();
48     n=f.getbottom()*g.getbottom();
49     Franction h(m,n);
50     return h;
51 }
52 Franction operator-(Franction& f,Franction& g){
53     int m,n;
54     m=f.gettop()*g.getbottom()-g.gettop()*f.getbottom();
55     n=f.getbottom()*g.getbottom();
56     Franction h(m,n);
57     return h;
58 }
59 Franction operator*(Franction& f,Franction& g){
60     int m,n;
61     m=f.gettop()*g.gettop();
62     n=f.getbottom()*g.getbottom();
63     Franction h(m,n);
64     return h;
65 }
66 Franction operator/(Franction& f,Franction& g){
67     int m,n;
68     m=f.gettop()*g.getbottom();
69     n=f.getbottom()*g.gettop();
70     Franction h(m,n);
71     return h;
72 }
73 int main() {
74     int n,m;
75     Franction b(1,2);
76     cout<<"b=";
77     b.draw();
78     Franction c(1,4);
79     cout<<"c=";
80     c.draw();
81     Franction a=b+c;
82     cout<<"b+c=";
83     a.draw();
84     Franction d=b-c;
85     cout<<"b-c=";
86     d.draw();
87     Franction e=b*c;
88     cout<<"b*c=";
89     e.draw();
90     Franction f=b/c;
91     cout<<"b/c=";
92     f.draw();
93     return 0;
94 }

 

posted @ 2018-06-06 16:34  YYWZS  阅读(124)  评论(0编辑  收藏  举报