c++ 面向对象

 

1)处理复数的例子

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 class Complex{    
 4     private:
 5         int x,y;
 6     
 7     public:
 8         Complex(int a=0,int b=0) //构造函数 
 9         {
10             x=a;
11             y=b;
12         }
13         
14         void SetX(int a){x=a;}     //功能函数    
15         void SetY(int a){y=a;}     //功能函数    
16         int GetX(){return x;}    //功能函数
17         int GetY(){return y;}   //功能函数
18         void Print(){cout<<x<<"+"<<y<<"i"<<endl;}  //功能函数        
19         
20         Complex operator + (Complex r)  //运算符号重载入 
21             {return Complex(x+r.x,y+r.y);}
22 
23         ~Complex(){    }      //析构函数,撤销实例。 
24 };
25 
26 
27 int main()
28 {  
29     Complex x1(6,8),x2(1,2);//实例化两个对象x1,x2,相当于定义两个变量 
30     
31     cout<<x1.GetX()<<"+"<<x1.GetY()<<"i"<<endl;//调用实例对象中的功能解决问题 
32     cout<<x2.GetX()<<"+"<<x2.GetY()<<"i"<<endl;
33     
34     x1.Print();
35     x2.Print();
36     
37     Complex x3=x1+x2;
38     x3.Print();
39     
40     return 0;
41 }//x1,x2,x3 离开作用域时,撤销实例,节省内存。 
复制代码

 

2)使用自定义的map和queue处理双向宽搜的例子

