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;

    }

     

C++ 学习总结 复习篇的更多相关文章

  1. C++学习总结 复习篇2

      延续上一小节内容:下面继续讲解虚函数和多态 虚函数和多态 基类指针可以指向任何派生类的对象,但是不能调用派生类对象的成员. 但是,基类可以调用覆盖了虚函数的函数.(现在调用将来,这有问题,说明现在 ...

  2. NOIP复习篇

    NOIP复习篇---枚举 --------------------------------------------------------------------------------------- ...

  3. Java工程师学习指南 初级篇

    Java工程师学习指南 初级篇 最近有很多小伙伴来问我,Java小白如何入门,如何安排学习路线,每一步应该怎么走比较好.原本我以为之前的几篇文章已经可以解决大家的问题了,其实不然,因为我之前写的文章都 ...

  4. 数据库MySQL学习笔记高级篇

    数据库MySQL学习笔记高级篇 写在前面 学习链接:数据库 MySQL 视频教程全集 1. mysql的架构介绍 mysql简介 概述 高级Mysql 完整的mysql优化需要很深的功底,大公司甚至有 ...

  5. 一步步学习javascript基础篇(0):开篇索引

    索引: 一步步学习javascript基础篇(1):基本概念 一步步学习javascript基础篇(2):作用域和作用域链 一步步学习javascript基础篇(3):Object.Function等 ...

  6. 一步步学习javascript基础篇(3):Object、Function等引用类型

    我们在<一步步学习javascript基础篇(1):基本概念>中简单的介绍了五种基本数据类型Undefined.Null.Boolean.Number和String.今天我们主要介绍下复杂 ...

  7. Python3学习(3)-高级篇

    Python3学习(1)-基础篇 Python3学习(2)-中级篇 Python3学习(3)-高级篇 文件读写 源文件test.txt line1 line2 line3 读取文件内容 f = ope ...

  8. Python3学习(2)-中级篇

    Python3学习(1)-基础篇 Python3学习(2)-中级篇 Python3学习(3)-高级篇 切片:取数组.元组中的部分元素 L=['Jack','Mick','Leon','Jane','A ...

  9. Python3学习(1)-基础篇

    Python3学习(1)-基础篇 Python3学习(2)-中级篇 Python3学习(3)-高级篇 安装(MAC) 直接运行: brew install python3 输入:python3 --v ...

随机推荐

  1. mysql 与QT的连接

    第一步:安装 qt开发环境 bi@bi-desktop:~$sudo apt-get install qt4-dev-tools qt4-doc qt4-qtconfig qt4-demos qt4- ...

  2. LeetCode(119) Pascal's Triangle II

    题目 Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [ ...

  3. Cocos2D 添加 UIView

    cocos2d是使用继承于ccnode的结点类型的层.但是我想用一个opengl来绘制,就简单的情况来说必须得加一个uiview.现转载如下: 第一部分:: 使用Cocos2D开发游戏和应用程序的时候 ...

  4. centos中python2替换为python3,并解决yum出错

    这里采用安装python3.6版本. 安装python3.6可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel r ...

  5. EFCore CodeFirst模型迁移生成数据库备注(mysql)

    重写Mysql下sql脚本生成器 using Framework.NetCore.Extensions; using Framework.NetCore.Models; using Microsoft ...

  6. http协议工作原理(转)

     WWW是以Internet作为传输媒介的一个应用系统,WWW网上最基本的传输单位是Web网页.WWW的工作基于客户机/服务器计算模型,由Web 浏览器(客户机)和Web服务器(服务器)构成,两者之间 ...

  7. cf898d Alarm Clock

    区间上有 \(n\) 个点,问你为达到目的:长度为 \(m\) 的区间内点的个数都 $ < k$需要去掉多少个点. 贪心.每个区间我们总是去掉最后的,也就是说除非万不得已我们是不会去掉点的. 队 ...

  8. luogu2604 [ZJOI2010]网络扩容

    先做一遍普通的dinic 然后再更改源点为超级源,超级源向原源加一条capacity=k && cost=0的边,再加上有费用的边跑最小费用最大流 #include <iostr ...

  9. 关于面试总结-linux篇

    前言 现在做测试的出去面试,都会被问到linux,不会几个linux指令都不好意思说自己是做测试的了,本篇收集了几个被问的频率较高的linux面试题 常用指令 1.说出10个linux常用的指令 ls ...

  10. TOJ 2353: Billiard

    数学?计算几何?物理?这个还是很轻松的. 353: Billiard  Time Limit(Common/Java):1000MS/10000MS     Memory Limit:65536KBy ...