C++复习 第一部分c到c++的过度

/*
#include <iostream>

using namespace std;

int aaaa;//不赋值 直接输出它的值的时候,因为是全局变量 所以默认给了值 0,如果是局部变量 那么就是一个垃圾值
//在C中全局变量可以重复定义 后定义的覆盖前面的 但是 C++不行 检测机制更加严格

namespace A
{
    int a = 1;
    void print()
    {
        cout << "namespace a" << endl;
    }
}

namespace B
{
    int a = 2;
    void print()
    {
        cout << "namespace b" << endl;
    }
}

namespace C
{
    namespace D
    {
        struct test
        {
            int x;
        };
    }
}

void sin(void)
{

}
//此处第一个void 不可以省略!(C中可以) 
//第二个void 有没有都代表 不允许传参 在C中不写void 代表可以接受任意多个形参

int main()
{
    cout << "helloworld" << endl;//cout是输出流对象 << 是重载了的符号  endl是换行

    using namespace A;
    cout << "a = " << a << endl;

    cout << "a = " << B::a << endl;//声明了用B中的a变量

    using B::a;//优先级更高 但是仅仅声明 一个B里面的变量a其余的还是用A中的(下面的print函数); ::是作用域限定符
    cout << "a = " << a << endl;

    print();

    C::D::test t;//多重嵌套的调用

    for (int z = 0; z < 3; z++)//允许了在for中定义变量
    {
        cout << "beauty" << endl;
    }

    register int aa;
    cout << "a的地址是 " << &aa << endl;//这时候给的地址是在内存中随机分配的一个地址
    //因为本来寄存器变量是不存在地址的 
    //但是G++默认这种取地址行为是可以的 所以自动给你分配了一个内存地址

    int a1 = 1, b1 = 2;
    (a1 > b1) ? a1 : b1 = 10;
    //作为一个三目运算符 在C++中的运算结果 返回的是变量 b1
    //所以可以对其赋值
    cout << "b1 = " << b1 << endl;
    //但是在C中三目运算符 运算结果 返回的是 数值常量 2,所以不可以进行赋值


    cin.get();//等待一个输入再退出程序 也可以用system("pause"); 多+一个 #include <windows>
    return 0;
}
*/


/*
//有关const
#include <iostream>

using namespace std;

int main()
{
    const int a = 1;//这里的a = 1 存在符号表中(恒定不变的对应关系)
    int *p = (int *)&a;// 这里取地址a 再取值 是将 a对应的值:1 赋值给 p

//a本身沒有地址 强行取地址系统就给它分配了一个地址 让p指向这个地址 而已

  *p = 3;//再将p中的 值:1  改为3

    cout << "*p = " << *p << "\n a = " << a << endl;
    cin.get();
    return 0;
}
*/


//const 与 define 区别 :作用域不一样
//const是局部变量 仅在函数内有效 define 是宏 该行向下都有效
/*
#include <iostream>

using namespace std;

void print()
{
    const int a = 1;
    #define b 1
    cout << "a = " << a << endl;
    cout << "b = " << b << endl;
}


int main()
{
    print();
    //cout << " main a = " << a << endl;//这就会报错 因为 a是局部变量
    cout << " main b = " << b << endl;
    cin.get();
    return 0;
}
*/

/*
//int & a = b 代表着给b取一个别名 叫做a
//例子:
#include <iostream>

using namespace std;

void swap(int *a, int *b)
{
    int tem = *a;
    *a = *b;
    *b = tem;
}

void swap(int &x, int &y)//这里的& 在定义的过程中代表着取别名 给传过来的a/b区别名为x/y
{
    int tem = x;
    x = y;
    y = tem;
}
int main()
{
    int a = 1, b = 2;
    swap(&a, &b);
    cout << "     a = " << a << "      b = " << b << endl;

    swap(a, b);
    cout << "别名 a = " << a << " 别名 b = " << b << endl;

    cin.get();
    return 0;
}
*/