复制代码
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxl = 2e5+10;
  4 
  5 class Map{
  6     
  7     struct item{
  8         string key;int value;
  9     }d[maxl];
 10     
 11     int cnt;
 12     
 13     public:        
 14         int find(string x){
 15             for(int i=0;i<cnt;i++) if(d[i].key==x)
 16               return d[i].value;
 17             return -1;
 18         }
 19         
 20         static int end(){return -1;}
 21         
 22         void insert (string k,int v){
 23             d[cnt].key=k;d[cnt++].value=v;
 24         }
 25     
 26 }s[2];//定义对象数组,对象类型是MAP。作者为什么把步数记MAP中,而不是队列中? 
 27 
 28 class Queue{
 29     
 30     string q[maxl];
 31     int head,tail;
 32     
 33     public:
 34         void pop(){++ head;}
 35         string front(){return q[head+1];}
 36         bool empty(){return head==tail;}
 37         void push(string x){q[++tail]=x;}
 38 }q[2];//定义对象数组,对象类型是Queue。
 39 
 40 
 41 string st0,st1;
 42 int m;
 43 
 44 string LtoR(string s,int L,int R){
 45     string t=s;
 46     char tmp=t[L];
 47     for(int i=L;i<R;++i) t[i]=t[i+1];
 48     t[R]=tmp;
 49     return t;
 50 }
 51 
 52 string RtoL(string s,int L,int R){
 53     string t=s;
 54     char tmp=t[R];
 55     for(int i=R;i>L;--i) t[i]=t[i-1];
 56     t[L]=tmp;
 57     return t;
 58 }
 59 
 60 bool check(string st,int p,int step){
 61     
 62     if(s[p].find(st)!=s[p].end()) return false;//产生的状态st在s[p]中已经存在,说明状态出现过了,不是新状态。  
 63     
 64     
 65     //程序会运行到这里时,说明产生的状态st在s[p]中不存在 
 66     ++step;
 67     
 68     if(s[p^1].find(st)==s[p].end())  //产生的状态st在对面s[p^1]中也不存在,说明是个新状态 
 69     {
 70         s[p].insert(st,step);//加入map 用于判断重复 ,顺便记录步数 
 71         q[p].push(st); // 加入队列 
 72         return false;  
 73     }
 74     
 75     
 76     //程序运行到这里时,说明产生的状态st在对面的map(s[p^1])中存在,说明双向bfs相遇了 
 77     cout<<s[p^1].find(st)+step<<endl;
 78     
 79     return true;//返回已经相遇了 
 80     
 81 }
 82 
 83 
 84 int main()
 85 {
 86     cin>>st0>>st1;
 87     
 88     int len=st0.length();
 89     if(len!=st1.length()) {    cout<<-1<<endl;    return 0;}
 90     if(st0==st1) { cout<<0<<endl; return 0;    }
 91     
 92     cin>>m;
 93     s[0].insert(st0,0);s[1].insert(st1,0);
 94     q[0].push(st0);q[1].push(st1);
 95     
 96     for(int p=0;!(q[0].empty() && q[1].empty());p^=1)//交替使用两个队列进行宽搜 
 97     {
 98         string st=q[p].front();q[p].pop();
 99         int step=s[p].find(st);
100         if(    
101             // 如果某个正向操作的结果与对面的状态相遇    正向操作:LtoR[m,len-1],RtoL[0,m]
102             (p==0 && (  check(LtoR(st,m,len-1),p,step) || check(RtoL(st,0,m),p,step))      )  
103               || 
104             //或者如果某个反向操作的结果与对面的状态相遇  反向操作:LtoR[0,m],RtoL[m,len-1] 
105             (p==1 && (  check(LtoR(st,0,m),p,step)     || check(RtoL(st,m,len-1),p,step))  ) 
106           )
107             return 0;
108             
109     }    
110     cout<<-1<<endl;
111     return 0;
112 
113 }
复制代码

 3)模板

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 
 5 template <typename T> //告诉编译器,类型T暂时不定,在调用时再定。
 6 
 7 //定义一个函数模板 求三个数的最大值,这个三个数的类型为暂时不确定类型T 
 8 T MaxV(T v1,T v2,T v3){
 9     T v=v1;
10     if (v<v2) v=v2;
11     if (v<v3) v=v3;
12     return v;
13 }
14 
15 
16 int main()
17 {
18     int a=1,b=2,c=3;
19     cout<<MaxV(a,b,c)<<endl;//输出3个整型数的最大值
20 
21     double x=1.1452,y=3.24542,z=0.64643;
22     cout<<MaxV(x,y,z)<<endl;//输出3个浮点数的最大值     
23     
24     string s1="hello",s2="ayaya",s3="byebye";
25     cout<<MaxV(s1,s2,s3)<<endl;//输出3个字符串的最大值     
26     
27     
28     
29     return 0;
30 
31 }
复制代码

 4)对象实例化的几种方法

复制代码
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct P0 { int r, c;};
 5 
 6 struct P1 {
 7     int r, c;    
 8     P1(int r, int c): r(r), c(c) { } //使用初始化列表给成员赋值。也是一种实例化对象的方法。 
 9     
10     public:
11         Print(){cout<<r<<" "<<c<<endl;}
12 };
13 
14 struct P2 {
15     int r, c; 
16     P2(int r, int c){this->r=r;  this->c=c;}
17         
18     public:
19         Print(){cout<<r<<" "<<c<<endl;}
20 };
21 
22 
23 int main() {
24     
25     P0 Pos01;             //定义,也叫实例化 
26     Pos01.r=1,Pos01.c=2;  //给对象成员赋值
27     cout<<Pos01.r<<" "<<Pos01.c<<endl; 
28     
29     P0 Pos02={3,4};     //定义并初始化
30     cout<<Pos02.r<<" "<<Pos02.c<<endl;  
31     
32     P1 Pos11={5, 6};        //定义并初始化
33     Pos11.Print();
34     
35     P1 Pos12(7, 8);            //定义并初始化
36     Pos12.Print();
37     
38     P2 Pos21={9, 10};        //定义并初始化
39     Pos21.Print();
40     
41     P2 Pos22(11, 12);        //定义并初始化
42     Pos22.Print();
43       
44     return 0;
45 }
复制代码

 

 
 
 
 
 
 
 
 
 
posted @   flatten  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示