zoukankan      html  css  js  c++  java
  • C++ 学习总结 复习篇

     

    1. 友元的使用

      分为友元类和友元函数

          //友元类与友元函数的共同点:都可以让某一个类作为另一个类或者函数的参数。

          

          //友元类:它让当前类成为另一个类的友元,然后,另一个类可以访问当前类的私有成员。

          #include "stdafx.h"

          #include <iostream>

          using namespace std;

          

          class myclass

          {

           int m;

           int n;

          public: //若不加public,则编译错误。友元类无法访问私有成员。

           myclass(int i, int j)

           {

           m = i;

           n = j;

           }

           friend class test; //友元类的使用

           //简单点讲就是外部函数的内嵌 可以访问

           friend int sub(myclass k); //友元函数的使用

          };

          

          class test

          {

          public:

           void Test(myclass k)

           {

           cout << k.m << " " << k.n << endl;

           }

          };

          

          int sub(myclass k)

          {

           return k.m;

          }

          

          int _tmain(int argc, _TCHAR* argv[])

          {

           myclass *p = new myclass(3,6);

      test *t = new test();

           t->Test(*p);

           cout << sub(*p) <<endl;

           return 0;

          }

    2. 突然想到static 、 const 、 static const 以及它们的初始化

      Const 定义的常量超出其作用域会被释放掉,而static 定义的静态常量在函数直线后不会释放其存储空间。

      对于各自的初始化规则如下:

      1. const定义的常量,需要在初始化列表中初始化。
      2. static定义的静态变量,需要在类的外部初始化。
      3. const static 与 static const 一样,它也是需要在类的外部初始化。
      4. const 形式的方法 其主要作用是为了防止成员函数修改成员变量的值。作用域为某个对象,不同的对象可以有不同的const定义的常量的值。
      5. static形式的方法 其主要作用是为了作为全局方法使用。作用域为整个类。一般作为工具类使用,不同的对象可以修改static静态变量的值,

        而无法修改const static 静态常量的值。

       

      注意:要想在类中建立恒定不变的值,除了用const static外,还可以用enum 枚举实现。

       

      举例如下:

      // BlankTest.cpp : 定义控制台应用程序的入口点。

      //

      // const 、 static 、 const static 、 enum 之间的区别与联系

      #include "stdafx.h"

      #include <iostream>

      using namespace std;

       

      class myclass

      {

          const int m;

      public:    static int n;

          const static int mn;

          enum

          {

              size1 = 50, //枚举变量中没有 ;冒号,只有 , 逗号。

              size2 = 60

          };

      public: //若不加public,则编译错误。友元类无法访问私有成员。

          static void print();

          const void print2();

       

      myclass(int a);

      };

       

      int myclass::n = 10; //静态成员的定义+初始化

      const int myclass::mn = 20;

       

      myclass::myclass(int a):m(a) //用a 来初始化const成员,此处可以直接写一个 数字,比如10,都是没有问题的。

      {

          n += 1;//说明 我们在构造函数里面可以对static变量进行更改

      }

       

       

      void myclass::print()

      {

          cout << "count= " << mn << endl;

      }

       

      const void myclass::print2()

      {

          //m = 20; //错误,错误提示:表达式必须是可修改的左值。

          cout << "const = " << m << endl;

          cout << "static = " << n << endl;

      }

      int _tmain(int argc, _TCHAR* argv[])

      {

          myclass a(10);

          a.print();//通过对象访问静态成员函数

          myclass::print();//通过类访问静态成员函数

       

          a.print2();

          a.n += 1; //static 变量是可以更改的。而static const 变量是不可更改的。

          a.print2();

       

          cout <<"enum = "<< a.size1 << endl;

          //a.mn += 1;//error,静态常量无法修改左值。

          return 0;

      }

       

       

    3. 继承与派生

      继承与派生其实是一个意思的两种表达方式而已。

      C++允许多继承,记住构造函数与析构函数的用法。

      派生类与基类构造函数与析构函数的执行顺序:一般先执行基类的构造函数,然后执行派生类中对象成员的构造函数,最后执行派生类自身的构造函数。

      析构顺序与构造函数的执行顺序相反。

      记得 类的访问属性的变化 class a : public/protected/private b, xxx c{ };

      这个当中,属性的变化会影响 派生类当中的成员变量自身的属性变化。 有着同类合并的原则。

      下面是举例:

      // BlankTest.cpp : 定义控制台应用程序的入口点。

      //

      #include "stdafx.h"

      #include <iostream>

      using namespace std;

       

      class myclass

      {

      public:

      int m;

      int n;

      myclass(int i, int j):m(i),n(j){}

       

      void set_m(int m2)

      {

           m = m2;

      }

       

      const int get_m()

      {

           return m;

      }

       

      void set_n(int m1)

      {

           n = m1;

      }

      const int get_n()

      {

           return n;

      }

      };

       

      class myclass2: public myclass

      {

      protected:

          int k;

      public:

          myclass2(int i ,int j, int p):myclass(i,j){k = p;}

       

          void set_k(int m3)

          {

              k = m3;

          }

          const int get_k()

          {

              return k;

          }

      };

      int _tmain(int argc, _TCHAR* argv[])

      {

          myclass2 l(3,4,5);

          cout << l.get_m() << " " << l.get_n() << " " << l.get_k() << endl;

          return 0;

      }

       

    4. 函数

      函数就和变量一样,先定义后使用。

      而且和传统意义上的函数有异曲同工之妙。

      举例:

      // BlankTest.cpp : 定义控制台应用程序的入口点。

      //函数的参数的两种传递方式:指针传递, 值传递,现在又有引用传递。

      //对于指针与引用的区别:

      //当对象为空,必须用指针,当对象在使用过程中会改变时,只能用指针。因为:没有空引用,且引用是一个常量,不能对其重新赋值。

      #include "stdafx.h"

      #include <iostream>

      using namespace std;

       

      class myclass

      {

      public:

      int *p;

      myclass(int num)

      {

           p = new int[num];

           for (int i = 0; i < num; ++i)

           {

               cin >> p[i];

           }

      }

      void acc(int num) //选择排序

      {

           for (int i = 0; i < num; ++i)

           {

               for (int j = i + 1; j < num; ++j)

               {

                   if (p[i] < p[j])

                   {

                       int temp = p[i];

                       p[i] = p[j];

                       p[j] = temp;

                   }

               }

           }

      }

      void display(int num)

      {

           for (int i = 0; i < num; ++i)

           {

               cout << p[i] << " " << endl;

           }

      }

       

      ~myclass()

      {

           delete[] p;

      }

      };

       

       

      int _tmain(int argc, _TCHAR* argv[])

      {

          myclass cc(10);

          cc.acc(10);

          cc.display(10);

          return 0;

      }

       

      函数重载:返回值与名字相同,而函数特征不同的两个或多个函数,带有const属性的不算是重载。

      举例:

      // BlankTest.cpp : 定义控制台应用程序的入口点。

      //函数的重载 不是 重复

      #include "stdafx.h"

      #include <iostream>

      using namespace std;

       

      static const double PI = 3.1415926;

      double ZC(double radius)

      {

          return 2*PI*radius;

      }

       

      double ZC(double width, double height)

      {

          return 2*(width+height);

      }

       

      int _tmain(int argc, _TCHAR* argv[])

      {

          double r = 3;

          double w = 1;

          double h = 2;

       

          cout << "r zc = " << ZC(r) << endl;

          cout << "rect zc = " << ZC(w,h) << endl;

          return 0;

      }

       

      递归的使用:

      一定要有终止条件,要有递推关系式。

      举例:

      // BlankTest.cpp : 定义控制台应用程序的入口点。

      //函数的重载 不是 重复

       

      #include "stdafx.h"

      #include <iostream>

      using namespace std;

       

      int find(int n, int m);

      int _tmain(int argc, _TCHAR* argv[])

      {

          int m = find(0,1);

          cout << m << endl;

          return 0;

      }

      //普通实现

      //int find(int n, int m)

      //{

      //    while(1)

      //    {

      //        if (n > 200)

      //        {

      //            return m-1;

      //        }

      //        else

      //        {

      //            n += m*m;

      //            m++;

      //        }

      //    }

      //}

      //递归实现

      int find(int n, int m)//有一个返回值即可

      {

          n = n + m*m; //递推关系式 fn = fn-1 + m*m;

          if (n < 200)

          {

              m++;

              find(n,m);//相当于循环

          }

          else

          {

      return m;

          }

      }

       

       

      //递归实现

      int find(double a, int b)//有一个返回值是 fn-1

      {

          if (b == 0)

          {

              return 1;

          }

          else

          {

      return a*find(a,b-1); // 递推式 fn = fn-1 * a;

          }

      }

       

    5. 结构体

      涉及到的概念有 结构体、联合体、位域等。

      举例:

      // BlankTest.cpp : 定义控制台应用程序的入口点。

      //map 的用法http://blog.csdn.net/sunshinewave/article/details/8067862

       

      #include "stdafx.h"

      #include <iostream>

      using namespace std;

      typedef struct test

      {

      int a:3;

      int b:4;

      int c:5;

      }test;

      typedef struct Date

      {

           int month;

           int day;

           int hour;

      } Date;

       

      Date oneday()

      {

           Date ad;

           cin >> ad.day >> ad.hour >> ad.month;

           return ad;

      }

       

      void show(Date &oneday) //此处改为 指针也可以。

      {

           cout << oneday.day << " " << oneday.hour << " " << oneday.month << endl;

      }

      int _tmain(int argc, _TCHAR* argv[])

      {

          Date one = oneday();

          show(one);

          return 0;

      }

       

  • 相关阅读:
    第十五周总结
    第二阶段冲刺
    课堂练习
    第十四周总结
    WP8 NavigationInTransition实现页面切换效果
    WP8学习笔记:如何在页面显示前自动转向到其他页面
    WP8简单的计算器
    WP8滑动条(Slider)控件的使用
    Wp8滚动区域(ScrollViewer)控件的使用
    C#中ToString格式大全
  • 原文地址:https://www.cnblogs.com/zhuxuekui/p/4133314.html
Copyright © 2011-2022 走看看