C++ 核心编程(1)

devtools/2024/9/20 9:18:46/ 标签: c++, 开发语言

c++面向对象编程

1.内存分区模型

程序运行前为代码区和全局区。程序运行后才有栈区和堆区。。

在这里插入图片描述

1.1 程序运行前

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
/*全局区全局变量、静态变量、常量 */
//全局变量
int g_1 = 20;
int g_2 = 30; //const修饰的全局变量,全局常量--也在全局区 
const int c_g_a = 10;int main() {//普通局部变量--写到 main 函数体内, int a=10;int b=10;//静态变量static int s_1 = 40;//常量//字符串常量cout<<"字符串常量地址为:"<<(int)&"hello world"<<endl; //const修饰的常量const int c_a = 49; //不在全局区中,1.局部变量  2.const修饰的局部变量(局部常量)//在全局区中,1.静态变量  2. 静态变量  3.常量(字符串常量,const修饰的常量) return 0;
}

在这里插入图片描述

1.2 程序运行后

栈区:编译器自动分配和释放,存放函数参数值,局部变量等。
注意:不要返回局部变量地址,栈区开辟的数据由编译器自动释放

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
/* 栈区 --栈区数据由编译器管理开辟和释放 不要返回局部变量地址 */int *fu(){int a = 10; //局部数据,存放在栈区, return &a;
}
int main() {//接收返回值 int *p = fu();//第一次可以打印正确数据,因为编译器做了一次保留 cout<<*p<<endl;//第二次乱码 cout<<*p<<endl;return 0;
}

堆区:程序员分配释放,若程序员不释放,程序结束时由操作系统回收。
在C++中主要利用new在堆区开辟内存

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
/* 堆区 --堆区数据由程序员管理开辟和释放 */int *fu(){int *a = new int (10); //利用new 将数据开辟到堆区 return a;
}int main() {//接收返回值 int *p = fu();//输出10 cout<<*p<<endl;//输出10cout<<*p<<endl;return 0;
}

1.3 new操作符

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//1.new 基本用法
int *f(){//堆区创建整型数据//new 返回的是 该数据类型的指针int *p = new int(10);return p; 
} 
void test01(){int *p = f();cout<<*p<<endl;cout<<*p<<endl;//释放堆区数据 -使用delete delete p;//已释放,再次访问就是非法操作 //cout<<*p<<endl;
}
//堆区利用new开辟数组
void test02(){//创建10整型数据的数组,在堆区int *arr = new int[10];for(int i=0;i<10;i++){arr[i] = i+100;cout<<arr[i]<<endl;} //释放堆区数组--释放数组需要加 [] delete[] arr; 
} int main() {return 0;
}

2.引用

2.1 引用的基本使用

作用:给变量起别名
语法:数据类型 &别名 = 原名

2.2 引用注意事项

1.引用必须初始化
2.引用初始化后不可改变

#include<iostream>
#include <bits/stdc++.h>
using namespace std;int main() {int a = 10;int &b = a;int c = 20;b = c; //赋值操作,不是更改引用 return 0;
}

2.3 引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//值传递
void swap1(int a,int b); 
//地址传递
void swap2(int *a,int *b);{int t = *a;*a = *b;*b = t;
}
//引用传递 
void swap3(int &a,int &b);{int t = a;a = b;b = t;
}
int main() {int a=1,b=3;swap1(a,b); //值传递swap2(&a,&b); //地址传递swap3(a,b); //引用传递 return 0;
}

2.4 引用做函数返回值

作用:引用可以作为函数返回值
注:不要返回局部变量引用

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//引用做函数返回值
int& test01(){int a = 1; //局部变量存放栈区 return a;
} 
//函数调用可以作为左值
int& test02(){static int a = 10; //静态变量存放全局区,全局区数据程序结束后释放 return &a;
} 
int main() {int &ref = test01();cout<<ref<<endl;  //第一次打印正确,因为编译器做了保留 //cout<<ref<<endl;	//再次打印乱码int &ref1 = test02();cout<<ref1<<endl;  //多次打印输出都正确,输出1 cout<<ref1<<endl;//函数做左值,若函数返回值是引用,那么函数可作为左值test02() = 1000;cout<<ref1<<endl;  //输出1000 cout<<ref1<<endl; return 0;
}

2.5 引用的本质

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

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//发现是引用,转换为 int *const ref = &a; 
void fc(int & ref){ref  = 1000; //ref是引用,转换为 *ref = 1000; 
}
int main() {int a = 10;//自动转换为 int *const ref = &a; 指针常量:指针指向不可改,也说明为什么引用不可更改int &ref = 0;ref = 20; // 内部发现ref是引用,自动转换为 *ref = 20;cout<<a<<endl;cout<<ref<<endl;fc(a); return 0;
}

2.6 常量引用

作用:修饰形参,防止误操作
函数形参中可以加const修饰形参,防止形参更改实参

#include<iostream>
#include <bits/stdc++.h>
using namespace std;void test01(const int &a){//不可修改 //a = 1000;cout<<a<<endl;
} int main() {/*int a = 20;int &ref = 10; //错误,引用必须引一块合法的内存空间//加上const之后,编译器将代码修改 int temp = 10;  const int &ref = temp;const int& ref = 10; //正确,编译器优化代码,//加入const后变为只读,不可修改 //ref = 20;*/int a = 100;test01(a);system("pause");return 0;
}

3.函数提高

3.1 函数默认参数

c++中,函数的形参列表中的形参是可以有默认值的
语法:返回值类型 函数名 (参数=默认值) {}

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//函数默认参数 
//若某个位置有了默认参数,那么后续的形参都要有默认参数,如b=10,则c必须要有值
int fc(int a,int b = 10,int c = 20){return a+b+c;
}
//声明和实现只能有一个有默认参数 ,否则会出现二义性问题 
int fc2(int a=10,int b=10);
int fc2(int a,int b){return a+b;
} 
int main() {//传入b=20,则使用20 cout<<fc(10,20)<<endl;//未传入b的值,使用10 cout<<fc(10)<<endl;system("pause");return 0;
}

3.2 函数占位参数

C++函数形参可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:返回值类型 函数名(数据类型){},如 int f(int a,int);

int fc(int a,int);
int fc1(int a,int = 23); 

