函数重载/类和对象(P14-P60)

一、函数重载--基本语法

1.函数重载的作用:函数名相同,提高复用性。

2.函数重载满足的条件:

(1)同一个作用域

(2)函数名称相同

(3)函数参数类型不同或者个数不同或者顺序不同

函数的返回值不可以作为函数重载的条件。

cpp 复制代码
#include<iostream>
using namespace std;
//函数重载
//可以让函数名相同,提高复用性。
//函数重载的条件: 同一个作用域下,函数名称相同。函数参数类型不同,个数不同,顺序不同。
//函数的返回值不可以作为函数重载的条件,比如void func(),int func()
void func(){
    cout<<"func的调用"<<endl;
}
void func(int a){
    cout<<"func(int a)的调用"<<endl;
}
void func(double a){
    cout<<"func(double a)的调用"<<endl;
}
void func(int a,double b){
    cout<<"func(int a,double b)的调用"<<endl;
}
void func(double a,int b){
    cout<<"func(double a,int b)的调用"<<endl;
}
int main(){
func(10);
func(3.14);
func(10,3.14);
func(3.14,10);
}
cpp 复制代码
func(int a)的调用
func(double a)的调用
func(int a,double b)的调用
func(double a,int b)的调用

二、函数重载的注意事项

cpp 复制代码
#include<iostream>
using namespace std;

//函数重载的注意事项
//1.引用作为重载的条件
void func(int &a){//int &a=10,不合法
    //引用必须在合法的内存空间,一般放在栈和堆区
    //10是放在常量区
    cout<<"func(int &a)调用"<<endl;

}
void func(const int &a){//const是只读
//const int &a=10;加上const会创建一个临时的空间指向a,合法
    cout<<"func(const int &a)调用"<<endl;

}

//2.函数重载碰到的默认参数
void func2(int a,int b=10)
{
    cout<<"func2(int a,int b)的调用"<<endl;
}
void func2(int a)
{
    cout<<"func2(int a)的调用"<<endl;
}

int main(){
    int a=10;
    func(a);

    func(10);
    //func2(10);//当函数重载碰到默认参数,出现二义性,报错,尽量避免,会报错
}
cpp 复制代码
//输出
func(int &a)调用
func(const int &a)调用
func2(int a)的调用

三、类和对象-封装-属性和行为作为整体。

c++面向对象的三大特性:封装、继承和多态。

对象有属性和行为。

(1)封装

1)封装说C++面向对象三大特性之一。

2)封装的意义:

将属性和行为作为一个整体,表现生活中的事物。

将属性和行为加以权限控制。

3)在设计类的生活,属性和行为写在一起,表现事物

(2)语法

cpp 复制代码
class 类名{访问权限;属性/行为};

(3)代码实现

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

类和对象-封装-属性和行为作为整体

cpp 复制代码
//示例1:设计圆类,求圆的周长
//类和对象-封装-属性和行为作为整体
#include<iostream>
using namespace std;
const double PI=3.14;
//设计一个圆类,求圆的周长
//圆求周长的公式:2*PI*半径

//class 设计一个类,类后面跟着的就是类的名称。
class Circle{
//访问权限
public:
//属性
int m_r;
//行为
//获取圆的周长吗
double calZC(){
    return 2*PI*m_r;
}
};
int main(){
//通过圆类,创建具体的圆(对象)
//实例化,通过一个类创建一个对象的过程。
Circle c1;
//给圆对象的属性进行赋值
c1.m_r=10;
cout<<"圆的周长为:"<<c1.calZC()<<endl;
}
cpp 复制代码
//输出
圆的周长为:62.8

(4)代码实现

设计一个学生类,属性有姓名和学号,可以给姓名和学号赋值,显示学生的姓名和学号。

cpp 复制代码
#include<iostream>
using namespace std;

class Student{
    public:
    //属性
    string m_name;
    int m_id;
    void setName(string name){
        m_name=name;
    }
    void setID(int id){
        m_id=id;
    }
    void showStudent(){
       cout<<"姓名:"<<m_name<<",学号"<<m_id<<endl; 
    }
};

int main(){
    //创建一个具体的学生 ,实例化对象
    Student s1;
    //给s1对象,进行属性赋值
    s1.m_name="LL";
    s1.m_id=111;
    //显示学生信息
    s1.showStudent();
    

    Student s2;
    s2.m_name="PPQQ";
    s2.m_id=2;
    s2.showStudent();

    return 0;  
}
cpp 复制代码
//输出
姓名:LL,学号111
姓名:PPQQ,学号2

给学生姓名和学号赋值

cpp 复制代码
#include<iostream>
using namespace std;

class Student{
    public:
    //属性
    string m_name;
    int m_id;
    //给姓名赋值
    void setName(string name){
        m_name=name;
    }
    void setID(int id){
        m_id=id;
    }
    void showStudent(){
       cout<<"姓名:"<<m_name<<",学号"<<m_id<<endl; 
    }
    
};

int main(){
    //创建一个具体的学生 ,实例化对象
    Student s1;
    //给s1对象,进行属性赋值
    //s1.m_name="LL";
    s1.m_id=111;
    s1.setName("LL");
    s1.setID(111);
    //显示学生信息
    s1.showStudent();
    

    Student s2;
    s2.m_name="PPQQ";
    s2.m_id=2;
    s2.showStudent();

    return 0;  
}
cpp 复制代码
姓名:LL,学号111
姓名:PPQQ,学号2

类中的属性和行为,我们统一称为成员。

属性也称为成员属性和成员变量。

行为也称成员函数和成员方法。

四、类和对象--封装-访问权限

类在设计时候,可以把属性和行为放在不同的权限下,加以控制:

访问权限有三种:

public(公共权限)、

protected(保护权限)、

private(私有权限)。

public 类内、类外都可以访问。

protected类内可以访问,类外不可以访问。

private类内可以访问,类外不可以访问。

cpp 复制代码
#include<iostream>
using namespace std;

class Person{
    public :
    string m_Name;

    protected:
    string m_Car;
    private:
    int m_Password;

    public://Person类里面,都属于类内,这边可以换成protected、private
    void func(){
        m_Name="LLL";
        m_Car="法拉利";
        m_Password=123456;
    }

};

int main(){
    //实例化一个具体的对象
Person p1;

p1.m_Name="OLL";
p1.func();
//p1.m_Car="奔驰";//保护权限,在类外访问不到
//p1.m_Password=123;//私有权限,在类外访问不到
return 0;
}
相关推荐
萧西待水2 小时前
奥赛一本通 1447 靶形数独
算法·深度优先
泯泷4 小时前
那段文字是谁删的?Yjs 14 正式版之前,一套删除归属方案的实现与边界
前端·javascript·算法
hold?fish:palm5 小时前
34 合并K个升序链表
javascript·算法·链表
Navigator_Z7 小时前
LeetCode //C - 1252. Cells with Odd Values in a Matrix
c语言·算法·leetcode
野槐7 小时前
前端项目优化(有了我,会发生...)
前端
aa小小8 小时前
【无标题】
前端
语戚8 小时前
力扣 1621. 大小为K的不重叠线段的数目:动态规划(Java 实现)
java·算法·leetcode·动态规划·力扣·dp
青山木9 小时前
Hot 100 --- 最长有效括号
java·数据结构·算法·leetcode·动态规划
计算机魔术师9 小时前
还在单线程跟 AI 聊天?Claude Code 并行多会话让你一个人干三个人的活
前端
志尊宝9 小时前
Vue3 零基础每日笔记(038):亲手封装 useDebounceFn 与 useThrottleFn——高频事件的流量阀
前端·javascript·vue·html·vue3