从C到C++入门

//namespace 命名空间

命名空间是为了防止多个头文件中有着一样命名的变量或函数,用命名空间来分别是哪一个空间中的变量和函数,防止命名冲突,例如:std::cout 命名空间std中的cout函数

:: 域作用限定符 前面为空则是全局找
using  namespace std;//命名空间展开 默认库std 有cout cin endl等
using  std::cout;//部分展开
using  std::endl;//只展开cout和endl  其他还是 std::cin  cin输入可以自动识别类型
#include <iostream>
using  namespace std;//全局展开 默认库 防止命名冲突

int test1()
{
    using  std::cout;//部分展开
    using  std::endl;//只展开cout和endl  其他还是 std::cin

    using  namespace lty;//局部展开
    struct AQueue::Node node1;
    struct BList::Node node2;
    cout << "1234" <<endl;//不展开 == std::cout   endl==\n
    
    int i=0;
    std::cin >> i; //cin自动识别数据类型 
    cout << "i=" << i << endl;
    return 0;
}

void Func(int a=10,int b=20,int c=30)//缺省参数

当使用函数时没有给实参,则使用函数给的参数,如果有实参则用实参

void Func(int a=10,int b=20,int c=30)//全缺省
void Func2(int a,int b=20,int c=30)//半缺省

Func();//缺省参数
Func(1,2,3);//只能连续使用 不能跳跃使用
void Func(int a=10,int b=20,int c=30)//全缺省
{
    cout << a <<endl;
    cout << b <<endl;
    cout << c <<endl;
}
void Func2(int a,int b=20,int c=30)//半缺省
{
    cout << a <<endl;
    cout << b <<endl;
    cout << c <<endl;
}

int test2()
{
    Func();//缺省参数
    Func(1,2,3);//只能连续使用 不能跳跃使用
    Func(1,2);
    Func(1);
    Func2(100);
    return 0;
}

//函数重载

从输入实参的类型来识别是int类型的add(),还是double类型的add()

int add(int a,int b)
double add(double a,double b)
//参数个数不同、参数类型顺序不同
int add(int a,int b)
{
    return a+b;
}
double add(double a,double b)
{
    return a+b;
}
int test3()//函数重载
{
    //参数个数不同、参数类型顺序不同
    cout << add(1,2) << endl;
    cout << add(1.1,2.2) << endl;
    return 0;
}

//引用

引用是特殊的重命名,它不占空间,也类似于地址,引用改变,被引用的变量也会变

void Swap(int& a,int& b)//形参是实参的引用名 地址一样 所以可以交换值
int*& pt=p;//指针引用
Swap(a,x);  //函数引用
void Swap(int& a,int& b)//形参是实参的引用名 地址一样 所以可以交换值
{
    int t=a;a=b;b=t;
}
int test4()//引用
{
    int a=10;
    int& b=a;
    // cout << a << endl << b << endl;
    // cout << &a << endl << &b << endl;//引用==重命名 地址一样
    int x=20;
    int* p=&x;
    int*& pt=p;
    cout << *p << endl << *pt << endl;//指针引用
    cout << p << endl << pt << endl;//地址

    Swap(a,x);  //函数引用
    cout << a << endl << x << endl;
    return 0;
}

//引用返回    

好处:减少拷贝,可以修改返回对象

int& PostAt(AY& ay,int i){return ay.a[i];}//PostAt函数相当于重命名为 ay.a[i]
PostAt(ay,i)=i;//可修改返回对象 给返回对象赋值
#define N 10
typedef struct Array
{
    int a[N];
    int size;    
}AY;

int& PostAt(AY& ay,int i)//PostAt 函数相当于重命名为 ay.a[i]
{
    assert(i<N);
    return ay.a[i];
}
int test5()//引用返回   1、减少拷贝 2、可以修改返回对象
{
    AY ay;
    for (int i=0;i<N;i++)
    {
        PostAt(ay,i)=i;//可修改返回对象
    }
    for (int i=0;i<N;i++)
    {
        cout << PostAt(ay ,i) << " ";
    }
    cout << endl;

    return 0;
}

//auto自动识别类型

可以识别赋值变量的类型,从而把新变量自动识别成赋值变量的类型

auto b=a;//自动识别类型
auto c=&a;//可以自动识别指针
auto* d=&a;//加*号 限定d是指针
auto& i=a;//引用必须加&才能自动识别
特殊的for循环:
for(auto& i:array){}//用i引用遍历array中所有数
//函数不能用 因为array传过去后是指针 不是数组
int test6()//auto自动识别类型
{
    int array[]={8,2,3,4,5,6,7};
    int a=0;
    auto b=a;//自动识别类型
    auto c=&a;//可以自动识别指针
    auto* d=&a;//限定d是指针
    cout <<typeid(b).name() << endl;//类型名输出
    cout <<typeid(c).name() << endl;
    for(auto& i:array)//函数不能用 因为array传过去后是指针 不是数组
    {
        i*=2;//取别名后可以改变数组值
        cout << i << " ";
    }
    cout << endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值