3.3 函数重载

3.3.1 函数重载概述

作用:函数名可以相同,提高复用性
满足条件:
1.同一作用域
2.函数名称相同
3.函数参数类型不同 或者 个数不同 或者 顺序不同
注:函数返回值不可以作为函数重载的条件

//均是重载函数 函数重载需要函数都在同一个作用域下
void f1(int a);
void f1(); 
void f1(int a,int b);
void f1(int);
void f1(int a,double b);
void f1(double a,int b); 
//非函数重载----函数返回值不可以作为函数重载的条件
int f2(int a);
double f2(int a);

3.3.2 函数重载注意事项

1.引用作为函数重载条件
2.函数重载碰到函数默认参数

#include<iostream>
#include <bits/stdc++.h>
using namespace std;//函数重载注意事项 
//1.const可以作为函数重载条件 
int f1(int &a);   
int f1(const int &a);
//2.函数重载碰到默认参数
int f2(int a,int b=10);
int f2(int a); 
int main() {//1.const可以作为函数重载条件 int a = 10;f1(a); //调用 int f1(int &a);f1(10); //调用 int f1(const int &a); 若调用 int f1(int &a); 相当于 int &A = 10 (不合法) //2.函数重载碰到默认参数f2(10);  //既可以调用 int f2(int a,int b=10);  也可调用  int f2(int a); 出现二义性f2(1,10); //调用 int f2(int a,int b=10);return 0;
}

4 类和对象

C++面向对象三大特性:封装 继承 多态
在这里插入图片描述

4.1 封装

4.1.1 封装的意义

封装是C++面向对象三大特性之一
封装的意义:
1.将属性和行为作为一个整体,表现生活中的事物
2.将属性和行为加以权限控制

封装的意义一:设计类时候,属性和行为写在一起,表现事物。
语法:class 类名 { 访问权限: 属性 / 行为 };

1.示例:设计圆类,求周长

#include<iostream>
#include <bits/stdc++.h>
using namespace std;const double PI = 3.14;
class Circle{//访问权限//公共权限:类内类外都可访问 //类中的属性和行为 都成为成员//属性:成员属性 成员变量//行为:成员函数 成员方法 
public://属性//1.半径 int m_r; //行为 //1.获取圆周长double calZC(){return 2*PI*m_r;}//2.给半径赋值void setMR(int r){m_r = r;}
};
int main() {//1.通过圆类, 创建具体的圆(对象)//实例化,通过一个类创建一个对象的过程Circle c1;c1.m_r = 10; //或 c1.setMR(5);cout<<"周长:"<<c1.calZC()<<endl; return 0;
}

封装的意义二:类在设计时,可以把属性和行为放在不同的权限下,加以控制
三种权限:
1.public : 共有
2.protected:保护权限
3.private:私有权限

#include<iostream>
#include <bits/stdc++.h>
using namespace std;const double PI = 3.14;
class Circle{
//1.public : 公共权限  成员 类内可以访问  类外可以访问
public:string name;
//2.protected:保护权限  成员 类内可以访问  类外不可以访问  儿子可访问父亲保护内容 
proteced:string car;
//3.private:私有权限  成员 类内可以访问  类外不可以访问   儿子不可访问父亲私有内容 
private:string password;public:void func(){name = "123";car = "345";password = "2452432";} 
};
int main() {Circle c1;c1.name = "王";//car 类外不可访问 c1.car = "米su7"; //password 类外不可访问 c1.password = "1234"; return 0;
}

4.1.2 struct和class区别

C++中struct和class唯一区别在于默认的访问权限不同
区别:struct默认权限公共,class默认权限为私有

4.1.3 成员属性设置为私有

优点1:将所有成员属性设置为私有,可以自己控制读写权限
优点2:对于写权限,可以检测数据的有效性

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//成员属性设置为私有//1.可以自己控制读写权限//2.对于写可以检测数据有效性 
class Circle{
public://通过公有方法对私有属性读写void setName(string name){m_name = name;} string getName(){return m_name;}void setAge(int age){if(age>0){m_age = age;}}int getAge(){return m_age;}void setIdo(int idol){m_idol = idol;}
private:	string m_name;  ///可读可写 int m_age = 18;	//只读  也可以写 int m_idol; //只写 
};int main() {Circle c1;c1.setName("张");cout<<c1.getName()<<endl;cout<<c1.getAge()<<endl;//读不到 cout<<c1.m_idol<<endl;return 0;
}

案例2 点和圆的关系

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//点和圆关系 
class Point{public:void setX(int x){m_X = x;}int getX(){return m_X;}void setY(int y){m_Y = y;}int getY(){return m_Y;}private:int m_X,m_Y;
}; 
class Circle{
public:void setR(int r){m_R = r;}int getR(){return m_R;}void setCenter(Point p){m_Center = p;}Point getCenter(){return m_Center;}
private:	int m_R;Point m_Center;
};
//判断点和圆关系
void isInCircle(Circle &c,Point &p){//计算两点距离平方int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());//计算半径平方int rDis = c.getR() * c.getR();if(rDis == distance){cout<<"点在圆上"<<endl; }else if(rDis > distance){cout<<"点在圆外"<<endl; }else{cout<<"点在圆内"<<endl; } 
} 
int main() {//圆 Circle c1;//半径 c1.setR(10);//设置圆心 Point p; p.setX(10);p.setY(0);c1.setCenter(p);//创建点Point p1;p1.setX(10);p1.setY(10); isInCircle(c1,p1);return 0;
}
//正常实现需要分离为Point.h Point.cpp Circle.h Circle.cpp main.cpp
Point.h:
class Point{public:void setX(int x);int getX();void setY(int y);int getY();private:int m_X,m_Y;
}; 
Point.cpp:#include <Point.h>//需要指明所属作用域void Point::setX(int x){m_X = x;}int Point::getX(){return m_X;}void Point::setY(int y){m_Y = y;}int Point::getY(){return m_Y;}
Circle.h 和 Circle.cpp类似

4.2 对象的初始化和清理

4.2.1 构造函数和析构函数

