面向对象第四讲

本文详细介绍了C++中的函数重载,包括重载规则、注意事项及编译器实现,并通过示例解释了如何避免重载错误。此外,还探讨了结构体的升级,从C语言中的结构体到C++中的类,讲解了类的访问权限、成员函数、继承等概念。同时,文章还涉及了C++中的string类和迭代器的使用,以及在迭代器中进行元素删除的常见问题和解决方案。最后,简要介绍了vector类的操作方法。
摘要由CSDN通过智能技术生成

面向对象第四讲

函数重载

代码质量:可读性、复用性、扩展性、维护性

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;
}
hello
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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值