/*
//研究引用是否占用空间
#include <iostream>

using namespace std;

struct test
{
    int &a;
    int &b;
};

struct test1
{
    char &a;
    char &b;
};
//这里的int & 和char &都是常指针 所以都占四个字节大小
//和 int a[10] = {0}; 中的 a 所代表的一样  a 也是常指针
//a++ 是不允许的 因为 a 所代表的数组的首地址是固定的 不可以修改(a++ 等价于 a = a+1)

int main()
{
    int a = 1;
    char b = 'i';
    int &c = a;
    char &d = b;
    //这里的int& char& 都是引用 代表对应的 a ,b 的数据类型 所占空间大小

    cout << "sizeof(c) = " << sizeof(c) << endl;
    cout << "sizeof(d) = " << sizeof(d) << endl;
    cout << "sizeof(test) = " << sizeof(test) << endl;
    cout << "sizeof(test1) = " << sizeof(test1) << endl;

    cin.get();
    return 0;
}
*/

//指针的引用
/*
#include <iostream>
#include <cstring>
#pragma warning (disable:4996)//注意:因为vs2013版本级以后不支持strcpy这个函数 unsafe加这个去除警告(error
using namespace std;

void initMemory(char *&ptr)
{
    ptr = (char *)malloc(100);
}

int main()
{
    char *str = NULL;
    initMemory(str);
    strcpy(str,"hello");
    cout << "str = " << str << endl;
    
    cin.get();
    return 0;
}
*/

//常引用的初始化相关

/*
#include <iostream>

using namespace std;

int main()
{
    const int &a = 1;//可以用常量初始化常引用
    //int &a = 1;//不允许用常量初始化引用

    int *p = (int *)(&a);
    (*p)++;
    cout << "a = " << a << endl;


    cin.get();
    return 0;
}
*/

/*
#include <iostream>

using namespace std;

int add(int a,int b = 0,int c = 0)//默认参数 放在最后 没有提供数值的时候 默认使用默认参数的值
{
    return (a + b +c);
}
int main()
{
    int result = 0;
    result = add(1,2);
    cout << "add( 1 , 2) = " << result << endl;
    cin.get();
    return 0;
}

*/

/*
#include <iostream>

using namespace std;

int add(int a, int b = 0, int c = 0, int = 0,int  = 0)                
{
    return (a + b + c);
}
//默认参数 放在最后 没有提供数值的时候 默认使用默认参数的值
//最后两个是占位参数 + 默认参数 方便程序员 以后修改程序
//占位参数 就是最后 ,int ,int 留给后期修改程序时 能够添加参数 不过调用函数需要提供足量实参 
int main()
{
    int result = 0;
    result = add(1, 2);
    cout << "add( 1 , 2) = " << result << endl;
    cin.get();
    return 0;
}
*/
/*
#include <iostream>

using namespace std;

int add(int a, int b)
{
    return a + b;
}

float add(float a, float b)
{
    return a + b;
}

int main()
{
    int a = 1;
    int b = 2;
    float c = 1.1;
    float d = 2.0;

    cout << "add(a, b) = " << add(a, b) << endl;
    cout << "add(c ,d) = " << add(c, d) << endl;
    cin.get();
    return 0;
}
*/

//函数指针指向重载函数
#include <iostream>

using namespace std;

int add(int a, int b)
{
    return a + b;
}

float add(float a, float b)
{
    return a + b;
}

int main()
{
    int a = 1;
    int b = 2;
    float c = 1.1;
    float d = 2.0;

    cout << "add(a, b) = " << add(a, b) << endl;
    cout << "add(c ,d) = " << add(c, d) << endl;

    int(*p)(int, int);//函数指针指向函数add(int型的)
    p = add;
    p(2 , 3);

    float(*q)(float, float);//同上
    q = add;
    q(2.1, 3.1);

    cin.get();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38313246/article/details/81100074