第四周嵌入式周结
C++的函数重载
代码质量:可读性、复用性、扩展性、维护性
//c
//较差的:
int main()
{
int a,b,c;//自注释性
int count;
int max_num;//maxNum;
int min_num;
add(5);
add(5,5.13);
add(5.13,5);
return 0;
}
//函数重载:
viod add_user()
{
}
int add(int a, int b)//add_int_int
{
cout << "add int int" << endl;
}
double add_double(double a, double b)//形参个数、顺序、类型不同
{
cout << "add double double" << endl;
}
int add(int a)
{
cout << "add int" << endl;
}
//顺序不同的情况:
double add(int a, double b)//或者double add(double b int a)
{
cout << "add int double" << endl;
}
函数重载(函数功能重新做修改):函数名相同。函数形参个数、类型或者顺序不同(函数的返回值不能做函数重载)
注意事项:默认参数会影响重载规则;(二义性)
编译器如何实现函数重载?
函数的占位参数概念
int add(int a, int b, int)//最后的int 为占位参数
struct 结构体
-
升级1:定义变量
-
升级2:可以保存变量和函数;
-
升级3:用class(类)替换struct(结构体)
- class VS struct
区别1:命名不同(对象,变量)(方法,函数)(属性,变量)
区别2:class默认访问权限私有
struct默认访问权限公有
- class VS struct
-
升级4:引入了访问修饰符:public、private、protected
public:可以在结构体或类外访问
private:可以在结构体或类内访问
protected:可以在结构体或类内访问 -
升级5:实现继承、多态
注意事项:凡是在类内实现的方法,编译器尽可能优化为online函数。方法、函数尽量类外实现
编码规范:“.h”文件定义类;“.cpp”文件实现类的成员函数
#pragme once//为了避免同一个文件被包含多次
(课上的代码例子,可能不完善)
//定义一个struct结构体的代码
#include <stdio.h>
#include <string.h>//实现下边代码的头文件
struct Student
{
public://以下的花括号内都可以访问
int num;
char name[20];
int age;
//升级2:可以保存变量和函数
viod printTnfo()
{
cout << num << endl;
cout << name << endl;
cout << age << endl;
}
};//分号不要忘
//升级3:用 class替换struct
class Teacher
{
int num;//成员变量或者属性
int age;
char name[20];
viod printInfo() //成员函数或者成员方法
{
cout << num << endl;
cout << name << endl;
cout << age << endl;
}
};
int main()
{// 升级1(定义变量) :
Student stu;//在C语言中:struct Student stu;
//stu是结构体变量
stu.num = 1;
stu.age = 12;
Student *p_stu = &stu;
stu.printInfo()//可调用这个函数
//如果是stu.name = "zhangsan";(会报错)则为strcpy(stu.name,"zhangsan");
struct Student *p_stu = &stu;
p_stu->age = 12;//结构体指针
p_stu->printInfo();
Teacher t1;//t1是对象
t1.age = 22;//class里的对象无法调用,public可以实现
return 0;
}
常见容器
string类(P255页)
-
特性描述
-
构造函数
-
遍历迭代器(指针)
(1)begin();指向容器开头元素的迭代器;
(2)end();指向容器末尾的=元素的迭代器;
(3)rend();开头;
(4)rbegin();末尾。 -
string类与char*类型的转换
-
赋值与连接
-
查找与替换
-
删除与插入
#include<iostream>
#include<string>
#include<stdio.h>//C语言头文件
using namespace std;
int main()
{
char src[100] = "helloo world";
const char *ptr = "hello world";
//C
while(*ptr != '\0')
{
printf("%c\n",*ptr);
ptr++;
}
string s1 = "hello world";//第一种初始化(赋值)s1;
//cout << s1.at(100) << endl;//at访问越界会报错
//访问字符s1:
for(int i = 0;i < s1.size();i++)
{
//cout << s1[i] << endl;
cout << s1.at(i) << endl;
}
//迭代器(指针)
//string::iterator it1 = s1.begin();
//string::iterator it2 = s2.end();
//for(;it1 != it2; it1++)
//{
// cout << "it = " << *it1 << endl;
//}
//语句优化1:
//for(string::iterator it1 = s1.begin();it1 != s1.end(); it++)
//{
// cout << "it = " << *it1 << endl;
}
//语句优化2:
for(auto it1 = s1.begin();it1 != s1.end(); it++)
{
cout << "it = " << *it1 << endl;
}
//删除字符
for(auto it1 = s1.begin();it1 != s1.end(); )
{
if(*it1 == 'l')//删除字符L
{
s1.erase(it1);
}
else
{it1++;}
}
string s2("hello world");//第二种构造函数
string s3;
s3 = "hello world";//第三种
string s4('h',100);//字符串后加100个h;
string s5;
s5 = s1 + s2;//连接两个字符串
s1 = s1 + "hihi";//
//判断大小
if(s1 == s2)
{
}
//string s2;
// cin >> s2;//键盘输入s2,遇见空格即停止。
cout << s1
return 0;
}
Vetor 容器(P232)
概念