代码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>

include

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;
}
//返回对象本身使用*this void 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;
}

**最后 today完成**

最新文章

  1. javaScript里的原型链
  2. split函数的实现
  3. Android线程之AsyncTask
  4. 优化SqlServer--数据压缩
  5. 3DMax 物体选择方法
  6. [转]理解RESTful架构
  7. Groovy常见语法汇总
  8. Jackson最简单用法
  9. oracle sql语句跟踪及性能分析工具实现
  10. BZOJ_2662_[BeiJing wc2012]冻结_分层图最短路
  11. Java 批量修改文件夹里面的文件的名字
  12. .net 4.0 中的特性总结(一):dynamic
  13. SSM商城项目(八)
  14. Entity Framework Core: A second operation started on this context before a previous operation completed
  15. Wiener’s attack python
  16. 在Win7中用ftp的方法
  17. ACM数论之旅9---中国剩余定理(CRT)(壮哉我大中华╰(*&#176;▽&#176;*)╯)
  18. Python基础之文件
  19. Oracle使用row_number()函数查询时增加序号列
  20. LPTSTR\LPCTSTR\LPWSTR\LPCWSTR 字母的意思 及 区别

热门文章

  1. latex中显示代码
  2. css实现气泡提示框三角及css中drop-shadow的使用
  3. Nginx中FastCGI参数的优化配置实例
  4. 力扣算法:LC 704-二分查找,LC 27-移除元素--js
  5. 【Azure Developer】使用Azure PubSub服务示例代码时候遇见了.NET 6.0的代码转换问题
  6. 无线:PIN码
  7. 好客租房57-props深入(4props的默认值)
  8. MVC 调试页面路径变成 Views/Controller/Action.cshtml问题
  9. 20212115 实验三 《python程序设计》实验报告
  10. WIN32 API 获取文件版本信息