C++ 简单的入门语法

 入门的hello world 

using namespace std; 是使用命名空间,有点像java里面的引入包
main 方法和java一样是主入口,有且只有一个,因为是int ,所以还必须返回一个整形
#include<iostream>
using namespace std;
int main()
{
cout << "hello world!"<< endl;
cin.get();//这个可以不要
return 0;
}

所以换成下面的方法也是可以的:

#include<iostream>
int main()
{
std::cout << "hello world12!"<< endl;
std::cin.get();//这个可以不要
return 0;
}

变量的使用:

#include<iostream>


int test(){
    int x = 10;
    int y = 20;
    int x1 = 20;
    int y1 = 10;
    char z('A');
     z = 'B';
    std::cout <<  x + y   <<std::endl;
    std::cout <<  x1 + y1  <<std::endl;
    std::cout <<  z  <<std::endl;
    std::cin.get();//这个可以不要
    return 0;
}
int main()
{
test();
return 0;
}

 枚举的用法:

#include<iostream>


int test(){
    enum Weekday{Mon= 1,Tsu= 2,Wen= 3};
    std::cout << Mon + Tsu + Wen <<std::endl;
    std::cin.get();//这个可以不要
    return 0;
}
int main()
{
test();
return 0;
}

 作用域的问题:

#include<iostream>


int test(){
    enum Weekday{Mon= 1,Tsu= 2,Wen= 3};

    {
        {
            { 
                int x = 10; 
                std::cout << x <<std::endl;
            }
        }
    }
    std::cout << Mon + Tsu + Wen <<std::endl;
    
    std::cin.get();//这个可以不要
    return 0;
}
int main()
{
test();

return 0;
}

 判断运算符和比较运算符:

#include<iostream>


int test(){
    enum Weekday{Mon= 1,Tsu= 2,Wen= 3};

    {
        {
            { 
                int x = 10; 
                std::cout << x <<std::endl;
            }
        }
    }
    if(2 > 1){
        std::cout << "确实大些!!" <<std::endl;
    }

    std::cout << Mon + Tsu + Wen <<std::endl;
    
    std::cin.get();//这个可以不要
    return 0;
}
int main()
{
test();

return 0;
}

 字符串的运用:

#include<iostream>
#include<string>
int test(){
    enum Weekday{Mon= 1,Tsu= 2,Wen= 3};

    {
        {
            { 
                int x = 10; 
                std::cout << x <<std::endl;
            }
        }
    }
    if(2 > 3){
        std::cout << "xiao!!" <<std::endl;
    }else{
        std::cout << "da!!" <<std::endl;
    }

    std::string  mystring("this is sting test");
    std::cout << Mon + Tsu + Wen <<std::endl;
    std::cout << mystring <<std::endl;
    std::cin.get();//这个可以不要
    return 0;
}
int main()
{
test();
  
return 0;
}

枚举和数组的用法:

#include<iostream>
#include<string>
int test(){
    enum Weekday{Mon= 1,Tsu= 2,Wen= 3};

    {
        {
            { 
                int x = 10; 
                std::cout << x <<std::endl;
            }
        }
    }
    if(2 > 3){
        std::cout << "xiao!!" <<std::endl;
    }else{
        std::cout << "da!!" <<std::endl;
    }

    std::string  mystring("this is sting test");
    std::cout << Mon + Tsu + Wen <<std::endl;
    std::cout << mystring <<std::endl;
    std::cin.get();//这个可以不要
double temp[3];
temp[0] = 10;
temp[1] = 12;
temp[2] = 20;

    return 0;
}
int main()
{
test();

return 0;
}

 指针的用法:

使用&可以获得任意变量的地址,但是必须在对应的指针中才能储存地址例如 long 的变量的地址就只能储存在long 的指针中。

记住指针一定要初始化

#include<iostream>
#include<string>
int test(){
long number;
number = 12345L;
long *pstr = 0L; //定义一直指针
pstr = &number; //获得变量的内存地址,将内存地址赋值给指针

std::cout << pstr <<std::endl;
std::cout << *pstr <<std::endl; //*间接运算符,和指针一起用,可以得到指针所指向的值 std::cin.
get(); return 0; } int main() { test(); return 0; }

指针的内存图

原文地址:https://www.cnblogs.com/sunxun/p/4867475.html