面向对象第四讲

面向对象第四讲

函数重载

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

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我无法回答第一个问题。 但是,我能够指导你如何运用Python语言实现这个问题的操作。 1. 创建Person class ``` class Person: def __init__(self, name, age, gender): self.name = name self.age = age self.gender = gender def personInfo(self): print("姓名:%s,年龄:%d,性别:%s" % (self.name, self.age, self.gender)) ``` 2. 创建Student class,继承自Person class,重写personInfo()方法,添加学生的college和stu_class属性以及learn()方法。 ``` class Student(Person): def __init__(self, name, age, gender, college, stu_class): super().__init__(name, age, gender) self.college = college self.stu_class = stu_class def personInfo(self): super().personInfo() print("所在学院:%s,班级:%s" % (self.college, self.stu_class)) def learn(self, teacher): knowledge = teacher.teach() print("老师%s,我终于学会了%s!" % (teacher.name, knowledge)) def __str__(self): return "姓名:%s,年龄:%d,性别:%s,所在学院:%s,班级:%s" % (self.name, self.age, self.gender, self.college, self.stu_class) ``` 3. 创建Teacher class,继承自Person class,添加学院college和专业major属性以及teach()方法。 ``` class Teacher(Person): def __init__(self, name, age, gender, college, major): super().__init__(name, age, gender) self.college = college self.major = major def personInfo(self): super().personInfo() print("教学院校:%s,所授专业:%s" % (self.college, self.major)) def teach(self): return "如何用面向对象设计程序" def __str__(self): return "姓名:%s,年龄:%d,性别:%s,教学院校:%s,所授专业:%s" % (self.name, self.age, self.gender, self.college, self.major) ``` 4. 创建三个学生对象并打印详细信息。 ``` student1 = Student("张三", 20, "男", "计算机科学与技术", "A班") student1.personInfo() print(student1) student2 = Student("李四", 21, "女", "软件工程", "B班") student2.personInfo() print(student2) student3 = Student("王五", 19, "男", "信息安全", "C班") student3.personInfo() print(student3) ``` 5. 创建一个老师对象并打印详细信息。 ``` teacher = Teacher("赵六", 35, "男", "计算机科学与技术", "软件工程") teacher.personInfo() print(teacher) ``` 6. 学生对象调用learn方法。 ``` student1.learn(teacher) student2.learn(teacher) student3.learn(teacher) ``` 7. 将三个学生对象添加至列表中,通过循环将列表中的对象打印出来。 ``` students = [student1, student2, student3] for student in students: print(student)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值