代码1:转义字符
#include<iostream>using namespace std;void test01()//换行{ cout << "Hello World" << endl; //等价于cout << "Hello World\n" << endl;}void test02()//反斜杠{ cout<<"\\"<<endl; //打入两个反斜杠才输出一个反斜杠}void test03()//水平制表符\t{ //cout << "1111aaa\tHelloWorld" << endl; //\t占8个空格 主要是为了整齐的输出其后面的内容 cout << "111aaa\thelloworld" << endl; cout << "111a\thelloworld" << endl; cout << "111aa\thelloworld" << endl; // cout << "111aaa helloworld" << endl; cout << "111a helloworld" << endl; cout << "111aa helloworld" << endl; //没有\t并不会自动对齐}int main(){ test03(); system("pause"); return 0;}代码2:字符串类型
//字符串型//C语言的形式:char 变量名字[] = "字符串值";//C++的形式:string 变量名字 = "字符串值";#include<iostream>using namespace std;//#include<string>int main(){ char str[] = "Hello World"; cout << str << endl; string arr = "Hello World"; //包含头文件 cout << arr << endl; return 0;}代码3:布尔类型
<details><summary>点击查看代码</summary>using namespace std;
int main()
{
bool flag = true;
cout << flag << endl;
//结果为1
flag = false;
cout << flag << endl;
//结果为0
cout << sizeof(bool) << endl;
//bool所占内存空间:1个字节大小
return 0;
}
</details>//布尔类型只要是非0的值都代表是真
代码4:取模运算
//两个整数相除结果依然为整数
//两个小数是可以相除的
//运算结果也可以为小数
//两个数相除 除数不可以为0
#include<iostream>using namespace std;int main(){ //取模 cout << 10 % 20 << endl;//10 cout << 10 % 3 << endl;//1 cout << 10 % 0 << endl;//10不可以除以0 //cout << 1.1 % 2.1 << endl;//两个小数是不可以进行取模运算的 return 0;}//只有整型变量才可以进行取模运算
代码5:随机数
#include<iostream>using namespace std;//#include<ctime>int main(){ //添加随机数种子 //利用当前系统的时间去生成随机数 防止每一次随机数都一样 srand((unsigned int)time(NULL)); int num1 = rand() % 100;//产生0到99 int num2 = rand() % 100 + 1;//产生1到100 int num3 = 0; for (int i = 1;1; i++) { cin >> num3; if (num3 > num1) { cout << "猜大了" << endl; } else if (num3 < num1) { cout << "猜小了" << endl; } else { cout << "猜对了" << endl; break; } } return 0;}// srand((unsigned int)time(NULL));随机数种子
代码6:成员变量和成员函数分开存储
只有非静态的成员变量才属于类的对象上
#include<iostream>using namespace std;//成员变量 和 成员函数 是分开存储的class Person{ int m_A;//非静态的成员变量 - 属于类的对象上 static int m_B;//静态成员变量 - 不属于类的对象上 void func(){}//非静态的成员函数 - 不属于类的对象上 static void funcc(){} //静态成员函数 - 不属于类的对象上};int Person::m_B = 190;void test01(){ Person p1; //空对象 占用的内存空间为:1 cout << sizeof(p1) << endl;//1 //每个空对象也应该有一个独一无二的内存空间地址}void test02(){ Person p1; cout << sizeof(p1) << endl;//4}int main() { //只有非静态的成员变量才属于类的对象上 test02(); return 0;}代码7:this指针
:可以解决名称冲突
可以返回对象本身使用*this
#include<iostream>using namespace std;class Person{public: Person(int age) { //this指针 //this指针指向 被调用的成员函数 所属的对象 this->age = age; } //Person PersonAddAge(Person& p) 以值的方式去返回 Person& PersonAddAge(Person& p) { this->age += p.age; //this指向的就是p2的指针 那么*this就是指向的p2这个对象本体 return *this; //返回本体需要使用引用的方式 } int age;};//解决名称冲突void test01(){ Person p1(190); cout << p1.age << endl;}//返回对象本身使用*thisvoid test02(){ Person p1(190); Person p2(191); //p2.PersonAddAge(p1); Person p3(1); p2.PersonAddAge(p1).PersonAddAge(p3).PersonAddAge(p3); //链式编程思想 cout << p2.age << endl;}int main(){ test02(); //190+191=381 return 0;}代码8:空指针访问成员函数
#include<iostream>using namespace std;//空指针访问成员函数class Person{public: void ShowAge() { cout << "Age" << endl; } void Showage() { if (this == NULL) { return; } //cout << m_Age << endl; //传入的指针是为空的(NULL) cout << this->m_Age << endl; } int m_Age;};void test01(){ Person* p = NULL; //p->ShowAge(); p->Showage();}int main(){ test01(); return 0;}代码9:
const修饰成员函数
首先 常函数:
#include<iostream>using namespace std;class Person{public: //this指针的本质就是:指针常量 //指针常量 是常量 不是指针 指针的指向是不可以修改的 void showPerson() const//常函数 { m_B = 191; this->m_B = 190;//未报错//因为 this指针的本质可以表示为:Person* const this;是一个指针常量 在Person*前面在加一个const//const Person* const this; 就是对应得值也不可以修改了 等价于我们在成员函数后面加const //所以说在成员函数后面加const 修饰的就是this指针 让指针指向的值也不可以修改//m_A = 100; // 等价于this->m_A = 100; //this = NULL;//this指针是不可以修改指针的指向的 //不想被修改值 需要在函数名字后面加const } int m_A; mutable int m_B;//可以在常函数当中修改成员变量的值};//在成员函数后面加上const 该函数称为常函数//常函数内部是不可以修改成员属性的//成员属性声明的时候加关键字mutable之后 可以在常函数当中修改void test01(){ Person p; p.showPerson();}//在声明对象的前面加上const 这个对象叫做常对象//常对象只可以调用常函数int main(){ test01(); return 0;}#include<iostream>using namespace std;class Person{public: void func() { m_A = 1; } //this指针的本质就是:指针常量 //指针常量 是常量 不是指针 指针的指向是不可以修改的 void showPerson() const//常函数 { m_B = 191; this->m_B = 190;//未报错//因为 this指针的本质可以表示为:Person* const this;是一个指针常量 在Person*前面在加一个const//const Person* const this; 就是对应得值也不可以修改了 等价于我们在成员函数后面加const //所以说在成员函数后面加const 修饰的就是this指针 让指针指向的值也不可以修改//m_A = 100; // 等价于this->m_A = 100; //this = NULL;//this指针是不可以修改指针的指向的 //不想被修改值 需要在函数名字后面加const } int m_A; mutable int m_B;//可以在常函数当中修改成员变量的值};//在成员函数后面加上const 该函数称为常函数//常函数内部是不可以修改成员属性的//成员属性声明的时候加关键字mutable之后 可以在常函数当中修改void test01(){ Person p; p.showPerson();}//在声明对象的前面加上const 这个对象叫做常对象//常对象只可以调用常函数void test02(){ const Person p; //在对象的前面 加上 const 变为常对象 //依然不允许修改指针指向的值 //p.m_A = 100;//no p.m_B = 190;//yes //m_B 是特殊值 在常对象下面也可以修改//常对象只可以调用常函数 p.showPerson(); //p.func(); //常对象 不可以调用普通的成员函数 因为普通的成员函数是可以修改属性的}int main(){ test01(); return 0;}