Chinaunix首页 | 论坛 | 博客
  • 博客访问: 354700
  • 博文数量: 79
  • 博客积分: 1270
  • 博客等级: 中尉
  • 技术积分: 1370
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-12 08:48
个人简介

freedom~~~~~~~~~~

文章分类

全部博文(79)

文章存档

2014年(10)

2013年(2)

2012年(13)

2011年(54)

分类: C/C++

2011-09-01 17:23:29

模板函数:
  1. #include <stdio.h>
  2. #include <iostream>

  3. using namespace std;

  4. template <class T>
  5. T min(T &x,T &y)
  6. {
  7.         return ((x<y)? x:y);
  8. }

  9. int main()
  10. {
  11.         int n1=2,n2=10;
  12.         double d1=1.5,d2=5.6;
  13.         cout<<"int min is"<<min(n1,n2)<<endl;
  14.         cout<<"double min is"<<min(d1,d2)<<endl;
  15. }
模板类:

  1. #include <stdio.h>

  2. template <class T> class Test
  3. {
  4. public:
  5.         T a;
  6. public:
  7.         Test(T temp)
  8.         {
  9.                 a=temp;
  10.         }

  11.         T get(void)//在内部
  12.         {
  13.                 return a;
  14.         }

  15.         void set(T temp);
  16.         
  17.         T set2(T temp);
  18. };

  19. template <class T>
  20. void Test<T>::set(T temp)
  21. {
  22.         a=temp;
  23. }

  24. template <class T>
  25. T Test<T>::set2(T temp)
  26. {
  27.         a=temp;
  28.         return a;
  29. }

  30. int main()
  31. {
  32.         Test<int>b(4);
  33.         printf("the a is %d\n",b.get());
  34.         printf("the a is %d\n",b.set2(110));

  35.         Test<double>c(1.2);
  36.         printf("the c is %lf\n",c.get());
  37.         c.set(3.4);
  38.         printf("the c is %lf\n",c.get());

  39. }
操作符重载:
  1. #include <stdio.h>
  2. #include <iostream>

  3. class test
  4. {
  5. public:
  6.         void operator+(int a)
  7.         {
  8.                 printf("hello world operate +");
  9.         }
  10.         void operator-(int b);
  11. };

  12. void test::operator-(int b)
  13. {
  14.         printf("operator - is %d",b);
  15. }

  16. int main()
  17. {

  18.     test mytest;
  19.         mytest-111;
  20. }
操作符重载2:
  1. #include <iostream>
  2. #include <stdio.h>

  3. using namespace std;

  4. class test
  5. {
  6. public:
  7.     test()
  8.     {
  9.         a=3;
  10.     }
  11.     int operator=(const test& mytest)//两个参数第一个隐含了this指针,返回值可以是任何类型。
  12.     {
  13.         this->a=5;
  14.         return 110;
  15.     }
  16.     int operator++(int a) //后增量
  17.     {
  18.         printf("opetator ++!!!!");
  19.         this->a=100;
  20.         return 1;
  21.     }
  22. public:
  23.     int a;
  24. };

  25. int main()
  26. {
  27.     test ma;
  28.     test mb;
  29.     test t;
  30.     cout<<ma.a<<" "<<mb.a<<endl;
  31.     mb=ma;

  32.     cout<<ma.a<<" "<<mb.a<<endl;
  33.     printf("the return is %d\n",(mb=ma));
  34.     ma++;
  35.     printf("the return test ++ is %d\n",(ma++));
  36. }
