面向对象第四讲
函数重载
代码质量:可读性、复用性、扩展性、维护性
int a,b,c;//缺少自注释性
int count;
int max_num;
代码规范:
1.用英文(不用拼音或单字母)
2.下划线或驼峰式
函数重载: 函数名相同,函数形参个数、类型或者顺序不同。
个数:
int add(int a,int b);
int add(int a);
类型:
int add(int a,int b);
double add(double a,double b);
顺序:(前提:类型不同)
double add(int a,double b);
double add(double b,int a);
函数重载注意事项: 默认参数会影响重载规则
示例:
int add(int a,int b = 6)//有默认参数
{
cout << "add int int" << endl;
}
int add(int a)
{
cout << "add int" << endl;
}
add(5);//报错
编译器如何实现函数重载?
编译器会自己给函数重命名。
int add(int a,int b)//add_int_int
函数占位参数
int add(int a,int b,int)//没有给形参名字(预留,提示函数可能会升级)
struct结构体升级
#include <stdio.h>
#include <string.h>
struct Student
{
int num;
char name[20];
int age;
};//;不可省
int main()
{
struct Student stu;
stu.num = 1;
stu.age = 12;
strcpy(stu.name,"zhangsan");
struct Student *p_stu = &stu;
p_stu->age = 12;
return 0;
}
C++升级结构体
#include <iostream>
#include <srting.h>
using namespace std;
struct Student
{
int num;
char name[20];
int age;
//升级2:可以保存变量和函数
void printInfo()
{
cout << num << endl;
cout << name << endl;
cout << age << endl;
}
};
//升级5:实现继承、多态
//升级4:引入了访问权限符
//public(可以在结构体或者类外访问)、protected(只允许结构体或者类内访问)、private(只允许结构体或者类内访问)
//升级3:用class(类)替换struct(结构体)
/**
*class VS struct
*区别1:命名不同(对象、变量)(方法、函数)(属性、变量)
*区别2:默认访问权限不同(struct默认访问权限是共有的,class默认访问权限是私有的)
*/
class Teacher
{
int num;//成员变量或者属性
char name[20];
int age;
void printInfo()//成员函数或者成员方法
{
cout << num << endl;
cout << name << endl;
cout << age << endl;
}
};
int main()
{
//升级1:定义变量(不需要再加struct)
Student stu;//stu结构体变量
stu.num = 12;
stu.age = 20;
strcpy(stu.name,"zhangsan");
stu.printInfo();
Student *p_stu = &stu;
p_stu->num = 13;
p_stu->printInfo();
Teacher t1;//t1对象
return 0;
}
总结:
升级1:定义变量(不需要再加struct)
升级2:可以保存变量和函数
升级3:用class(类)替换struct(结构体)
class & struct 区别 |
---|
区别1:命名不同(对象、变量)(方法、函数)(属性、变量) |
区别2:默认访问权限不同(struct默认访问权限是共有的,class默认访问权限是私有的) |
升级4:引入了访问权限符
public | 可以在结构体或者类外访问 |
---|---|
protected | 只允许结构体或者类内访问 |
private | 只允许结构体或者类内访问 |
升级5:实现继承、多态
注意事项: 凡是在类内实现的方法,编译器会尽可能优化成inline函数;方法函数尽量类外实现。
编码规范: .h文件定义类 .cpp文件实现类的成员函数
#pragma once//每次写头文件加,防止头文件重名包含
string类
#include <iostream>
#include <string>
using namespace std;
int main()
{
char src[100] = "hello world";
const char *ptr = "hello world";
string s1 = "hello world";
for(int i = 0;i < s1.size();i++)
{
//cout << s1[i] <<endl;
cout << s1.at(i) << endl;//越界会报错,更安全
}
string s2("hello world");//构造函数
string s3;
s3 = "hello world";
string s4('h',100);//构造函数
string s5;
s5 = s1 + s2;//s5 = s1.append(s2);
s1 = s1 + s2;
s5 = s1.append(ptr);
s1 = s1 + "hihi";
s1.append(10,'a');
if(s1 == s2)
{
}
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
迭代器(指针):
以"hello"为例
string s1 = "hello world";
string::iterator it1 = s1.begin();
string::iterator it2 = s1.end();
for(;it1 != it2; it1++)
{
cout << "it1 = " << *it1 << endl;
}
优化
for(string::iterator it1 = s1.begin(); it1 != s1.end(); it1++)
{
cout << "it1 = " << *it1 << endl;
}
再优化
for(auto it1 = s1.begin(); it1 != s1.end(); it1++)
{
cout << "it1 = " << *it1 << endl;
}
h | e | l | l | o |
---|---|---|---|---|
begin(); | end(); | |||
rend(); | rbegin(); | |||
cbegin(); | cend(); |
删除
错误一:
string s1 = "hello world";
for(auto it1 = s1.begin(); it1 != s1.end(); it1++)
{
if(*it1 == 'l')
{
s1.erase(it1);
}
}
cout << "erase = " << s1 << endl;
现象: 无法删除连续的两个字符
原因: 如上操作,第一个"l"被删除后,第二个“l”被it1++操作跳过了
改进:
string s1 = "hello world";
for(auto it1 = s1.begin(); it1 != s1.end(); it1++)
{
if(*it1 == 'l')
{
s1.erase(it1);
}
else
{
it1++;
}
}
cout << "erase = " << s1 << endl;
错误二:
string s1 = "hello worldl";//末尾加了个"l"
for(auto it1 = s1.begin(); it1 != s1.end(); it1++)
{
if(*it1 == 'l')
{
s1.erase(it1);
}
}
cout << "erase = " << s1 << endl;
现象: 段错误
原因: "endl();“未更新新的”\n"的地址
改进:
string s1 = "hello worldl";
for(auto it1 = s1.begin(); it1 != s1.end(); it1++)
{
if(*it1 == 'l')
{
s1.erase(it1);
}
else
{
it1++;
}
}
cout << "erase = " << s1 << endl;
建议: 一般不在迭代器里做插入和删除操作
vector类
#include <iostream>
#include <vector>
//还可以保存类(未列举)
using namespace std;
int main()
{
int a[10];
char src[100];
vector<int> va;
va.push_back(1);
va.push_back(2);
va.pushback(3);
vector<string> vs;
vs.push_back("hello1");
vs.push_back("hello2");
vs.push_back("hello3");
for(auto it = va.begin(); it != va.end(); it++)//①
{
cout << *it << endl;
}
for(int i = 0 ; i < va.size(); i++)//或者②
{
cout << va[i] << endl;
cout << va.at(i) << endl;
}
return 0;
}