C++PrimerPlus学习笔记一

221 阅读1分钟

前言:

javaer 转 C++ 选手,开始学习!!生命不息,学习不止!!!

HelloWorld

#include <iostream> // 预处理器编译指令: 其实就是在程序编译之前将 iostream 中的内容加入到程序中,为了下面的 cout 和 cin进行输入和输出
#include <math.h> // .h 结尾的文件称为 头文件(head)。

using namespace std; // 可以使用 std名称空间中定义的名称,而不需要使用 std:: 前缀!


int main() {
    std::cout << "Hello, World!" << std::endl;

    // 因为 用了 using namespace std; 声明了名称空间,所以可以省略 std

    cout << "Hello, World!" << endl;  // endl 表示换行
    cout << "Hello, World!\n";  // \n 也表示换行

    cout << "sqrt(4) = " << sqrt(4) << endl;  // sqrt 函数就是 math.h 中引入的 内容

    return 0;
}


//运行结果:
Hello, World!
Hello, World!
Hello, World!
sqrt(4) = 2

cin和cout

#include <iostream>
using namespace std;


int main() {

    // 使用cin来接受输入

    int carrots; // 声明变量

    cout << "how many carrots do you have ?" << endl;
    cin >> carrots;  // 将输入的数字赋给 carrots变量
    cout << "here are two more.." << endl;
    carrots = carrots + 2;
    cout << "now you have " << carrots << " carrots. " << endl;

}

//运行结果:
how many carrots do you have ?
5
here are two more..
now you have 7 carrots. 

函数定义

#include <iostream>
using namespace std;

void simon(int n); // 函数原型,如果main函数中需要用到自定义函数必须要先定义函数原型

int sum(int, int);

int main() {

    simon(3);
    cout << "pick an integer: " << endl;
    int count;
    cin >> count;
    simon(count);
    cout << "Done !" << endl;

    int x , y;
    cout << "enter the x = ";
    cin >> x;

    cout << "enter the y = ";
    cin >> y;

    cout << "sum of x and y = " << sum(x,y) << endl;

}

// 自定义无返回值的函数
void simon(int n){
    cout << "simon says touch your toes " << n << " times. " << endl;
}

//自定义有返回值的函数
int sum(int x ,int y){
    return x + y ;
}


// 运行结果:
simon says touch your toes 3 times. 
pick an integer: 
5
simon says touch your toes 5 times. 
Done !
enter the x = 3
enter the y = 4
sum of x and y = 7

基本数据类型

#include <iostream>
#include "climits" // 预编译一些限制符号
using namespace std;


int main() {

    // 整型 分为 short 、 int 、 long 、 long long
    // 运算符 sizeof + 数据类型 可以查看占用的字节数
    cout << "short is " << sizeof (short) << " bytes!" << endl;
    cout << "int is " << sizeof (int) << " bytes!" << endl;
    cout << "long is " << sizeof (long) << " bytes!" << endl;
    cout << "long long is " << sizeof (long long) << " bytes!" << endl;


    int max_int = INT_MAX;
    short max_short = SHRT_MAX;
    long max_long = LONG_MAX;
    long  long max_long_long = LONG_LONG_MAX;

    cout << "Maximum values: " << endl;
    cout << "int: " << max_int << endl;
    cout << "short: " << max_short << endl;
    cout << "long: " << max_long << endl;
    cout << "long long: " << max_long_long << endl;


    unsigned int a = 1; // unsigned 表示是无符号数,只有正数

    // 正常 short 是 2个字节,1 byte = 8 bit ,所以表示的范围是 -32768 到 32767
    // 无符号 short 表示的范围是 0 - 65535
    unsigned short b = -1; // 如果赋值给-1 给一个无符号数,则变成了最大的数
    cout << "b = " << b << endl; // 65535


    // char 类型
    char ch = 'a';
    int num = ch; // 赋给 num 的是 字符 a 的 ASCII值
    cout << "ch = " << ch << endl;
    cout << "char is " << sizeof (char) << " bytes!" << endl;
    cout << "ASCII code for a is " << num << endl;
    
    // bool 类型
    cout << "bool is " << sizeof (bool) << " bytes!" << endl;
    
    // 浮点类型
    cout << "float is " << sizeof (float) << " bytes!" << endl;
    cout << "double is " << sizeof (double) << " bytes!" << endl; 

}

// 运行结果:
short is 2 bytes!
int is 4 bytes!
long is 8 bytes!
long long is 8 bytes!
Maximum values: 
int: 2147483647
short: 32767
long: 9223372036854775807
long long: 9223372036854775807
b = 65535
ch = a
char is 1 bytes!
ASCII code for a is 97
bool is 1 bytes!
float is 4 bytes!
double is 8 bytes!

const 常量

#include <iostream>
using namespace std;

#define YEAR  12; //常量方式1

int main() {

    const int Mouths = 12; //常量方式2

    //Mouths = 10; // 编译报错,常量是不允许修改值的

    cout << YEAR;

}