在这里插入图片描述
在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
class Circle{
public://1.构造函数,//无返回值;不用写void; 函数名与类名相同; 构造函数可有参数; 可重载;创建对象时候构造函数会自动调用,只调用一次Circle(){cout<<"构造"<<endl; } //2.析构函数//无返回值;不写void;函数名同类目 在名称前加~ ; 析构不可有参数;不可重载; 对象销毁前会自动调用析构函数,只调用一次~Circle(){cout<<"析构"<<endl; } 
};
void test01(){Circle c;
}
int main() {//调用构造函数 和 析构 //因为test01中的Circle c是局部变量,是栈上的数据,执行完会释放对象 test01();//加上 system("pause"); 则系统会中断不停止,不会执行析构,不加还是会执行析构。点击按任意键继续后会执行析构 Circle c1;system("pause"); return 0;
}    

4.2.2 构造函数的分类及调用

两种分类方式:
1.按参数分为:有参构造和无参构造
2.按类型分为:普通构造和拷贝构造
三种调用方式:
1.括号法
2.显示法
3.隐式转换法

#include<iostream>
#include <bits/stdc++.h>
using namespace std;class Circle{
public://普通构造 //无参和有参 Circle(){cout<<"构造"<<endl; } Circle(int a){cout<<"有参构造"<<endl; }//拷贝构造Circle(const Circle &c){cout<<"拷贝构造"<<endl; age = c.age;} ~Circle(){}int age;};
void test01(){//1.括号法Circle c; //默认构造调用Circle c1(10); //有参构造调用Circle c2(c1);	//拷贝构造函数 //调用默认构造函数时候,不要加() ,此时执行则什么都不输出//因为下边代码,编译器会认为是一个函数的声明 Cirlce c3(); //2.显示法Circle c1;Cirlce c2 = Circle(10);Cirlce c3 = Circle(c2); //1.匿名对象, 特点:当前行执行结束后,系统会立即回收匿名对象。先执行构造再执行析构Circle(10); //2.不要利用拷贝构造函数初始化匿名对象。此时编译器会认为 Circle(c3) = Circle c3; 两个c3,名称重复 Circle(c3);//3.隐式转换法 //相当于 Cirlce c4 = Circle(10); Cirlce c4 = 10;	//有参构造 Cirlce c5 = c4;	//拷贝构造 
}int main() {return 0;
}

4.2.3 拷贝构造函数的调用时机

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//拷贝构造调用时机 
class Circle{
public:Circle(){cout<<"构造"<<endl; } Circle(int a){age = a;cout<<"有参构造"<<endl; }//拷贝构造Circle(const Circle &c){cout<<"拷贝构造"<<endl; age = c.age;} ~Circle(){}int age;};
//1.使用一个已经创建完毕的对象初始化一个新对象 
void test01(){Circle c1(20);Circle c2(c1);
}
//2.值传递方式 给函数参数传值
void doWork(Circle c){}
void test02(){Circle c1;//执行函数过程也会执行 构造函数,因为是值传递 doWork(c1);
}
//3.值方式返回局部对象 
Circle doWork2(){Circle c1;//返回过程会根据 c1 创建一个新的对象 return c1;
}
void test03(){//c和 doWork2中 Circle c1; 不是一个对象 Cirlce c = doWork2();
}
int main() {//调用两次构造 两次析构; Circle c1;一次。 doWork(c1);一次 testo3();//调用两次构造 两次析构:Circle c1;一次。return c1;一次 test03();return 0;
}

4.2.4 构造函数调用规则

在这里插入图片描述

提供有参构造,则编译器提供拷贝构造,但不提供无参构造
提供拷贝构造,则编译器有参构造和无参构造都不提供,需要手动编写

4.2.4 深拷贝与浅拷贝

浅拷贝:简单的赋值拷贝操作
深拷贝:在堆区重新申请框架,进行拷贝操作

浅拷贝
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//深拷贝和浅拷贝 
class Circle{
public:Circle(){cout<<"构造"<<endl; } Circle(int a,int height){age = a;m_H = new int(height);cout<<"有参构造"<<endl; }~Circle(){//析构代码,将堆区开辟数据做释放操作if(m_H != NULL){delete m_H;//防止野指针 m_H = NULL;}cout<<"析构"<<endl; }int age;int *m_H;
};
void test01(){//先进后出,c2的析构先被释放 Circle c1(18,160);cout<<"c1年龄:"<<c1.age<<" c1身高:"<<*c1.m_H<<endl;Circle c2(c1);cout<<"c2年龄:"<<c2.age<<" c2身高:"<<*c2.m_H<<endl;
}int main() {test01();return 0;
}

浅拷贝的问题:堆区的内存重复释放。案例中p2先执行析构释放了 *m_H。然后p1又进行释放,重复释放引发异常。

在这里插入图片描述

解决:利用深拷贝解决。重新在堆区申请一块内存,使p1中的m_H和p2中的m_H申请不同的内存。
如果有堆区开辟的数据进行浅拷贝,则需要使用拷贝构造进行深拷贝。

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//深拷贝和浅拷贝 
class Circle{
public:Circle(){cout<<"构造"<<endl; } Circle(int a,int height){age = a;m_H = new int(height);cout<<"有参构造"<<endl; }//自己实现拷贝构造函数,解决浅拷贝带来的问题Circle(const Cirlce &p){cout<<"拷贝构造函数"<<endl;age = p.age;//编译器默认执行的是 m_H = p.m_H;// 深拷贝操作m_H = new int(*p.m_H); } ~Circle(){//析构代码,将堆区开辟数据做释放操作if(m_H != NULL){delete m_H;//防止野指针 m_H = NULL;}cout<<"析构"<<endl; }int age;int *m_H;
};
void test01(){//先进后出,c2的析构先被释放 Circle c1(18,160);cout<<"c1年龄:"<<c1.age<<" c1身高:"<<*c1.m_H<<endl;Circle c2(c1);cout<<"c2年龄:"<<c2.age<<" c2身高:"<<*c2.m_H<<endl;
}
int main() {test01();return 0;
}

4.2.6 初始化列表

C++提供了初始化列表语法,用来初始化属性。
语法:构造函数():属性1(值1),属性2(值2),… {}

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//深拷贝和浅拷贝 
class Circle{
public://传统方式 /*Circle(int a,int b,int c){a_1 = a;b_1 = b;c_1 = c;}*///初始化列表初始化属性Circle(int a,int b,int c) :a_1(a),b_1(b),c_1(c){}int a_1,b_1,c_1;
};
void test01(){//Circle c(1,2,3);Circle c(1,2,3); //等价 Circle c(1,2,3);
}
int main() {test01();return 0;
}    

4.2.7 类对象作为类成员

C++类中的成员可以是另一个类的对象,我们称该成员为 对象成员

class A{}
//B中有对象A作成员,A是对象成员,此时会先执行对象成员的构造函数,即先调用A的构造函数
class B{A a;
}
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//类对象作为类成员
class Phone{
public:Phone(string pName){m_Name = pName;cout<<"Phone构造函数"<<endl; }string m_Name;
};
class Person{
public://等价 Phone m_phone = pname; Person(string name,string pname):m_name(name),m_phone(pname){cout<<"Person构造函数"<<endl; }//姓名string m_name;//手机Phone m_phone; 
}; 
//当其他类对象作为本类成员,构造时候先构造其它类对象构造函数,再调用本类构造。析构函数相反 
void test01(){Person p("张","苹果max");
}
int main() {test01();return 0;
}    

4.2.8 静态成员

静态成员即在成员变量前加 static,称为静态成员。
在这里插入图片描述

静态成员变量
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//静态成员变量 
class Person{
public://1.所有对象共享同一份数据//2.编译阶段就分配内存//3.类内声明,类外初始化操作 static int m_A; 
}; 
void test01(){Person p;//出错,因为去找m_A具体值时候找不到,因此需要初始化 cout<<p.m_A<<endl;
}
int main() {test01();return 0;
}    
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//静态成员变量 
class Person{
public://1.所有对象共享同一份数据//2.编译阶段就分配内存//3.类内声明,类外初始化操作 static int m_A; //静态成员变量也有访问权限
private:static int m_B; 
}; 
int Person::m_A = 100;
int Person::m_B = 100;void test01(){Person p;cout<<p.m_A<<endl;Person p1;p1.m_A = 200;//打印200 cout<<p.m_A<<endl;
}
void test02(){//静态成员变量,不属于某个对象上,所有对象共享同一份数据// 因此静态成员变量有两种访问方式//1.通过对象进行访问Person p; cout<<p.m_A<<endl;//2.通过类名进行访问cout<<Person::m_A<<endl;//私有作用域:无法通过这种方式访问 cout<<Person::m_B<<endl;
} 
int main() {test01();return 0;
}     
静态成员函数
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//静态成员函数
//所有对象共享同一个函数
//静态成员函数只能访问静态成员变量 
class Person{
public:static void func(){m_A = 100;//静态成员函数,不可以访问,非静态成员变量,因为无法区分到底是哪个对象的 m_B m_B = 200;cout<<"static void func 调用"<<endl;}static int m_A; int m_B;//静态成员函数的访问权限 
private:static void func2(){cout<<"static void func2 调用"<<endl;}}; 
int Person::m_A = 10;//两种访问方式 
void test01(){//1.通过对象访问Person p;p.func();//2.通过类名访问Person::func();//私有作用域下无法访问 ,类外访问不到私有静态成员函数 Person::func2();
}
int main() {test01();return 0;
}    

4.3 c++对象模型和this指针

4.3.1 成员变量和成员函数分开存储

C++中,类内的成员变量和成员函数分开存储。
只有非静态成员变量才属于类的对象上

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//成员变量和成员函数是分开存储的 
class Person{
}; 
class Person1{int m_A;
}; 
class Person2{int m_A;static int m_B;
};
class Person3{void func();
}; 
void test01(){Person p;//空对象占用的内存空间为 1 //C++会给每个空对象也分配一个字节空间,为了区分空对象占内存的位置,每个空对象也应有内存地址 cout<<"sizeof(p):"<<sizeof(p)<<endl; 
}
void test02(){Person1 p1;//输出 4 cout<<"sizeof(p1):"<<sizeof(p1)<<endl; 
}
void test03(){Person2 p2;//输出 4, 静态成员变量在对象中不占内存空间 cout<<"sizeof(p2):"<<sizeof(p2)<<endl; 
}
void test04(){Person3 p3;//输出1 cout<<"sizeof(p3):"<<sizeof(p3)<<endl; 
}
int main() {test01();	test02();	test03();	test04();return 0;
}    

4.3.2 this指针概念

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//this指针 
class Person{
public:Person(int age){//不加this会出问题,this指针指向的是被调用的成员函数 所属的对象 this->age = age;}int age;Person& PersonAddAge(Person &p){this->age += p.age;//this指向p2的指针,*this指向p2本身 return *this;}
}; 
//1.解决名称冲突 
void test01(){Person p(19);cout<<p.age<<endl;
}
//2.返回对象本身用*this 
void test02(){Person p1(10);Person p2(19);//输出 19+10+10p2.PersonAddAge(p1).PersonAddAge(p1).PersonAddAge(p1);cout<<p2.age<<endl;
}
int main() {test01();test02();return 0;
}    

4.3.3 空指针访问成员函数

C++中空指针也是可以调用成员函数的, 但是也要注意有没有用到this指针。
若用到this指针,需要加以判断保证代码健壮性。

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//空指针调用成员函数 
class Person{
public:void showClassName(){cout<<"this showClassName"<<endl;}void showAge(){// 等价于 cout<<"age= "<<this->m_age<<endl; cout<<"age= "<<m_age<<endl;}int m_age;
}; 
void test01(){Person *p = NULL;//正常调用 p->showClassName();//提示空指针 p->showAge();
}
int main() {test01();return 0;
}    

4.3.4 const修饰成员函数

在这里插入图片描述

常函数和常对象
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//空指针调用成员函数 
class Person{
public://this指针本质:是指针常量 指针的指向不可修改 指向的值可修改 void showAge1(){//可修改 m_age = 100; //this = NULL;	//this指针不可修改指针指向 }//加上 const后 this指针的指向不可修改 指向的值也不可修改 void showAge() const {//m_age = 100; this->m_b = 23;}int m_age;mutable int m_b;	//特殊变量 使其在常含数也可修改,加上关键字 mutable
}; 
//常函数 
void test01(){Person p;p.showAge(); 
}
//常对象 
void test02(){const Person p;	//在对象前加const,变为常对象//p.m_age = 20; //不可修改p.m_b = 234;	//可修改,添加了mutable的成员变量在常对象下也能修改 //常对象只能调用常含数,不能调用普通成员函数,因为普通成员函数可修改不加mutable的属性 p.showAge(); 
}
int main() {test01();test02();return 0;
}    

4.4 友元

在这里插入图片描述

友元的三种实现:
1.全局函数友元
2.类做友元
3.成员函数做友元

4.4.1 全局函数做友元

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//
class Building{//goodGay全局函数是 Building好朋友,可以访问 Building 中私有成员 friend void goodGay(Building *build); 
public:Building(){m_setRoom = "客厅";m_bedRoom = "卧室"; }
public:string m_setRoom;
private:string m_bedRoom;
};
//全局函数做友元:把函数放入类中即可 
//访问build的全局和私有属性 
void goodGay(Building *build){cout<<"goodGay正在访问:"<< build->m_setRoom<<endl;//出错,私有属性无法访问,此时将本函数全部放入building类中 即可访问 cout<<"goodGay正在访问:"<< build->m_bedRoom<<endl;
} 
void test01(){Building build;goodGay(&build);
}
void test02(){}
int main() {test01();test02();return 0;
}    

4.4.2 类做友元

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//类做友元 
class Building;
class GoodGay{public:GoodGay();void visit();	//参观函数访问Building类中的属性Building * build;
};
class Building{//加入后 GoodGay 中的函数即可访问  Building 的私有成员 friend class GoodGay;public:Building();public:string m_setRoom;private:string m_bedRoom;
};
//类外写成员函数
Building::Building(){m_setRoom = "客厅";m_bedRoom = "卧室"; 
}
GoodGay::GoodGay(){build = new Building;
}
void GoodGay::visit(){cout<<"GoodGay正在访问:"<< build->m_setRoom<<endl;//此时不可访问,此时只需要将 friend class GoodGay; 加入Building中即可访问 cout<<"GoodGay正在访问:"<< build->m_bedRoom<<endl;
}
void test01(){GoodGay gg;gg.visit();
}int main() {test01();return 0;
}    

4.3.4 成员函数做友元

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//成员函数做友元 
class Building;
class GoodGay{public:GoodGay();void visit();	//让 visit 函数可访问Building类中的私有属性void visit1();  //不可访问 Building类中的私有属性Building * build;
};
class Building{//告诉编译器 GoodGay 中的visit函数作为友元函数,可访问 Building 的私有成员 friend void GoodGay::visit(); public:Building();public:string m_setRoom;private:string m_bedRoom;
};
//类外写成员函数
Building::Building(){m_setRoom = "客厅";m_bedRoom = "卧室"; 
}
GoodGay::GoodGay(){build = new Building;
}
void GoodGay::visit(){cout<<"visit 正在访问:"<< build->m_setRoom<<endl;//此时不可访问,此时只需要将 friend void GoodGay::visit(); 加入Building中即可访问 cout<<"visit 正在访问:"<< build->m_bedRoom<<endl;
}
void GoodGay::visit1(){cout<<"visit1 正在访问:"<< build->m_setRoom<<endl;//此时不可访问//cout<<"visit1 正在访问:"<< build->m_bedRoom<<endl;
}
void test01(){GoodGay gg;gg.visit();gg.visit1(); 
}
int main() {test01();return 0;
}    

4.5 运算符重载

运算符重载概念:对已有的运算符重新进行定义,赋予另一种功能,以适应不同的数据类型。

4.5.1 加号运算符重载

作用:实现两个自定义数据类型相加的运算。

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//加号运算符重载 
class Person
{
public://1.成员函数重载 + 号
/*	Person operator+(Person &p){Person temp;temp.ma = this->ma + p.ma;temp.mb = this->mb + p.mb;} */
public:int ma;int mb;	 
};
//2.全局函数重载 + 号
Person operator+(Person &p1,Person &p2){Person temp;temp.ma = p1.ma + p1.ma;temp.mb = p2.mb + p2.mb;return temp;
} 
//运算符重载也可以发生函数重载
Person operator+(Person &p1,int a){Person temp;temp.ma = p1.ma + a;temp.mb = p1.mb + a;return temp;
} 
void test01(){Person p1;p1.ma = 1;p1.mb = 2;Person p2;p2.ma = 1;p2.mb = 2;Person p3 = p1 + p2;cout<<"p3.ma: "<<p3.ma<<" p3.mb: "<<p3.mb<<endl;Person p4 = p3 + 100;cout<<"p4.ma: "<<p4.ma<<" p4.mb: "<<p4.mb<<endl;
}
int main() {test01();return 0;
}    

总结:1.对于内置的数据类型的表达式的运算符是不可能改变的
2.不要滥用运算符重载

4.5.2 左移运算符重载

作用:可以输出自定义数据类型

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//左移运算符重载 
class Person
{friend ostream & operator<<(ostream &cout,Person &p);
public://利用成员函数重载,左移运算符. p.opeartor<<(cout) 简化版本 p << cout//不会利用成员函数重载 << 运算符,因为无法实现cout在<<左侧 //void opeartor<<(){	}Person(int a,int b){ma = a;mb = b;} 
private:int ma;int mb;	 
};
//只能利用全局函数重载 左移运算符
ostream & operator<<(ostream &cout,Person &p){		//本质 opeartor<< (cout ,p) 简化 cout<< cout<<"ma:"<<p.ma<<" mb:"<<p.mb;return cout;
} 
//未返回cout,不能追究endl 
/*void operator<<(ostream &cout,Person &p){		//本质 opeartor<< (cout ,p) 简化 cout<< cout<<"ma:"<<p.ma<<" mb:"<<p.mb<<endl;
} */
// 输出对象p,直接输出其属性 
void test01(){Person p1(10,10);//此时无法直接输出,需要重载 << 符号。利用全局函数重载 //cout<<p;//利用全局函数重载 << 后追加endl后就无法输出,因为重载的左移运算符返回的不是 cout 。将重载返回cout后即可使用 cout<<p1<<endl; 
}
int main() {test01();return 0;
}    

4.5.3 递增运算符重载

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//递增运算符重载 
class MyInteger{friend ostream & operator<<(ostream &cout,MyInteger &p);
public:MyInteger(){m_num = 0;}//1.重载前置++运算符。返回引用是为了一直对一个数据递增。返回引用。 //不加引用的话,MyInteger mint; ++(++mint);执行后,默认的m_num是1,不是2; MyInteger& operator++(){//先进行++,再返回自身 m_num++;return *this;	}//2.重载后置++运算符。返回值。因为中间使用了临时变量,若返回其引用,后续调用就是非法操作 //int 代表占位参数,可以用于区分前置和后置 MyInteger operator++(int){//先 记录当时结果MyInteger temp = *this;//后 递增m_num++;//返回未+1前的记录结果 return temp; }
private:int m_num;
};
//只能利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,MyInteger &p){		//本质 opeartor<< (cout ,p) 简化 cout<< cout<<"m_num:"<<p.m_num;return cout;
} 
//测试重载前置运算符 
void test01(){MyInteger mint;cout<<++(++mint)<<endl;
}
//测试重载后置运算符 
void test02(){MyInteger mint1;mint1++;cout<< mint1 <<endl;
}
int main() {test01();test02();return 0;
}    

4.5.4 赋值运算符重载

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//赋值运算符重载 
class Person{public:Person(int age){mage = new int(age);//创建变量到堆区 }~Person(){if(mage!=NULL){delete mage;mage = NULL;}}//重载赋值运算符Person& operator=(Person &p){//编译器是提供浅拷贝,但是对于堆区的数据存在问题 //mage = p.mage; //应该先判断是否有属性在堆区,若有则先释放干净,然后再深拷贝if(mage != NULL){delete mage;mage = NULL;}//深拷贝mage = new int(*p.mage); //返回对象本身return *this;} int *mage;
};
void test01(){Person p1(10);Person p2(16);Person p3(20);p3 = p2 = p1;	//赋值操作,写析构函数中的 if(mage!=NULL){	delete mage;mage = NULL;} 后会崩。没写可以输出。 cout<<"p1的年龄为:"<<*p1.mage<<endl;cout<<"p2的年龄为:"<<*p2.mage<<endl;cout<<"p3的年龄为:"<<*p3.mage<<endl;
}
int main() {test01();return 0;
}    

4.5.5 关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//关系运算符重载 
class Person{
public:Person(string name,int age){mname = name;mage = age; }//重载==bool operator==(Person &p){if(this->mname == p.mname && this->mage == p.mage){return true;}return false;} //重载!=bool operator!=(Person &p){if(this->mname != p.mname || this->mage != p.mage){return false;}return true;} 	int mage;string mname; 
};
void test01(){Person p1("tom",10);Person p2("tom",10);if(p1 == p2){cout<<"相等"<<endl; }else cout<<"不相等"<<endl; 
}
int main() {test01();return 0;
}    

4.5.6 函数调用运算符重载

在这里插入图片描述

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//函数调用运算符重载 
class MyPrint{
public://重载()void operator()(string test){cout<<test<<endl;} int mage;string mname; 
};
void MyPrint2(string t){cout<<t<<endl;
}
void test01(){MyPrint mp;mp("hello");	//因为使用起来非常类似于函数调用,因此称为仿函数MyPrint2("hello1"); //同上 
}
int main() {test01();return 0;
}    
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//函数调用运算符重载--仿函数 
class MyAdd{
public:int operator()(int n1,int n2){return n1+n2;}
};
void test02(){MyAdd ad;int ret = ad(2,5); //仿函数 cout<<ret<<endl;//匿名函数对象。匿名对象(类名+()) cout<< MyAdd()(100,10)<<endl;
}
int main() {test02();return 0;
}    

4.6 继承

继承是面向对象三大特性之一。

在这里插入图片描述

4.6.1 继承的用法

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//公共页面 
class BasePage{
public:void header(){cout<<"头部信息"<<endl; }void footer(){cout<<"底部信息"<<endl; }void left(){cout<<"左侧信息"<<endl; }
}; 
//java页面 继承 公共页面
//语法: class 子类 : 继承方式 父类
//子类 也称 派生类
//父类 也称 基类
class Java:public BasePage{
public:void context(){cout<<"java内容"<<endl; }
}; 
//Python页面 继承 公共页面 
class Py:public BasePage{
public:void context(){cout<<"Python内容"<<endl; }
};void test01(){Java ja;ja.header();ja.footer();ja.left();ja.context();Py p;p.header();p.footer();p.left();p.context();
}
int main() {test01();return 0;
}    

继承好处:减少代码量
在这里插入图片描述

4.6.2 继承方式

继承语法:class 子类:继承方式 父类
继承方式一共有三种:
1.公共继承:
2.保护继承:
3.私有继承:
在这里插入图片描述
1.公有数据:类外可访问
2.保护数据:类外不可访问
3.私有数据:类外不可访问

4.6.3 继承中的对象模型

问题:从父类继承过来的成员,哪些属于子类对象中?

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//继承中的对象模型 
class Base{
public:int ma;
protected:int mb;
private:int mc;
}; 
class Son : public Base{
public:int md;
};//利用开发人员命令提示工具查看对象模型,
//https://www.bilibili.com/video/BV1et411b73Z?p=136&vd_source=fa51e45d3ee148477e96ca9b174edea6  p136 14:55处
//跳转盘符 F:
//跳转文件路径 cd 具体路径下
//查看命名
// c1 /d1 reportSingleClassLayout类名 文件名 
void test01(){// 输出 16。即父类中所有非静态成员属性都会被子类程序继承下去// 父类中私有成员属性,是被编译器给隐藏了,因此访问不到,但是确实被继承下去了 cout<<"size of Son = "<<sizeof(Son)<<endl;
}
int main() {test01();return 0;
}    

4.6.4 继承中构造和析构顺序

子类继承父类后,当创建子类对象,也会调用父类的构造函数
问题:父类和子类的构造和析构顺序是谁先谁后?

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//继承中构造和析构的顺序 
class Base{
public:Base(){cout<<"Base构造函数!"<<endl; }~Base(){cout<<"Base析构函数!"<<endl; }
}; 
class Son : public Base{
public:Son(){cout<<"Son构造函数!"<<endl; }~Son(){cout<<"Son析构函数!"<<endl; }
};
void test01(){//Base b; //先创建父类在创建子类。即先父类构造,再有子类构造;然后是父类析构,子类析构(析构顺序与构造相反) Son s;
}
int main() {test01();return 0;
}    

4.6.5 继承同名成员处理方式

问题:当子类与父类出现同名的成员,如何通过子类对象,访问到子类或父类中同名的数据?
1.访问子类同名成员,直接访问
2.访问父类同名成员 需要加作用域

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//继承中构造和析构的顺序 
class Base{
public:Base(){ma = 10;}int ma; void func(){cout<<"Base-func调用"<<endl; }void func(int a){cout<<"Son-func(int a)调用"<<endl; }
}; 
class Son : public Base{
public:Son(){ma = 20;}int ma;void func(){cout<<"Son-func调用"<<endl; }};
//同名属性处理方式 
void test01(){Son s;//访问子类自身的ma,直接访问 cout<<"Son ma:"<<s.ma<<endl;//访问父类的同名 ma,加作用域cout<<"Base ma:"<<s.Base::ma<<endl;
}
void test02(){Son s;s.func();	//直接调用,调用的是子类的同名成员//调用父类中同名的成员函数s.Base::func(); //若子类中出现和父类中同名的 成员函数,子类的同名成员会隐藏吊父类所有同名成员函数 //如果想访问父类中被隐藏的同名成员函数,需要加作用域s.Base::func(100);
}
int main() {test01();test02();return 0;
}    

在这里插入图片描述

4.6.6 继承同名静态成员处理方式

问题:继承中通吗的静态成员函数在子类对象上如何进行访问?
静态成员和非静态成员出现同名,处理方式一样。
1.访问子类同名成员 直接访问即可
2.访问父类同名成员 需要加作用域

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//继承中的同名静态成员处理方式 
class Base{
public:static int ma;static void func(){cout<<"Base static void func()"<<endl;} 
}; 
int Base::ma = 100;
class Son : public Base{
public:static int ma;static void func(){cout<<"Son static void func()"<<endl;} static void func(int a){cout<<"Son static void func(int a)"<<endl;} 
};
int Son::ma = 200;
//1.静态同名属性
void test01(){//1.通过对象访问静态数据 Son s;cout<<"Son ma:"<<s.ma<<endl;cout<<"Base ma:"<<s.Base::ma<<endl;//2.通过类名访问cout<<"通过类名访问:"<<endl; cout<<"Son 下ma:"<<Son::ma<<endl;//第一个 ::代表通过类名方式访问    第二个::代表访问父类作用域下 cout<<"Base 下ma:"<<Son::Base::ma<<endl;
}
//2。同名静态函数成员 
void test02(){//1.通过对象访问 Son s;s.func();s.Base::func();//2.通过类名访问Son::func(); Son::Base::func();//子类和父类出现同名函数,子类会将父类所有同名静态成员函数,也会隐藏掉父类所有童名成员函数//若要访问父类被隐藏同名函数,需要加作用域Son::Base::func(100); 
}
int main() {test01();test02();return 0;
}    

4.6.7 多继承语法

C++允许一个类继承多个类。
语法:class 子类 : 继承方式 父类1,继承方式 父类2 …

多几次可能会引发父类中同名成员出现,需要加作用域区分。
C++实际开发中不建议使用多继承。

#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//多继承语法 
class Base1{
public:Base1(){ma = 100;}int ma;
}; 
class Base2{
public:Base2(){mb = 200;}int mb;
}; 
//子类:继承base1 和 base2 
class Son : public Base1,public Base2{
public:Son(){mc = 300;md = 400;}int mc;int md;
};void test01(){Son s;//输出16,四个成员变量 cout<<"sizeof(Son):"<<sizeof(s)<<endl; 
}int main() {test01();return 0;
}    
#include<iostream>
#include <bits/stdc++.h>
using namespace std;
//多继承语法 
class Base1{
public:Base1(){ma = 100;}int ma;
}; 
class Base2{
public:Base2(){ma = 200;}int ma;
}; 
//子类:继承base1 和 base2 
class Son : public Base1,public Base2{
public:Son(){mc = 300;md = 400;}int mc;int md;
};void test01(){Son s;//输出16,四个成员变量 cout<<"sizeof(Son):"<<sizeof(s)<<endl;cout<<"Base 1 ma=:"<<s.Base1::ma<<endl;cout<<"Base 2 ma=:"<<s.Base2::ma<<endl;
}int main() {test01();return 0;
}    

http://www.ppmy.cn/devtools/17918.html

相关文章

在Go项目中使用ELK进行日志采集

在当今企业级应用开发中&#xff0c;日志管理和分析是非常重要的部分。ELK Stack&#xff08;Elasticsearch、Logstash、和Kibana&#xff09;是一套广泛使用的开源工具&#xff0c;用于日志的收集、存储、搜索、分析与可视化。本文将深入解析如何在Go项目中实现ELK日志采集&am…

基于Python实现的推箱子小游戏

Python贪吃蛇小游戏实现: 推箱子曾经在我们的童年给我们带来了很多乐趣。推箱子这款游戏现在基本上没人玩了&#xff0c;甚至在新一代人的印象中都已毫无记忆了。。。但是&#xff0c;这款游戏可以在一定程度上锻炼自己的编程能力。 运行效果如图所示&#xff1a; 游戏关卡有点…

Cache缓存

在计算机架构中&#xff0c;缓存&#xff08;Cache&#xff09;是一种高速数据存储层&#xff0c;它存储了一部分从原始数据源&#xff08;如主内存&#xff09;频繁访问的数据副本。通过将数据暂时存储在物理上更接近处理器的位置&#xff0c;缓存能够减少数据访问的延迟&…

Qt下使用OpenCV截取图像并在QtableWidget表格上显示

文章目录 前言一、在QLabel上显示图片并绘制矩形框二、保存矩形框数据为CSV文件三、保存截取图像四、将截取图像填充到表格五、图形视图框架显示图像六、示例完整代码总结 前言 本文主要讲述了在Qt下使用OpenCV截取绘制的矩形框图像&#xff0c;并将矩形框数据保存为CSV文件&a…

苹果一次性开源了8个大模型! 包含模型权重、训练日志和设置,OpenELM全面开源

不以开放性著称的苹果居然同时开源了大模型的权重、训练和评估框架&#xff0c;涵盖训练日志、多个保存点和预训练设置。同时升级计算机视觉工具包 CVNets 为 CoreNet&#xff01;支持 OpenELM&#xff01; ▲图1.由Stable Diffusion3生成。 OpenELM是Apple苹果公司最新推出的…

前端学习笔记3

列表、表格与表单​ 列表就是信息资源的一种展示形式。它可以使信息结构化和条理化,并以列表的样式显示出来,以便浏览者能更快捷地获得相应的信息。 3.0 代码访问地址 https://gitee.com/qiangge95243611/java118/tree/master/web/day03 3.1 列表 ​ 列表大致可以分为3类…

Flutter开发之--初识Flutter

文章目录 概述Flutter整体架构嵌入层引擎层框架层 跑通demo尝鲜Flutter项目的目录介绍Flutter demo项目的运行 总结 概述 Flutter 是由Google公司研发的一种跨端开发技术&#xff0c;在2018年正式推出。Flutter自带Skia图形绘制引擎&#xff0c;采用自绘制的方式&#xff0c;不…

基于SpringBoot开发的同城租房系统租房软件APP小程序源码

项目背景 一、市场前景 随着城市化进程的加快和人口流动性的增强&#xff0c;租房市场正逐渐成为一个不可忽视的巨大市场。传统的租房方式往往存在着信息不对称、效率低下等问题&#xff0c;而同城租房软件的出现&#xff0c;则有效地解决了这些问题&#xff0c;为租房市场注…

Leetcode-168.Excel表列名称

题目描述 给你一个整数 columnNumber &#xff0c;返回它在 Excel 表中相对应的列名称。 例如&#xff1a; A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...示例 1&#xff1a; 输入&#xff1a;columnNumber 1 输出&#xff1a;"A"…

DDR3 MIG IP核解决方案

DDR3 MIG IP核解决方案 信号方向描述app_addr [ADDR_WIDTH - 1&#xff1a;0]输入该输入指示当前请求的地址。app_cmd [2&#xff1a;0]输入该输入选择当前请求的命令。app_en输入这是app_addr []&#xff0c;app_cmd [2&#xff1a;0]&#xff0c;app_sz和app_hi_pri输入的高…

BGP的基本配置

l 按照以下步骤配置BGP协议&#xff1a; 第1步&#xff1a;设备基本参数配置&#xff0c;AS内配置IGP确保内部网络连通性&#xff1b; l 配置IGP&#xff08;OSPF协议等&#xff09;路由解决peer对等体的源和目标IP之间连通性&#xff0c;确保peer之间TCP&#xff08;179&a…

故障诊断 | 基于迁移学习和SqueezeNet 的滚动轴承故障诊断(Matlab)

目录 效果一览基本介绍程序设计参考文献 效果一览 基本介绍 将一维轴承振动信号转换为二维尺度图&#xff08;时频谱图&#xff09;&#xff0c;并使用预训练网络应用迁移学习对轴承故障进行分类。 迁移学习显著减少了传统轴承诊断方法特征提取和特征选择所花费的时间&#xff…

react18 antd 引入导航栏之后一些bug,解决方法收集

概述&#xff1a; 我们开发react引入antd之后导航栏会出现刷新不选中、不展开二级导航栏、页面js点击之后不选中最新tab、只能展开一个二级tab之类的问题。那么我们一起来把问题给解决了 问题描述 其实问题这些问题差不多就是一个问题&#xff0c;就是Menu没有刷新选中的状态…

【算法刷题 | 贪心算法03】4.25(最大子数组和、买卖股票的最佳时机|| )

文章目录 4.最大子数组和4.1题目4.2解法一&#xff1a;暴力4.2.1暴力思路4.2.2代码实现 4.3解法二&#xff1a;贪心4.3.1贪心思路4.3.2代码实现 5.买卖股票的最佳时机||5.1题目5.2解法&#xff1a;贪心5.2.1贪心思路5.2.2代码实现 4.最大子数组和 4.1题目 给你一个整数数组 n…

Lagent AgentLego 智能体应用搭建-作业六

本次课程由Lagent&AgentLego 核心贡献者樊奇老师讲解【Lagent & AgentLego 智能体应用搭建】课程。分别是&#xff1a; Agent 理论及 Lagent&AgentLego 开源产品介绍Lagent 调用已有 Arxiv 论文搜索工具实战Lagent 新增自定义工具实战&#xff08;以查询天气的工具…

python篇-自动化-poco

欢迎使用Poco (ポコ) UI自动化框架 — poco 1.0 文档 Poco脚本入门 - 如何使用Poco API文档 - 《Airtest v1.2 中文文档》 - 书栈网 BookStack

深入浅出 SQL 优化:全面提升查询性能的技巧

文章目录 前言一、表结构分析1. 索引分析2. 数据类型分析3. 思考反范式设计的适用场景与潜在风险3.1数据冗余3.2 数据一致性3.3 更新性能 4. 关注临时表的创建与使用。4.1.尽量减少临时表的使用&#xff0c;以降低系统资源的消耗。4.2 使用合适的索引和数据类型优化临时表的性能…

说一下 session 的工作原理?

Session的工作原理主要涉及到用户与服务器之间的交互和会话状态的维护。以下是关于Session工作原理的详细解释&#xff1a; 当用户首次访问Web应用时&#xff0c;服务器会为该用户创建一个唯一的Session ID。这个Session ID是服务器用来标识和跟踪该用户会话的关键。服务器将生…

项目管理中常用的三个工具:甘特图、看板、燃尽图

在日常项目管理的实践中&#xff0c;为了更有效地追踪项目进度、优化资源配置和提高团队协作效率&#xff0c;管理者常常会借助一些工具来辅助工作。这些工具的本质在于将抽象复杂的项目管理任务具象化、简单化&#xff0c;以更直观、方便的方式呈现出来。 以下介绍项目管理中…

Electron 桌面应用程序的框架,快速入门搭建一个桌面程序

Electron是什么&#xff1f; Electron 快捷传送门,点击走你。。。 快速让你的web项目成为桌面应用 // 初始化一个 package.json npm init// 添加 Electron 依赖&#xff0c;安装过可忽略 npm install --save-dev electron{ "name": "my-electron-app",&…