• C++【个人笔记1】


    1.C++的初识

    1.1 简单入门

    1. #include
    2. using namespace std;
    3. int main() {
    4. cout << "hello world" << endl;
    5. return 0;
    6. }
    1. #include; 预编译指令,引入头文件iostream.
    2. using namespace std; 使用标准命名空间
    3. cout << “hello world”<< endl; 和printf功能一样,输出字符串”hello wrold”

    问题1:c++头文件为什么没有.h?

    在c语言中头文件使用扩展名.h,将其作为一种通过名称标识文件类型的简单方式。但是c++得用法改变了,c++头文件没有扩展名。但是有些c语言的头文件被转换为c++的头文件,这些文件被重新命名,丢掉了扩展名.h(使之成为c++风格头文件),并在文件名称前面加上前缀c(表明来自c语言)。例如c++版本的math.h为cmath.

    问题2:using namespace std 是什么?

    namespace是指标识符的各种可见范围。命名空间用关键字namespace 来定义。命名空间是C++的一种机制,用来把单个标识符下的大量有逻辑联系的程序实体组合到一起。此标识符作为此组群的名字。

    问题3:cout 、endl 是什么?

    cout是c++中的标准输出流,endl是输出换行并刷新缓冲区。

    1.2 namespace 【命名空间】

    C++中自定义命名空间(namespace)及其使用的三种方法_自定义namespace-CSDN博客

    由于namespace的概念,使用C++标准程序库的任何标识符时,可 以有三种选择:

    1)直接指定标识符。例如std::ostream而不是ostream。完整语句如 下:

    std::cout	<<	std::hex	<<	3.4	<<	std::endl;	

    2)使用using关键字。

    1. using std::cout;
    2. using std::endl;
    3. using std::cin;

    3)最方便的就是使用using namespace std;

    4.命名空间使用语法

    1. 创建一个命名空间:
    1. namespace A{
    2. int a = 10;
    3. }
    4. namespace B{
    5. int a = 20;
    6. }
    7. void test(){
    8. cout << "A::a : " << A::a << endl;
    9. cout << "B::a : " << B::a << endl;
    10. }

            2.命名空间只能全局范围内定义(以下错误写法)

    1. void test(){
    2. namespace A{
    3. int a = 10;
    4. }
    5. namespace B{
    6. int a = 20;
    7. }
    8. cout << "A::a : " << A::a << endl;
    9. cout << "B::a : " << B::a << endl;
    10. }

            3.命名空间可以嵌套命名

    1. namespace A{
    2. int a = 10;
    3. namespace B{
    4. int a = 20;
    5. }
    6. }
    7. void test(){
    8. cout << "A::a : " << A::a << endl;
    9. cout << "A::B::a : " << A::B::a << endl;
    10. }

            4.命名空间是开放的,可以随时加入新的成员

    1. namespace A{
    2. int a = 10;
    3. }
    4. namespace A{
    5. void func(){
    6. cout << "hello namespace!" << endl;
    7. }
    8. }

            5.声明和实现可以分离

    1. #pragma once
    2. namespace MySpace{
    3. void func1();
    4. void func2(int param);
    5. }
    6. void MySpace::func1(){
    7. cout << "MySpace::func1" << endl;
    8. }
    9. void MySpace::func2(int param){
    10. cout << "MySpace::func2 : " << param << endl;
    11. }

            6.命名空间起别名

    1. namespace veryLongName{
    2. int a = 10;
    3. void func(){ cout << "hello namespace" << endl; }
    4. }
    5. void test(){
    6. namespace shortName = veryLongName;
    7. cout << "veryLongName::a : " << shortName::a << endl;
    8. veryLongName::func();
    9. shortName::func();
    10. }

    2.C++对C的扩展

    2.1 ::作用域运算符

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. //定义一个命名空间
    5. namespace spaccA {
    6. int g_a = 10;
    7. }
    8. namespace spaceB {
    9. int a = 20;
    10. namespace spaceC {
    11. struct teacher
    12. {
    13. int id;
    14. char name[64];
    15. };
    16. }
    17. namespace spaceD {
    18. struct teacher
    19. {
    20. int id;
    21. char name[64];
    22. };
    23. }
    24. using namespace spaceC;
    25. }
    26. int main(void)
    27. {
    28. //using spaccA::g_a;
    29. using namespace spaccA;
    30. int a = 10;
    31. cout << g_a << endl;
    32. //spaceB::spaceC::teacher t1;
    33. //using spaceB::spaceC::teacher;
    34. //teacher t1;
    35. //using namespace spaceB::spaceC;
    36. //spaceB::spaceC::teacher t1;
    37. using namespace spaceB;
    38. teacher t1;
    39. t1.id = 10;
    40. //spaceB::spaceD::teacher t2;
    41. //t2.id = 20;
    42. return 0;
    43. }

    2.2 "实用性“增强

    c语言一定要在使用之前先定义

    c++可以在使用的时候进行定义

    1. //C语⾔中的变量都必须在作⽤域开始的位置定义!!
    2. //C++中更强调语⾔的“实⽤性”,所有的变量都可以在需要使⽤时再定义。
    3. #include
    4. using namespace std;
    5. int main() {
    6. int i = 0;
    7. cout << "i=" << i << endl;
    8. int k = 4;
    9. cout << "k=" << k << endl;
    10. return 0;
    11. }

    2.3 变量检测增强

    c语言允许定义多个同名的变量

    c++不能定义多个同名变量,即使作用域不同

    1. /*
    2. 在C语⾔中,重复定义多个同名的全局变量是合法的
    3. 在C++中,不允许定义多个同名的全局变量
    4. C语⾔中多个同名的全局变量最终会被链接到全局数据区的同⼀个地址空间上
    5. int g_var;
    6. int g_var = 1;
    7. C++直接拒绝这种⼆义性的做法。
    8. */
    9. #include
    10. int g_var;
    11. int g_var = 1;
    12. int main(int argc, char* argv[])
    13. {
    14. printf("g_var = %d\n", g_var);
    15. return 0;
    16. }

    2.4 C++中所有变量和函数都必须有类型

    C++中所有的变量和函数都必须有类型

    C语⾔中的默认类型在C++中是不合法的

    函数f的返回值是什么类型,参数⼜是什么类型?

    函数g可以接受多少个参数?

    1. //i没有写类型,可以是任意类型
    2. int fun1(i){
    3. printf("%d\n", i);
    4. return 0;
    5. }
    6. //i没有写类型,可以是任意类型
    7. int fun2(i){
    8. printf("%s\n", i);
    9. return 0;
    10. }
    11. //没有写参数,代表可以传任何类型的实参
    12. int fun3(){
    13. printf("fun33333333333333333\n");
    14. return 0;
    15. }
    16. //C语言,如果函数没有参数,建议写void,代表没有参数
    17. int fun4(void){
    18. printf("fun4444444444444\n");
    19. return 0;
    20. }
    21. g(){
    22. return 10;
    23. }
    24. int main(){
    25. fun1(10);
    26. fun2("abc");
    27. fun3(1, 2, "abc");
    28. printf("g = %d\n", g());
    29. return 0;
    30. }
    1. 在C语言中,int fun() 表示返回值为int,接受任意参数的函数,int fun(void) 表示返回值为int的无参函数。
    2. 在C++ 中,int fun() 和int fun(void) 具有相同的意义,都表示返回值为int的无参函数。

    2.5 更严格的类型转换

    在C++,不同类型的变量一般是不能直接赋值的,需要相应的强转。

    以上c代码c编译器编译可通过,c++编译器无法编译通过。

    1. typedef enum COLOR{ GREEN, RED, YELLOW } color;
    2. int main(){
    3. color mycolor = GREEN;
    4. mycolor = 10;
    5. printf("mycolor:%d\n", mycolor);
    6. char* p = malloc(10);
    7. return EXIT_SUCCESS;
    8. }

    2.6 struct类型加强

    1. c中定义结构体变量需要加上struct关键字,c++不需要。
    2. c中的结构体只能定义成员变量,不能定义成员函数。c++即可以定义成员变量,也可以定义成员函数。
    1. //1. 结构体中即可以定义成员变量,也可以定义成员函数
    2. struct Student{
    3. string mName;
    4. int mAge;
    5. void setName(string name){ mName = name; }
    6. void setAge(int age){ mAge = age; }
    7. void showStudent(){
    8. cout << "Name:" << mName << " Age:" << mAge << endl;
    9. }
    10. };
    11. //2. c++中定义结构体变量不需要加struct关键字
    12. void test01(){
    13. Student student;
    14. student.setName("John");
    15. student.setAge(20);
    16. student.showStudent();
    17. }

    2.7 对函数返回参数个数的要求

    C传递参数大于实际定义的参数,只会报警告,C++传递参数大于实际定义的参数会报错

    2.8 新增的bool类的关键字

    bool b=100;//无论将修改为多少,sizeof(b)永远为1

    1. #include
    2. using namespace std;
    3. int main(int argc, char* argv[])
    4. {
    5. int a;
    6. bool b = true;
    7. printf("b = %d, sizeof(b) = %d\n", b, sizeof(b));//1 1
    8. b = 4;
    9. a = b;
    10. printf("a = %d, b = %d\n", a, b);//1 1
    11. b = -4;
    12. a = b;
    13. printf("a = %d, b = %d\n", a, b);//1 1
    14. a = 10;
    15. b = a;
    16. printf("a = %d, b = %d\n", a, b);//10 1
    17. b = 0;
    18. printf("b = %d\n", b);//0
    19. return 0;
    20. }

    2.9 三目运算符功能增强

    1. c语言三目运算表达式返回值为数据值,为右值,不能赋值。
    1. int a = 10;
    2. int b = 20;
    3. printf("ret:%d\n", a > b ? a : b);
    4. //思考一个问题,(a > b ? a : b) 三目运算表达式返回的是什么?
    5. //(a > b ? a : b) = 100;//错误
    6. //返回的是右值
    1. c++语言三目运算表达式返回值为变量本身(引用),为左值,可以赋值。
    1. int a = 10;
    2. int b = 20;
    3. printf("ret:%d\n", a > b ? a : b);
    4. //思考一个问题,(a > b ? a : b) 三目运算表达式返回的是什么?
    5. cout << "b:" << b << endl;//20
    6. //返回的是左值,变量的引用
    7. (a > b ? a : b) = 100;//返回的是左值,变量的引用
    8. cout << "b:" << b << endl;//100

    2.10 const增强

    const单词字面意思为常数,不变的。它是c/c++中的一个关键字,是一个限定符,它用来限定一个变量不允许改变,它将一个对象转换成一个常量。

    1.c语言中的const

    们c中的const理解为”一个不能改变的普通变量”,也就是认为const应该是一个只读变量,既然是变量那么就会给const分配内存,并且在c中const是一个全局只读变量,c语言中const修饰的只读变量是外部连接的。

    2.c++中的const

    在c++中,一个const不必创建内存空间,而在c中,一个const总是需要一块内存空间。在c++中,是否为const常量分配内存空间依赖于如何使用。一般说来,如果一个const仅仅用来把一个名字用一个值代替(就像使用#define一样),那么该存储局空间就不必创建。

    3.C/C++中const异同总结

    1. //const 定义常量---> const 意味只读
    2. const int a;
    3. int const b;
    4. //第⼀个第⼆个意思⼀样 代表⼀个常整形数
    5. const int *c;
    6. //第三个 c是⼀个指向常整形数的指针(所指向的内存数据不能被修改,但是本⾝可以修改)
    7. int * const d;
    8. //第四个 d 常指针(指针变量不能被修改,但是它所指向内存空间可以被修改)
    9. const int * const e ;
    10. //第五个 e⼀个指向常整形的常指针(指针和它所指向的内存空间,均不能被修改)

    4.const 和 #define 的相同

    1. #include
    2. //#define N 10
    3. int main()
    4. {
    5. const int a = 1;
    6. const int b = 2;
    7. int array[a + b] = {0};
    8. int i = 0;
    9. for(i = 0; i < (a+b); i++)
    10. {
    11. printf("array[%d] = %d\n", i, array[i]);
    12. }
    13. return 0;
    14. }

    C++中的const修饰的,是一个真正的常量,而不是C中变量(只读)。在 const修饰的常量编译期间,就已经确定下来了

    5.const 和 #define 的区别

    1. #include
    2. void fun1()
    3. {
    4. #define a 10
    5. const int b = 20;
    6. }
    7. void fun2()
    8. {
    9. printf("a = %d\n", a);
    10. //printf("b = %d\n", b);
    11. }
    12. int main()
    13. {
    14. fun1();
    15. fun2();
    16. return 0;
    17. }

    1. const有类型,可进行编译器类型安全检查。#define无类型,不可进行类型检查.
    2. const有作用域,而#define不重视作用域,默认定义处到文件结尾.如果定义在指定作用域下有效的常量,那么#define就不能用。

    辨析;
     

    1. #include
    2. int main()
    3. {
    4. const int a = 10;
    5. //将a的地址传递给p
    6. int* p = (int*)&a;
    7. printf("a===>%d\n", a);//10
    8. *p = 11;
    9. printf("a===>%d\n", a);//10
    10. return 0;
    11. }
    1. #include
    2. //#define N 10
    3. int main()
    4. {
    5. const int a = 1;
    6. const int b = 2;
    7. int array[a + b] = { 0 };
    8. int i = 0;
    9. for (i = 0; i < (a + b); i++)
    10. {
    11. printf("array[%d] = %d\n", i, array[i]);//0 0 0
    12. }
    13. return 0;
    14. }
    1. #include
    2. void fun1()
    3. {
    4. #define a 10
    5. const int b = 20;
    6. }
    7. void fun2()
    8. {
    9. printf("a = %d\n", a);//10
    10. //printf("b = %d\n", b);
    11. }
    12. int main()
    13. {
    14. fun1();
    15. fun2();
    16. return 0;
    17. }

    2.11 真正的枚举

    c 语言中枚举本质就是整型,枚举变量可以用任意整型赋值。而 c++中枚举 变量, 只能用被枚举出来的元素初始化。

    1. #include
    2. using namespace std;
    3. enum season {SPR,SUM,AUT,WIN};
    4. int main()
    5. {
    6. enum season s = SPR;
    7. //s = 0; // error, 但是C语⾔可以通过
    8. s = SUM;
    9. cout << "s = " << s <//1
    10. return 0;
    11. }

    3 引⽤(reference)

    3.1 引用基本用法

    引用是c++对c的重要扩充。在c/c++中指针的作用基本都是一样的,但是c++增加了另外一种给函数传递地址的途径,这就是按引用传递(pass-by-reference),它也存在于其他一些编程语言中,并不是c++的发明。

    1. 变量名实质上是一段连续内存空间的别名,是一个标号(门牌号)
    2. 程序中通过变量来申请并命名内存空间
    3. 通过变量的名字可以使用存储空间

    对一段连续的内存空间只能取一个别名吗?

    c++中新增了引用的概念,引用可以作为一个已定义变量的别名。

    基本语法

    1. Type & ref=val;
    2. int & ref=&a;//表示ref是a的别名
    1. #include
    2. using namespace std;
    3. int main() {
    4. int a = 10;
    5. int& b = a;//int* b
    6. a = 11;
    7. {
    8. int* p = &a;
    9. *p = 12;
    10. cout << a << endl;//12
    11. }
    12. b = 14;
    13. cout << a <<"" << b << endl;//14 14
    14. }

    注意事项:

    1. &在此不是求地址运算,而是起标识作用。
    2. 类型标识符是指目标变量的类型
    1. 必须在声明引用变量时进行初始化。
    2. 引用初始化之后不能改变。
    3. 不能有NULL引用。必须确保引用是和一块合法的存储单元关联。
    4. 可以建立对数组的引用。
    1. //1. 认识引用
    2. void test01(){
    3. int a = 10;
    4. //给变量a取一个别名b
    5. int& b = a;
    6. cout << "a:" << a << endl;
    7. cout << "b:" << b << endl;
    8. cout << "------------" << endl;
    9. //操作b就相当于操作a本身
    10. b = 100;
    11. cout << "a:" << a << endl;
    12. cout << "b:" << b << endl;
    13. cout << "------------" << endl;
    14. //一个变量可以有n个别名
    15. int& c = a;//给c取了一个名为”c”的别名
    16. c = 200;
    17. cout << "a:" << a << endl;
    18. cout << "b:" << b << endl;
    19. cout << "c:" << c << endl;
    20. cout << "------------" << endl;
    21. //a,b,c的地址都是相同的
    22. cout << "a:" << &a << endl;
    23. cout << "b:" << &b << endl;
    24. cout << "c:" << &c << endl;
    25. }
    26. //2. 使用引用注意事项
    27. void test02(){
    28. //1) 引用必须初始化
    29. //int& ref; //报错:必须初始化引用
    30. //2) 引用一旦初始化,不能改变引用
    31. int a = 10;
    32. int b = 20;
    33. int& ref = a;
    34. ref = b; //不能改变引用
    35. //3) 不能对数组建立引用
    36. int arr[10];
    37. //int& ref3[10] = arr;
    38. }

    3.2 注意点 

    1.一个变量可以有n个别名

    2.操作别名等于操作原数

    3.别名和操作数的地址是一样的

    4.引用必须初始化

    5.一旦初始化,不能进行修改

    6.不能对数组建立引用

    3.3 建立对数组的引用

    方法一:

    1. typedef int ArrRef[10];
    2. int arr[10];
    3. ArrRef& aRef = arr;
    4. for (int i = 0; i < 10;i ++){
    5. aRef[i] = i+1;
    6. }
    7. for (int i = 0; i < 10;i++){
    8. cout << arr[i] << " ";
    9. }
    10. cout << endl;

    方法二:

    1. int(&f)[10] = arr;
    2. for (int i = 0; i < 10; i++){
    3. f[i] = i+10;
    4. }
    5. for (int i = 0; i < 10; i++){
    6. cout << arr[i] << " ";
    7. }
    8. cout << endl;

    3.4 函数中的引用

    当引用被用作函数参数的时,在函数内对任何引用的修改,将对还函数外的参数产生改变

    1. //值传递
    2. void ValueSwap(int m,int n){
    3. int temp = m;
    4. m = n;
    5. n = temp;
    6. }
    7. //地址传递
    8. void PointerSwap(int* m,int* n){
    9. int temp = *m;
    10. *m = *n;
    11. *n = temp;
    12. }
    13. //引用传递
    14. void ReferenceSwap(int& m,int& n){
    15. int temp = m;
    16. m = n;
    17. n = temp;
    18. }
    19. void test(){
    20. int a = 10;
    21. int b = 20;
    22. //值传递
    23. ValueSwap(a, b);
    24. cout << "a:" << a << " b:" << b << endl;
    25. //地址传递
    26. PointerSwap(&a, &b);
    27. cout << "a:" << a << " b:" << b << endl;
    28. //引用传递
    29. ReferenceSwap(a, b);
    30. cout << "a:" << a << " b:" << b << endl;
    31. }

    通过引用参数产生的效果同按地址传递是一样的。引用的语法更清楚简单:  

    1. 函数调用时传递的实参不必加“&”符
    2. 在被调函数中不必在参数前加“*”符

    引用作为其它变量的别名而存在,因此在一些场合可以代替指针。C++主张用引用传递取代地址传递的方式,因为引用语法容易且不易出错。

    1. #include
    2. using namespace std;
    3. int getA1()
    4. {
    5. int a;
    6. a = 10;
    7. return a;
    8. }
    9. int& getA2()
    10. {
    11. int a;
    12. a = 10;
    13. return a;
    14. }
    15. int main(void)
    16. {
    17. int a1 = 0;
    18. int a2 = 0;
    19. //值拷⻉
    20. a1 = getA1();
    21. //将⼀个引⽤赋给⼀个变量,会有拷⻉动作
    22. //理解: 编译器类似做了如下隐藏操作,a2 = *(getA2())
    23. a2 = getA2();
    24. //将⼀个引⽤赋给另⼀个引⽤作为初始值,由于是栈的引⽤,内存⾮法
    25. int& a3 = getA2();
    26. cout << "a1 = " << a1 << endl;
    27. cout << "a2 = " << a2 << endl;
    28. cout << "a3 = " << a3 << endl;
    29. return 0;
    30. }
    1. #include
    2. using namespace std;
    3. //函数当左值
    4. //返回变量的值
    5. int func1()
    6. {
    7. static int a1 = 10;
    8. return a1;
    9. }
    10. //返回变量本⾝ ,
    11. int& func2()
    12. {
    13. static int a2 = 10;
    14. return a2;
    15. }
    16. int main(void)
    17. {
    18. //函数当右值
    19. int c1 = func1();
    20. cout << "c1 = " << c1 << endl;
    21. int c2 = func2(); //函数返回值是⼀个引⽤,并且当右值
    22. cout << "c2 = " << c2 << endl;
    23. //函数当左值
    24. //func1() = 100; //error
    25. func2() = 100; //函数返回值是⼀个引⽤,并且当左值
    26. c2 = func2();
    27. cout << "c2 = " << c2 << endl;
    28. return 0;
    29. }

    注意点:

    1. 不能返回局部变量的引用。
    2. 函数当左值,必须返回引用。【因为引用定义的函数,返回的是别名】

    3.5 引用的本质:指针常量.(int* const ref)

    引用的本质在c++内部实现是一个指针常量.

    Type& ref = val; // Type* const ref = &val;

    c++编译器在编译过程中使用常指针作为引用的内部实现,因此引用所占用的空间大小与指针相同,只是这个过程是编译器内部实现,用户不可见。

    1. //发现是引用,转换为 int* const ref = &a;
    2. void testFunc(int& ref){
    3. ref = 100; // ref是引用,转换为*ref = 100
    4. }
    5. int main(){
    6. int a = 10;
    7. int& aRef = a; //自动转换为 int* const aRef = &a;这也能说明引用为什么必须初始化
    8. aRef = 20; //内部发现aRef是引用,自动帮我们转换为: *aRef = 20;
    9. cout << "a:" << a << endl;
    10. cout << "aRef:" << aRef << endl;
    11. testFunc(a);
    12. return EXIT_SUCCESS;
    13. }

    3.6 指针引用

    对于c++中的定义那个,语法清晰多了。函数参数变成指针的引用,用不着取得指针的地址。

    1. struct Teacher{
    2. int mAge;
    3. };
    4. //指针间接修改teacher的年龄
    5. void AllocateAndInitByPointer(Teacher** teacher){
    6. *teacher = (Teacher*)malloc(sizeof(Teacher));
    7. (*teacher)->mAge = 200;
    8. }
    9. //引用修改teacher年龄
    10. void AllocateAndInitByReference(Teacher*& teacher){
    11. teacher->mAge = 300;
    12. }
    13. void test(){
    14. //创建Teacher
    15. Teacher* teacher = NULL;
    16. //指针间接赋值
    17. AllocateAndInitByPointer(&teacher);
    18. cout << "AllocateAndInitByPointer:" << teacher->mAge << endl;
    19. //引用赋值,将teacher本身传到ChangeAgeByReference函数中
    20. AllocateAndInitByReference(teacher);
    21. cout << "AllocateAndInitByReference:" << teacher->mAge << endl;
    22. free(teacher);
    23. }
    1. #include
    2. using namespace std;
    3. struct Teacher
    4. {
    5. char name[64];
    6. int age;
    7. };
    8. //在被调⽤函数 获取资源
    9. int getTeacher(Teacher * *p)
    10. {
    11. Teacher* tmp = NULL;
    12. if (p == NULL)
    13. {
    14. return -1;
    15. }
    16. tmp = (Teacher*)malloc(sizeof(Teacher));
    17. if (tmp == NULL)
    18. {
    19. return -2;
    20. }
    21. tmp->age = 33;
    22. // p是实参的地址 *实参的地址 去间接的修改实参的值
    23. *p = tmp;
    24. return 0;
    25. }
    26. //指针的引⽤ 做函数参数
    27. int getTeacher2(Teacher*& myp)
    28. {
    29. //给myp赋值 相当于给main函数中的pT1赋值
    30. myp = (Teacher*)malloc(sizeof(Teacher));
    31. if (myp == NULL)
    32. {
    33. return -1;
    34. }
    35. myp->age = 36;
    36. return 0;
    37. }
    38. void FreeTeacher(Teacher* pT1)
    39. {
    40. if (pT1 == NULL)
    41. {
    42. return;
    43. }
    44. free(pT1);
    45. }
    46. int main(void)
    47. {
    48. Teacher* pT1 = NULL;
    49. //1 c语⾔中的⼆级指针
    50. getTeacher(&pT1);
    51. cout << "age:" << pT1->age << endl;
    52. FreeTeacher(pT1);
    53. //2 c++中的引⽤ (指针的引⽤)
    54. //引⽤的本质 间接赋值后2个条件 让c++编译器帮我们程序员做了。
    55. getTeacher2(pT1);
    56. cout << "age:" << pT1->age << endl;
    57. FreeTeacher(pT1);
    58. return 0;
    59. }

    3.7 常量引用(const)

    常量引用的定义格式:

    const Type& ref = val;

    注意:

    1. 如果想对一个常量进行引用, 必须是一个const引用。
    2. const修饰的引用,不能修改。

    1. void test01(){
    2. int a = 100;
    3. const int& aRef = a; //此时aRef就是a
    4. //aRef = 200; 不能通过aRef的值
    5. a = 120; //OK
    6. cout << "a:" << a << endl;//120
    7. cout << "aRef:" << aRef << endl;//120
    8. }
    9. void test02(){
    10. //不能把一个字面量赋给引用
    11. //int& ref = 100;
    12. //但是可以把一个字面量赋给常引用
    13. //因为const int的级别比int高
    14. const int& ref = 100; //int temp = 200; const int& ret = temp;
    15. }

        [const引用使用场景]

        常量引用主要用在函数的形参,尤其是类的拷贝/复制构造函数。

    将函数的形参定义为常量引用的好处:

    • 引用不产生新的变量,减少形参与实参传递时的开销。
    • 由于引用可能导致实参随形参改变而改变,将其定义为常量引用可以消除这种副作用。

        如果希望实参随着形参的改变而改变,那么使用一般的引用,如果不希望实参随着形参改变,那么使用常引用。

    1. //const int& param防止函数中意外修改数据
    2. void ShowVal(const int& param){
    3. cout << "param:" << param << endl;
    4. }

    3.8 例子

    C++引用10分钟入门教程

    1. #include
    2. using namespace std;
    3. int main(void)
    4. {
    5. int a = 10; //c编译器分配4个字节内存, a内存空间的别名
    6. int& b = a; //b就是a的别名-->const * int b=a
    7. a = 11; //直接赋值
    8. {
    9. int* p = &a;
    10. *p = 12;
    11. cout << a << endl;//12
    12. }
    13. b = 14;
    14. cout << "a = " << a << ", b = " << b << endl;//14 14
    15. return 0;
    16. }

    结论:可以通过普通参数修改引用,但是不能通过引用修改参数 

    1. #include
    2. using namespace std;
    3. int main() {
    4. int a = 20;
    5. int& ref = a;
    6. ref = 30;
    7. cout << ref << a << endl;//30 30
    8. int b = 40;
    9. const int& ref2 = b;
    10. b = 100;
    11. cout << ref2 << b << endl;//100 100
    12. int& rref = ref;
    13. rref = 200;
    14. cout << rref << ref << a << endl;//200 200 200
    15. }

    作业:

    1.简述C++中命名空间的作用。

    namespace AAA{
        int a;

    1. using AAA::a;
    2. AAA::a;
    3. using namespace AAA;

    2. 定义两个命名空间A 和 B 分别在A中和B中定义变量value

    1. #include
    2. using namespace std;
    3. namespace A {
    4. int a = 10;
    5. }
    6. namespace B {
    7. int b = 20;
    8. }
    9. int main() {
    10. using namespace A;
    11. using namespace B;
    12. cout << A::a << endl;
    13. cout << B::b << endl;
    14. }

    3. C语言的三目运算符 ? : , 可以当左值么? C++的是否可以? 为什么?

    c语言的三目运算符不可以当左值,因为返回的是一个常量。

    c++的三目运算符可以做左值

    4. 下面哪条语句是错误的?为什么?

    (1 < 2? a: b) = 100;//正确,因为结果是一个变量,可以被赋值
       
    (1 < 2? 10: 20) = 100//错误,因为结果是一个常量,常量不能被常量赋值

    5. const int a; 在C++编译器中是否需要初始化,为什么?
     

    在c++中,一旦声明一个const变量,则在声明的过程中一定要对起进行初始化

    6.temp

    1. int main() {
    2. int a = 10;
    3. int* p = (int*)&a;
    4. //*p:实际上修改的是temp的值
    5. *p = 20;
    6. cout << a << endl;//20
    7. cout << *p << endl;//20
    8. }

    6. 简述引用的特点?

    1.引用本身不占用内存空间,与被引用对象共用内存

    2.引用定义时,必须初始化

    3.引用的类型必须与被引用对象的类型保持一致 比如:int a; 引用必须使用int&

    4.引用只能引用一个对象,一个对象可以被多次引用。

    7.判断

    1. int main() {
    2. int a = 20;
    3. const int& re_a = a;
    4. re_a = 30;//错误,不能直接修改const变量
    5. }

     4.inline内联函数

    https://www.cnblogs.com/chengxuyuancc/archive/2013/04/04/2999844.html

    4.1 内联函数的引出

    c++从c中继承的一个重要特征就是效率。假如c++的效率明显低于c的效率,那么就会有很大的一批程序员不去使用c++了。

    4.2  预处理宏的缺陷

    宏函数是预处理器进行的

    内联的编译器处理的

    预处理器宏存在问题的关键是我们可能认为预处理器的行为和编译器的行为是一样的。当然也是由于宏函数调用和函数调用在外表看起来是一样的,因为也容易被混淆。但是其中也会有一些微妙的问题出现:

    问题一:

    1. #define ADD(x,y) x+y
    2. inline int Add(int x,int y){
    3. return x + y;
    4. }
    5. void test(){
    6. int ret1 = ADD(10, 20) * 10; //希望的结果是300
    7. int ret2 = Add(10, 20) * 10; //希望结果也是300
    8. cout << "ret1:" << ret1 << endl; //210
    9. cout << "ret2:" << ret2 << endl; //300
    10. }

    问题二:

    1. #define COMPARE(x,y) ((x) < (y) ? (x) : (y))
    2. int Compare(int x,int y){
    3. return x < y ? x : y;
    4. }
    5. void test02(){
    6. int a = 1;
    7. int b = 3;
    8. //cout << "COMPARE(++a, b):" << COMPARE(++a, b) << endl; // 3
    9. cout << "Compare(int x,int y):" << Compare(++a, b) << endl; //2
    10. }

    4.3 内联函数基本概念:空间换时间

    在c++中,预定义宏的概念是用内联函数来实现的,而内联函数本身也是一个真正的函数。内联函数具有普通函数的所有行为。唯一不同之处在于内联函数会在适当的地方像预定义宏一样展开,所以不需要函数调用的开销。因此应该不使用宏,使用内联函数。

    注意必须函数体和声明结合在一起,否则编译器将它作为普通函数来对待。

    1. inline void func(int a);
    2. inline int func(int a) {
    3. return a;
    4. }

    内联函数的确占用空间,但是内联函数相对于普通函数的优势只是省去了函数调用时候的压栈,跳转,返回的开销。我们可以理解为内联函数是以空间换时间

    4.4 类内部的内联函数

    为了定义内联函数,通常必须在函数定义前面放一个inline关键字。但是在类内部定义内联函数时并不是必须的。任何在类内部定义的函数自动成为内联函数。

    1. class Person{
    2. public:
    3. Person(){ cout << "构造函数!" << endl; }
    4. void PrintPerson(){ cout << "输出Person!" << endl; }
    5. }

    4.5 内联函数和编译器

    4.6 注意点

    • 不能存在任何形式的循环语句
    • 不能存在过多的条件判断语句
    • 函数体不能过于庞大
    • 不能对函数进行取址操作

    内联仅仅只是给编译器一个建议,编译器不一定会接受这种建议,如果你没有将函数声明为内联函数,那么编译器也可能将此函数做内联编译。一个好的编译器将会内联小的、简单的函数。

    4.7 内联函数 VS 宏函数

    4.8 总结

    5. 函数的默认参数和占位函数

    5.1 默认参数

    c++在声明函数原型的时可为一个或者多个参数指定默认(缺省)的参数值,当函数调用的时候如果没有指定这个值,编译器会自动用默认值代替。

    1. void TestFunc01(int a = 10, int b = 20){
    2. cout << "a + b = " << a + b << endl;
    3. }
    4. //注意点:
    5. //1. 形参b设置默认参数值,那么后面位置的形参c也需要设置默认参数
    6. void TestFunc02(int a,int b = 10,int c = 10){}
    7. //2. 如果函数声明和函数定义分开,函数声明设置了默认参数,函数定义不能再设置默认参数
    8. void TestFunc03(int a = 0,int b = 0);
    9. void TestFunc03(int a, int b){}
    10. int main(){
    11. //1.如果没有传参数,那么使用默认参数
    12. TestFunc01();
    13. //2. 如果传一个参数,那么第二个参数使用默认参数
    14. TestFunc01(100);
    15. //3. 如果传入两个参数,那么两个参数都使用我们传入的参数
    16. TestFunc01(100, 200);
    17. return EXIT_SUCCESS;
    18. }

    注意点:

    5.2  函数的占位参数

    c++在声明函数时,可以设置占位参数。占位参数只有参数类型声明,而没有参数名声明。一般情况下,在函数体内部无法使用占位参数。【占位符也是参数,必须传参数】

    1. void TestFunc01(int a,int b,int){
    2. //函数内部无法使用占位参数
    3. cout << "a + b = " << a + b << endl;
    4. }
    5. //占位参数也可以设置默认值
    6. void TestFunc02(int a, int b, int = 20){
    7. //函数内部依旧无法使用占位参数
    8. cout << "a + b = " << a + b << endl;
    9. }
    10. int main(){
    11. //错误调用,占位参数也是参数,必须传参数
    12. //TestFunc01(10,20);
    13. //正确调用
    14. TestFunc01(10,20,30);
    15. //正确调用
    16. TestFunc02(10,20);
    17. //正确调用
    18. TestFunc02(10, 20, 30);
    19. return EXIT_SUCCESS;
    20. }

    6. 函数重载(overload)

    6.1 函数重载概述

        能使名字方便使用,是任何程序设计语言的一个重要特征!

    在传统c语言中,函数名必须是唯一的,程序中不允许出现同名的函数。在c++中是允许出现同名的函数,这种现象称为函数重载。

    函数重载的目的就是为了方便的使用函数名。

    6.2 函数重载基本语法

    实现函数重载的条件:

    1. 同一个作用域
    2. 参数个数不同
    3. 参数类型不同
    4. 参数顺序不同
    5. 返回值并不是构成函数重载的条件
    1. //1. 函数重载条件
    2. namespace A{
    3. void MyFunc(){ cout << "无参数!" << endl; }
    4. void MyFunc(int a){ cout << "a: " << a << endl; }
    5. void MyFunc(string b){ cout << "b: " << b << endl; }
    6. void MyFunc(int a, string b){ cout << "a: " << a << " b:" << b << endl;}
    7. void MyFunc(string b, int a){cout << "a: " << a << " b:" << b << endl;}
    8. }
    9. //2.返回值不作为函数重载依据
    10. namespace B{
    11. void MyFunc(string b, int a){}
    12. //int MyFunc(string b, int a){} //无法重载仅按返回值区分的函数
    13. }

    注意: 函数重载和默认参数一起使用,需要额外注意二义性问题的产生。

    为什么函数返回值不作为重载条件呢?

    调用准则

    1. #include
    2. using namespace std;
    3. void print(double a) {
    4. cout << a << endl;
    5. }
    6. void print(int a) {
    7. cout << a << endl;
    8. }
    9. int main()
    10. {
    11. print(1); // print(int)
    12. print(1.1); // print(double)
    13. print('a'); // print(int)
    14. print(1.11f); // print(double)
    15. return 0;
    16. }

    编译器调用重载函数的准则:

    6.3 重载底层实现(name mangling)

    6.4 函数重载与函数默认参数

    6.5 函数重载和函数指针

    函数指针基本语法

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. int func(int a, int b)
    5. {
    6. cout << "func(int, int)" << endl;
    7. return 0;
    8. }
    9. int func(int a, int b, int c)
    10. {
    11. cout << "func(int, int,int )" << endl;
    12. return 0;
    13. }
    14. //1 . 定义一种函数类型
    15. typedef int(MY_FUNC)(int, int);
    16. //2 顶一个指向之中函数类型的指针类型
    17. typedef int(*MY_FUNC_P)(int, int);
    18. int main(void)
    19. {
    20. //1.
    21. MY_FUNC *fp = NULL;
    22. fp = func;
    23. fp(10, 20);
    24. //2.
    25. MY_FUNC_P fp1 = NULL;
    26. fp1 = func;
    27. fp1(10, 20);
    28. //3.
    29. int(*fp3)(int, int) = NULL;
    30. fp3 = func;
    31. fp3(10, 20);
    32. func(10, 20);
    33. func(10, 20, 30);
    34. fp3 = func; //fp3 ---> func(int,int)
    35. //实际上在给函数指针赋值的时候,是会发生函数重载匹配的
    36. //在调用函数指针的时候,所调用的函数就已经固定了。
    37. int(*fp4)(int, int, int) = NULL;
    38. fp4 = func; //fp4 ---> func(int,int,int)
    39. fp3(10, 30);//func(int,int)
    40. fp3(10, 20);
    41. fp4(10, 30, 30);
    42. return 0;
    43. }

    6.6 总结

    7.类和对象

    7.1 类和对象的基本概念

    C和C++中struct区别

    1. c语言struct只有变量
    2. c++语言struct 既有变量,也有函数

    访问控制权限

    一个类类的内部,是默认private

    我们编写程序的目的是为了解决现实中的问题,而这些问题的构成都是由各种事物组成,我们在计算机中要解决这种问题,首先要做就是要将这个问题的参与者:事和物抽象到计算机程序中,也就是用程序语言表示现实的事物。

    1. //封装两层含义
    2. //1. 属性和行为合成一个整体
    3. //2. 访问控制,现实事物本身有些属性和行为是不对外开放
    4. class Person{
    5. //人具有的行为(函数)
    6. public:
    7. void Dese(){ cout << "我有钱,年轻,个子又高,就爱嘚瑟!" << endl;}
    8. //人的属性(变量)
    9. public:
    10. int mTall; //多高,可以让外人知道
    11. protected:
    12. int mMoney; // 有多少钱,只能儿子孙子知道
    13. private:
    14. int mAge; //年龄,不想让外人知道
    15. };
    16. int main(){
    17. Person p;
    18. p.mTall = 220;
    19. //p.mMoney 保护成员外部无法访问
    20. //p.mAge 私有成员外部无法访问
    21. p.Dese();
    22. return EXIT_SUCCESS;
    23. }

    sruct和class的区别

    1. class A{
    2. int mAge;
    3. };
    4. struct B{
    5. int mAge;
    6. };
    7. void test(){
    8. A a;
    9. B b;
    10. //a.mAge; //无法访问私有成员
    11. b.mAge; //可正常外部访问
    12. }
    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. struct Hero
    5. {
    6. char name[64];
    7. int sex;
    8. };
    9. void printHero(struct Hero& h)
    10. {
    11. cout << "Hero" << endl;
    12. cout << "name = " << h.name << endl;
    13. cout << "sex = " << h.sex << endl;
    14. }
    15. class AdvHero
    16. {
    17. public://访问控制权限
    18. char name[64];
    19. int sex;
    20. void printHero()
    21. {
    22. cout << "advHero" << endl;
    23. cout << "name = " << name << endl;
    24. cout << "sex = " << sex << endl;
    25. }
    26. };
    27. class Animal
    28. {
    29. //{}以内 叫类的内部, 以外叫类的外部
    30. public:
    31. char kind[64];
    32. char color[64];
    33. //在public下面定义成员变量和函数 是能够在类的内部和外部都可以访问的。
    34. void printAnimal()
    35. {
    36. cout << "kind = " << kind << endl;
    37. cout << "color = " << color << endl;
    38. }
    39. void write()
    40. {
    41. cout << kind << "开始鞋子了" << endl;
    42. }
    43. void run()
    44. {
    45. cout << kind << "跑起来了" << endl;
    46. }
    47. //
    48. private:
    49. //在private下面定义的成员变量和方法只能够在类的内部访问
    50. };
    51. int main(void)
    52. {
    53. Hero h;
    54. strcpy(h.name, "gailun");
    55. h.sex = 1;
    56. printHero(h);
    57. AdvHero advH;
    58. strcpy(advH.name, "ChunBro");
    59. advH.sex = 1;
    60. advH.printHero();
    61. cout << "-----------" << endl;
    62. Animal dog;
    63. strcpy(dog.kind, "dog");
    64. strcpy(dog.color, "yellow");
    65. Animal sheep;
    66. strcpy(sheep.kind, "sheep");
    67. strcpy(sheep.color, "white");
    68. dog.write();
    69. sheep.run();
    70. return 0;
    71. }

    将成员变量设置为private

    1. class AccessLevels{
    2. public:
    3. //对只读属性进行只读访问
    4. int getReadOnly(){ return readOnly; }
    5. //对读写属性进行读写访问
    6. void setReadWrite(int val){ readWrite = val; }
    7. int getReadWrite(){ return readWrite; }
    8. //对只写属性进行只写访问
    9. void setWriteOnly(int val){ writeOnly = val; }
    10. private:
    11. int readOnly; //对外只读访问
    12. int noAccess; //外部不可访问
    13. int readWrite; //读写访问
    14. int writeOnly; //只写访问
    15. };

    7.2 用class去封装带行为的类

    class 封装的本质,在于将数据和行为,绑定在一起然后能过对象来完成操 作。

    1. #include
    2. using namespace std;
    3. class Date
    4. {
    5. public:
    6. void init(Date & d);
    7. void print(Date& d);
    8. bool isLeapYear(Date& d);
    9. private:
    10. int year;
    11. int month;
    12. int day;
    13. };
    14. void Date::init(Date& d)
    15. {
    16. cout << "year,month,day:" << endl;
    17. cin >> d.year >> d.month >> d.day;
    18. }
    19. void Date::print(Date& d)
    20. {
    21. cout << "year month day" << endl;
    22. cout << d.year << ":" << d.month << ":" << d.day << endl;
    23. }
    24. bool Date::isLeapYear(Date& d)
    25. {
    26. if ((d.year % 4 == 0 && d.year % 100 != 0) || d.year % 400 == 0)
    27. return true;
    28. else
    29. return false;
    30. }
    31. int main()
    32. {
    33. Date d;
    34. d.init(d);
    35. d.print(d);
    36. if (d.isLeapYear(d))
    37. cout << "leap year" << endl;
    38. else
    39. cout << "not leap year" << endl;
    40. return 0;
    41. }

    7.3 类的封装

    一般类中的属性是private

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. struct Date
    5. {
    6. int year;
    7. int month;
    8. int day;
    9. };
    10. void init_date(struct Date & d)
    11. {
    12. cout << "year, month, day" << endl;
    13. cin >> d.year;
    14. cin >> d.month;
    15. cin >> d.day;
    16. }
    17. //打印data的接口
    18. void print_date(struct Date &d)
    19. {
    20. cout << d.year << "年" << d.month << "月" << d.day << "日" << endl;
    21. }
    22. bool is_leap_year(struct Date &d)
    23. {
    24. if (((d.year % 4 == 0) && (d.year % 100 != 0)) || (d.year % 400 == 0)) {
    25. return true;
    26. }
    27. return false;
    28. }
    29. class MyDate
    30. {
    31. public:
    32. //成员方法 成员函数
    33. void init_date()
    34. {
    35. cout << "year, month, day" << endl;
    36. cin >> year;
    37. cin >> month;
    38. cin >> day;
    39. }
    40. //打印data的接口
    41. void print_date()
    42. {
    43. cout << year << "年" << month << "月" << day << "日" << endl;
    44. }
    45. bool is_leap_year()
    46. {
    47. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
    48. return true;
    49. }
    50. return false;
    51. }
    52. int get_year()
    53. {
    54. return year;
    55. }
    56. void set_year(int new_year)
    57. {
    58. year = new_year;
    59. }
    60. protected://保护控制权限。在类的继承中跟private有区别,在单个类中,跟private是一抹一样。
    61. private:
    62. int year;
    63. int month;
    64. int day;
    65. };
    66. //一个类类的内部,默认的访问控制权限是private
    67. class Hero
    68. {
    69. int year;
    70. };
    71. //一个结构体默认的访问控制权限的是public
    72. struct Hero2
    73. {
    74. int year;
    75. void print()
    76. {
    77. }
    78. };
    79. int main(void)
    80. {
    81. #if 0
    82. Date d1;
    83. init_date(d1);
    84. print_date(d1);
    85. if (is_leap_year(d1) == true) {
    86. cout << "是闰年 " << endl;
    87. }
    88. else {
    89. cout << "不是闰年 " << endl;
    90. }
    91. #endif
    92. MyDate my_date;
    93. my_date.init_date();
    94. my_date.print_date();
    95. if (my_date.is_leap_year() == true)
    96. {
    97. cout << "是闰年 " << endl;
    98. }
    99. else {
    100. cout << "不是闰年 " << endl;
    101. }
    102. //getter,setter
    103. cout << my_date.get_year() << endl;
    104. my_date.set_year(2000);
    105. cout << my_date.get_year() << endl;
    106. Hero h;
    107. //h.year = 1000;
    108. Hero2 h2;
    109. h2.year = 100;
    110. return 0;
    111. }

    7.4 ⾯向对象编程

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. //面向对象
    5. class Dog
    6. {
    7. public:
    8. void eat(char *food)
    9. {
    10. cout << name << "吃" << food << endl;
    11. }
    12. char name[64];
    13. };
    14. //面向过程
    15. void eat(class Dog &dog, char *food)
    16. {
    17. cout << dog.name << "吃" << food << endl;
    18. }
    19. int main(void)
    20. {
    21. Dog dog;
    22. strcpy(dog.name, "狗");
    23. eat(dog, "翔");
    24. dog.eat("翔");
    25. return 0;
    26. }

     求圆的面积和周长

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. //圆的周长
    5. double getCircleGirth(double r)
    6. {
    7. return 2 * 3.14 * r;
    8. }
    9. //源的面积
    10. double getCircleArea(double r)
    11. {
    12. return 3.14 * r * r;
    13. }
    14. //用面向对象实现
    15. //圆类
    16. class Circle
    17. {
    18. public:
    19. void setR(double r)
    20. {
    21. m_r = r;
    22. }
    23. double getR()
    24. {
    25. return m_r;
    26. }
    27. double getGirth()
    28. {
    29. return 2 * 3.14 * m_r;
    30. }
    31. double getArea()
    32. {
    33. return m_r * m_r * 3.14;
    34. }
    35. private:
    36. double m_r; //圆的私有成员 半径
    37. };
    38. class Circle2
    39. {
    40. public:
    41. void setR(double r)
    42. {
    43. m_r = r;
    44. }
    45. double getR()
    46. {
    47. return m_r;
    48. }
    49. double getArea()
    50. {
    51. m_area = m_r * m_r * 3.14;
    52. return m_area;
    53. }
    54. double getGirth()
    55. {
    56. m_girth = m_r * 2 * 3.14;
    57. return m_girth;
    58. }
    59. private:
    60. double m_r;
    61. double m_girth; //周长
    62. double m_area;//面积
    63. };
    64. int main(void)
    65. {
    66. double r = 10; //圆的半径
    67. double g = 0;
    68. double a = 0;
    69. g = getCircleGirth(r);
    70. a = getCircleArea(r);
    71. cout << "圆的半径是" << r << endl;
    72. cout << "圆的周长是" << g << endl;
    73. cout << "圆的面积是" << a << endl;
    74. cout << "------" << endl;
    75. Circle c;
    76. c.setR(10);
    77. cout << "圆的半径是" << c.getR() << endl;
    78. cout << "圆的周长是" << c.getGirth() << endl;
    79. cout << "圆的面积是" << c.getArea() << endl;
    80. cout << "------------" << endl;
    81. Circle2 c2;
    82. c2.setR(10);
    83. cout << "圆的半径是" << c2.getR() << endl;
    84. cout << "圆的周长是" << c2.getGirth() << endl;
    85. cout << "圆的面积是" << c2.getArea() << endl;
    86. return 0;
    87. }

    圆的周长和面积多文件编写

    Circle.h

    1. #pragma once
    2. #if 0
    3. #ifndef __CIRCLE_H_
    4. #define __CIRCLE_H_
    5. #endif
    6. #endif
    7. class Circle
    8. {
    9. public:
    10. //设置半径:
    11. void setR(double r);
    12. //得到半径
    13. double getR();
    14. double getArea();
    15. double getGirth();
    16. private:
    17. double m_r;
    18. double m_area;
    19. double m_girth;
    20. };

    Circle.cpp

    1. #include "Circle.h"
    2. //Circle::表示还在圆的内部
    3. void Circle::setR(double r)
    4. {
    5. m_r = r;
    6. }
    7. double Circle::getR()
    8. {
    9. return m_r;
    10. }
    11. double Circle::getArea()
    12. {
    13. m_area = m_r *m_r *3.14;
    14. return m_area;
    15. }
    16. double Circle::getGirth()
    17. {
    18. m_girth = m_r * 2 * 3.14;
    19. return m_girth;
    20. }

    mian.cpp

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. #include "Circle.h"
    4. using namespace std;
    5. int main(void)
    6. {
    7. Circle c;
    8. c.setR(10);
    9. cout << "面积" << c.getArea() << endl;
    10. return 0;
    11. }

    判断两个立方体是否相等

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. //立方体类
    5. class Cube
    6. {
    7. public:
    8. void setABC(int a, int b, int c)
    9. {
    10. m_a = a;
    11. m_b = b;
    12. m_c = c;
    13. }
    14. int getArea()
    15. {
    16. return (m_a*m_b) * 2 + (m_a*m_c) * 2 + (m_b*m_c) * 2;
    17. }
    18. int getVolume()
    19. {
    20. return (m_a*m_b*m_c);
    21. }
    22. int getA()
    23. {
    24. return m_a;
    25. }
    26. int getB()
    27. {
    28. return m_b;
    29. }
    30. int getC()
    31. {
    32. return m_c;
    33. }
    34. //同类之间无私处
    35. bool judgeCube(Cube &another)
    36. {
    37. if (m_a == another.m_a &&
    38. m_b == another.getB() &&
    39. m_c == another.getC()) {
    40. return true;
    41. }
    42. else {
    43. return false;
    44. }
    45. }
    46. private:
    47. int m_a;
    48. int m_b;
    49. int m_c;
    50. };
    51. //全局函数
    52. bool judgeCube(Cube &c1, Cube &c2)
    53. {
    54. if (c1.getA() == c2.getA() &&
    55. c1.getB() == c2.getB() &&
    56. c1.getC() == c2.getC()) {
    57. return true;
    58. }
    59. else {
    60. return false;
    61. }
    62. }
    63. int main(void)
    64. {
    65. Cube c1;
    66. c1.setABC(10, 20, 30);
    67. Cube c2;
    68. c2.setABC(10, 20, 30);
    69. cout << "c1 的体积是" << c1.getVolume() << endl;
    70. cout << "c1 的面积是" << c1.getArea() << endl;
    71. if (judgeCube(c1, c2) == true) {
    72. cout << "相等" << endl;
    73. }
    74. else {
    75. cout << "不相等" << endl;
    76. }
    77. cout << " ------ " << endl;
    78. if (c1.judgeCube(c2) == true) {
    79. cout << "相等" << endl;
    80. }
    81. else {
    82. cout << "不相等" << endl;
    83. }
    84. return 0;
    85. }

    判断点是否在圆的内部

    1. #define _CRT_SECURE_NO_WARNINGS
    2. #include
    3. using namespace std;
    4. //点类
    5. class Point
    6. {
    7. public:
    8. void setXY(int x, int y)
    9. {
    10. m_x = x;
    11. m_y = y;
    12. }
    13. int getX()
    14. {
    15. return m_x;
    16. }
    17. int getY()
    18. {
    19. return m_y;
    20. }
    21. private:
    22. int m_x;
    23. int m_y;
    24. };
    25. //圆类
    26. class Circle
    27. {
    28. public:
    29. void setXY(int x, int y)
    30. {
    31. x0 = x;
    32. y0 = y;
    33. }
    34. void setR(int r)
    35. {
    36. m_r = r;
    37. }
    38. //提供一个判断点是否在圆内
    39. //true 在内部
    40. //false 在外部
    41. bool judgePoint(Point &p)
    42. {
    43. int dd;
    44. dd = (p.getX() - x0)*(p.getX() - x0) + (p.getY() - y0)*(p.getY() - y0);
    45. if (dd > m_r*m_r) {
    46. return false;
    47. }
    48. else {
    49. return true;
    50. }
    51. }
    52. private:
    53. int x0;
    54. int y0;
    55. int m_r;
    56. };
    57. int main(void)
    58. {
    59. Circle c;
    60. c.setXY(2, 2);
    61. c.setR(4);
    62. Point p;
    63. p.setXY(8, 8);
    64. if (c.judgePoint(p) == true) {
    65. cout << "圆的内部" << endl;
    66. }
    67. else {
    68. cout << "圆的外部" << endl;
    69. }
    70. return 0;
    71. }

    判断两个圆是否相交

    1. #include
    2. using namespace std;
    3. class Point {
    4. public:
    5. void setXY(int x, int y) {
    6. m_x = x;
    7. m_y = y;
    8. }
    9. //计算两个点之间的距离
    10. double pointDistance(Point& another) {
    11. int dis_x = m_x - another.m_x;
    12. int dis_y = m_y - another.m_y;
    13. //斜边
    14. double dis = sqrt(dis_x * dis_x + dis_y * dis_y);
    15. return dis;
    16. }
    17. private:
    18. int m_x;
    19. int m_y;
    20. };
    21. class Circle {
    22. public:
    23. void setR(int r) {
    24. m_r = r;
    25. }
    26. void setXY(int x1, int y1) {
    27. p0.setXY(x1, y1);
    28. }
    29. //判断圆是否相交
    30. bool isConnection(Circle& another) {
    31. //两个圆的半径之和
    32. int r = m_r + another.m_r;
    33. //两个圆心之间的距离
    34. double dis = p0.pointDistance(another.p0);
    35. if (dis <= r) {
    36. //相交
    37. return true;
    38. }
    39. else {
    40. return false;
    41. }
    42. }
    43. private:
    44. int m_r;
    45. Point p0;
    46. };
    47. int main(void)
    48. {
    49. Circle c1, c2;
    50. int x, y, r;
    51. cout << "请输入第一个圆的半径" << endl;
    52. cin >> r;
    53. c1.setR(r);
    54. cout << "请输入第一个圆的x" << endl;
    55. cin >> x;
    56. cout << "请输入第一个圆的y" << endl;
    57. cin >> y;
    58. c1.setXY(x, y);
    59. cout << "请输入第2个圆的半径" << endl;
    60. cin >> r;
    61. c2.setR(r);
    62. cout << "请输入第2个圆的x" << endl;
    63. cin >> x;
    64. cout << "请输入第2个圆的y" << endl;
    65. cin >> y;
    66. c2.setXY(x, y);
    67. if (c1.isConnection(c2) == true) {
    68. cout << "相交" << endl;
    69. }
    70. else {
    71. cout << "不想交" << endl;
    72. }
    73. return 0;
    74. }

  • 相关阅读:
    1064 Complete Binary Search Tree
    马来西亚农村致富经 丰收节贸促会-艾迪:跨境电商中国样本
    nova服务的基本使用
    Hive基础知识(十八):Hive 函数的使用
    Golang 中的 Context 包
    JAVA进程CPU负载过高导致问题分析
    WSL2和ubuntu的安装过程
    Jquery常用操作总结
    paddle Conv2D参数,在手撕数字识别案例中调参数;卷积神经网络的卷积核大小、个数,卷积层数如何确定呢?
    在系统识别 App 中估计传递函数模型
  • 原文地址:https://blog.csdn.net/m0_63077733/article/details/132945632