C++学习日记 day001

1、Ubuntu安装g++编译器

sudo apt-get update
sudo apt-get install g++

2、创建第一个文件main.cpp

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, world!" << endl;
    return 0;
}

3、编译

// 经过编译之后会在同目录下出现一个 a.out 文件
g++ main.cpp

4、运行文件

$ ./a.out
Hello, world!

5、使用-o选择指定生成的可执行程序的文件名

$ g++ helloworld.cpp -o helloworld
$ ./helloworld
Hello, World!

6、C++基本数据类型

// 1个字节
char  // -128~127或0~255
unsigned  // 0~255
signed  // -128~127

// 2个字节
short int
unsigned short int
signed short int


// 4个字节
int 
unsigned int
signed int
float

// 8个字节
long int
signed long int
unsigned long int
double

// 16个字节
long double

wchar_t  // 2个或4个字节

7、取别名,typedef声明

// 突然想起了初学C时候老是会打错,就用这个例子吧
typedef true ture;  // 表示代码中出现的ture表示true

8、枚举类型

// 变量的值可能有几个,这个变量也只可能是定义的这几个值
// 其中enum表示枚举函数,color表示枚举名,red,green表示枚举的值,通过为0、1、2递增
// c表示枚举变量
enum color { red, green, blue } c;
c = blue;

enum color { red, green=5, blue };


#include<iostream>

int main(){
    enum color {red,green,blue} c;
    c = blue;
    if (c== blue){
        std::cout<<"1";
    }
    return 0;
}

// out:1

#include<iostream>

int main(){
    enum color {red,green,blue} c;
    c = blue;
    if (c== blue){
        std::cout<<"blue' value is 2";
    }
    return 0;
}


// out: blue' value is 2

9、# define预处理器

#include <iostream>
using namespace std;
 
#define LENGTH 10   
#define WIDTH  5
#define newline'100'
 
int main()
{
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << newline;
   return 0;
}


// output:501

10、const关键字

#include <iostream>
using namespace std;
 
int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  
   
   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

// 50

11、static存储类

// static修饰的局部变量可以在函数调用之间保持局部变量的值
// static用于全局变量时,会使变量的作用域限制在声明它的文件内
// 当 static 用在类数据成员上时,会导致仅有一个该成员的副本被类的所有对象共享

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hithithithithit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值