友元:
  1. #include <stdio.h>

  2. class test
  3. {
  4. public:
  5.     test()
  6.     {
  7.         a=11;
  8.     }
  9.     friend void show(test &mytest);//一般都要把引用传进来,不然没有意义。
  10.     friend class ttt; //友元声明只能出现再内部。
  11.     void get()
  12.     {
  13.         printf("a is %d\n",a);
  14.     }
  15. private:
  16.     int a;
  17. };

  18. class ttt //可以使用test类的private 成员 //但是对于test的派生类没有权限
  19. {
  20.     public:
  21.     int show(test &b)
  22.     {
  23.         printf("^^this is fridend class a is %d\n",b.a);
  24.     }
  25.     static void gg(test &b)
  26.     {
  27.         printf("^^this is fridend class a is %d\n",b.a);
  28.     
  29.     }
  30. };

  31. class newttt:public ttt
  32. {
  33.     /*
  34.     int show2(test &b) //重新定义的方法无法在有private 访问权限 //原来的show方法任何有
  35.     {
  36.         printf("show2 is fridend class a is %d\n",b.a);
  37.     }
  38.     */
  39. };


  40. void show(test &mytest) //这里注意定义,不是成员
  41. {
  42.     mytest.a=111;
  43.     printf("this is friend func\n");
  44. }

  45. int main()
  46. {
  47.     test b;
  48.     show(b); //直接使用
  49.     b.get();
  50.     ttt t;
  51.     t.show(b);
  52.     ttt::gg(b);
  53.     newttt nt;
  54.     nt.show(b);
  55. //    nt.show2(b);
  56. }
拷贝构造函数:
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. class Test
  4. {
  5. public:
  6.         int data;
  7. public:
  8.        Test(int temp)//直接构造函数
  9.        {
  10.                 printf("this direct Test\n");
  11.                 data=temp;
  12.        }

  13.        Test(){}

  14.        Test(Test &temp)//拷贝构造函数
  15.        {
  16.                 printf("this is copy! Test\n");
  17.                 data=temp.data;
  18.        }

  19.        void hello(Test temp)//再传递参数到过程中,将调用到拷贝构造函数,再函数完成后就析构了,是临时对象
  20.        {
  21.                printf("hello ooooooooooooooooo\n");
  22.        }

  23.        ~Test()
  24.        {
  25.                 printf("~~~~~~~\n");
  26.        }
  27. };

  28. int main()
  29. {
  30.         Test a(111);//这样再main函数结束后,将调到析构函数
  31.         Test b=a;//将调到拷贝构站函数,如果再拷贝构造函数中,没有些data到值,将会是原来内存里的值
  32.         //b.hello(a);
  33.         printf("%d\n",b.data);
  34.         
  35.         printf("start f\n");
  36.         Test f;
  37.         f=a;//这里不会调到拷贝构造函数
  38.         printf("f------%d\n",f.data);
  39.         
  40.         Test *c=new Test(111);//用指针指向到对象,如果不delete指针,将不会调到析构函数
  41.         Test *d=c;
  42.         
  43.         delete(c);
  44.         printf("%d\n",d->data);
  45. }

深度拷贝:

  1. /*
  2.  *在某些状况下,类内成员变量需要动态开辟堆内存,如果实行位拷贝,也就是把对象里的值完全复制给另一个对象,如A=B。这时,如果B中有一个成员变量指针已经申请了内存,那A中的那个成员变量也指向同一块内存。这就出现了问题:当B把内存释放了(如:析构),这时A内的指针就是野指针了,出现运行错误。

  3.    深拷贝和浅拷贝可以简单理解为:如果一个类拥有资源,当这个类的对象发生复制过程的时候,资源重新分配,这个过程就是深拷贝,反之,没有重新分配资源,就是浅拷贝
  4.  * */

  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>

  8. class Test
  9. {
  10. public:
  11.         char *name;
  12.         int data;
  13.         int length;
  14. public:
  15.         Test(){}
  16.         Test(const char *myname,int mydata,int mylength)
  17.         {
  18.                 length=mylength;
  19.                 data=mydata;
  20.                 name=(char *)malloc(10);
  21.                 memset(name,0,10);
  22.                 memcpy(name,myname,mylength);
  23.         }
  24.         Test(const Test &temp)
  25.         {
  26.                 data=temp.data;
  27.                 length=temp.length;
  28.                 name=new char[length];//重新申请空间
  29.                 strcpy(name,temp.name);
  30.         }
  31. };

  32. int main()
  33. {
  34.         Test a("hello",10,3);
  35.         printf("----------------------%d\n",a.data);
  36.         Test b=a;

  37.         printf("%s\n",b.name);
  38. }

  39. 模板类:

阅读(893) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~