【C++学习教程01】C++命名空间重名&函数原型&字符类型&数据类型

参考资料

  • 均来自范磊C++教程(1-4课时)
  • 开发平台Xcode

代码

学习内容十分的简单,因此直接用代码体现。

#include <iostream>
#include <iomanip>
#include <locale>
namespace a{
    int b=5;
}
namespace c {
    int b=8;
}
using namespace std;
using namespace c;
using namespace a;
//********
//函数又叫做【方法】,PLC编程中通常叫做【Method】
int show(int a, int b){
    cout<<"函数调用"<<endl;
    return a+b;
}
//*********
//规范的操作时先声明函数【函数原型】,在再最后面进行定义,这样可以防止重复调用
void A();
void B();
void swap(int,int);
void A(){
    cout<<"function A is used"<<endl;
    B();
}
void B(){
    cout<<"function B is used"<<endl;
}
void swap(int x, int y){
    int z;
    cout<<"before change"<<x<<","<<y<<endl;
    z=x;
    x=y;
    y=z;
    cout<<"after change"<<x<<","<<y<<endl;
}
//*********
//在任意函数之外的变量声明成为全局变量
int golbal_a=3;
int global_b=4;
int main(int argc, const char * argv[]) {
    int b=9;
    cout << b<<"
"<<endl;  //"
"和"endl"作用一样
    cout << c::b<<endl;     /*重名问题*/
    cout << a::b<<endl;
    //********
    char char_s='A';
    int int_s;
    int_s=char_s;
    cout << int_s <<endl;
    
    //********
    A();
    //********
    swap(golbal_a,global_b);
     cout<<"after change"<<golbal_a<<","<<global_b<<endl;
    //********
    unsigned char char_test;
    char_test=65; //整型自动转化为字符型
    cout<<"char is "<<char_test<<endl;
    cout<<"char is "<<(int)char_test<<endl;
    //********此段代码无效?
    //setlocale(LC_ALL,"chs");
    //wchar_t w[]=L"中";
    //wcout<<w;
    float f_a=1123456789;//虽然也是4个字节但是他的指数位有8位//对有效数字的理解
    cout<<f_a<<endl;
    cout<<setprecision(15)<<f_a<<endl;//有效数字位数为6~7位
    double f_b=1.234567891987654321;//8字节 指数位11位
    cout<<setprecision(15)<<f_b<<endl;//有效数字位数为15~16位
    //const double PI=3.1415926;
    //PI=3.14;//此时会发生报错
    enum num{zero, one=100, two, three, four};
    cout<<zero<<endl;//不指定的话就会自动设置为0
    cout<<one<<endl;
    cout<<two<<endl;//指定后自动加1
    enum day{sun,mon , tus, wed, thur,fir,sat};//增加程序可读性
    day today;
    today=tus;
    cout<<tus<<endl;
    //********
    int temp_a;
    double temp_b;
    cout << "input 2 int num"<<endl;
    cin>>temp_a;//中间用空格表示区分 //输入字符变量等时无效显示为0
    cin>>temp_b;//输入double时候,会退1法。
    int a=show(temp_a,temp_b);//数值采用退1法//但是不准确
    cout << temp_a <<endl;
    cout << temp_b <<endl;
    cout << a <<endl;
    return 0;
}


输出结果:
在这里插入图片描述

数据类型

参考见: 菜鸟论坛.在这里插入图片描述

原文地址:https://www.cnblogs.com/vrijheid/p/14222953.html