C++入门

C++基础——day01

1.hellow world.

#include<iostream> //头文件  iostream,标准输入输出流,其中io是输入输出
using namespace std; //使用命名空间 std;
int main() // 主函数 函数入地址口
{
    //cout 标准的输出,定义在iostream里面
    // <<左移运算符
    //endl为C++标准结束换行  \n \r\n 
    cout<<"hellow word"<<endl;
    system("pause"); //阻塞功能
    return EXIT_SUCCESS; //EXIT_SUCCESS 宏定义为0,程序正常结束;
}

2.双冒号作用域运算符

全局作用域符号:当全局变量在局部函数中与其中某个变量重名,那么就可以用::来区分

#include<iostream> //:: 全局作用域符号:当全局变量在局部函数中与其中某个变量重名,那么就可以用::来区分
using namespace std;

int atk=200;
void test01()
{
    int atk=100;
    cout << "攻击力为:"<< atk << endl; //输出结果为局部变量 atk=100

    cout << "攻击力为:"<< ::atk << endl; //由于使用了双冒号运算符,这里输出的是全局变量atk=200
}
int main()
{
    test01();
    system("pause");
    return EXIT_SUCCESS;
}

3.namespace命名空间

#include<iostream>
using namespace std;
// namespace命名空间主要用途用来解决命名冲突的问题
//1.命名空间下可以放函数、变量、结构体、类
//2.命名空间必须定义在全局作用域下
//3.命名空间可以嵌套命名空间
namespace KingGlory
{
    void goAtk()
    {
        cout<<"KingGlory攻击实现"<<endl;
    }
}
namespace LOL 
{
    void goAtk()  命名空间中的函数
    {
        cout<< "LOL攻击实现"<<endl;
    }
    int a_L=200;    //命名空间中的变量
    struct Zero //命名空间中的结构体
    {
        int HP=500;
        int MP=500;
    };
    class Animal{};  //命名空间中的类
    namespace YunDing
    {
        int a_L=100;
    }
    

}
//4.命名空间是开放的,可以随时往原先的命名空间添加内容
namespace LOL //此命名空间会和上面同名命名空间进行合并
{
    int b_L=666;
}
//5.无名、匿名命名空间
namespace
{
    int c_L=0;
    int d_L=0;
}
//当写了无名命名空间,相当于写了static int c_L;  static int d_L;
//只能在当前文件内使用
//6.命名空间可以起别名
namespace verylongname
{
    int e_L=10;
}
void test03()
{
    namespace S=verylongname;
    cout<<"verylongname命名空间中e_L的值为"<<S::e_L<<endl;
}
void test01()
{
    KingGlory::goAtk();
    LOL::goAtk();
}
void test02()
{
    cout << "作用域LOL中YunDing的a_L的值为:"<<LOL::YunDing::a_L<<endl;
    //输出命名空间中嵌套的空间中的变量值
    cout<<"命名空间LOL中的a_L值为:"<<LOL::a_L<<endl;
    cout<<"命名空间LOL中的b_L值为:"<<LOL::b_L<<endl;
}
int main()
{
    test01();
    test02();
    test03();
    system("pause");
    return 0;
} 

4.using作用

#include<iostream>
using namespace std;
namespace KingGlory
{
    int MonkeySunId=10;
}
void test01()
{
    int MonkeySunId=20;
    //using声明     注意避免二义性问题
    //写了using声明后,下面这行代码说明以后看到的MonkeySunId是用KingGlory下的
    //但是编译器又有就近原则,导致此段产生了二义性
    //using KingGlory::MonkeySunId;
    cout<<MonkeySunId<<endl;
}
//using 编译指令
namespace LOL
{
    int MonkeySunId=30;
}
void test02()
{
    //int MonkeySunId=20;
    //using 编译指令
    using namespace KingGlory;//打开王者荣耀房间
    using namespace LOL; //打开LOL房间
    //如果打开多个房间,也要避免二义性问题
    cout << LOL::MonkeySunId<<endl;  
}
int main()
{
    test01();
    test02();
    system("pause");
    return 0;
}

5.C++对C语言的增强

#include<stdio.h>
#include<stdlib.h>
//1、全局变量检测增强
int a;
int a=10; //在C语言中可以编译通过,但在C++中,同名变量多次定义无法编译通过
//2、函数检测加强,参数类型加强,返回值增强
int getRectS(w,h)  //在C++中 参数类型必须要声明且需要有返回值,否则无法通过编译
{

}
void test01()
{
    getRectS(10,10,10);  //该函数只有两个参数,在C++中会报错但在C中不会
}
//3、类型转换检测增强
void test02()
{
    char * p=malloc(sizeof(64)); //malloc函数返回值是void
}
//4、struct增强
struct Person
{
    int Age;
    //void pluAge(); //c语言中struct不可以加函数
};
void test03()
{
    struct Person p1; //C语言中必须要加struct,C++中不用
}
//5、bool类型增强 C语言中没有bool类型
//bool类型只有true(非0)或者false(0)大小为1

//6、三目运算符增强
void test04()
{
    int a=10;
    int b=20;
    printf("Res= ",a>b?a:b);
    //(a>b?a:b)=100;  //在c++中  这个语法可以编译通过且将100赋值给a,b中最大的值
    *(a>b?&a:&b)=100; //c语言中仿写上面语句
}
int main()
{
    //test01(); 
    test02();
    test03();
    test04();
    system("pause");
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值