2022.9.27从零开始学习c++的第九天。

函数高级

#include<iostream>
using namespace std;

//函数的默认参数
int func1(int a, int b, int c)
{
    return a + b + c;
}
int func2(int a, int b = 20, int c = 30)//如果传入参数值,用传入值。如果没有传入参数值则使用默认值
{   
    //如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值
    return a + b + c;
}
//如果函数的声明有了默认参数,函数的实现就不能有默认参数
// 声明和实现只能有一个有默认参数
//int func3(int a=10,int b=10)函数的声明

//int func3(int a=10,int b=10)函数的实现error
//{
// return a+b;
//}


//占位参数
void func3(int a, int)//占位参数还可以有默认参数void fun3(int a,int =10)
{
    cout << "this is func3" << endl;
}


//函数的重载
void func4(double a)
{
    cout << "func的调用float" << endl;
}
void func4(int a)
{
    cout << "func的调用int" << endl;
}
void func4(int a ,int b)
{
    cout << "func的调用 两个int" << endl;
}
void func4(double a, int b)
{
    cout << "func的调用 double int" << endl;
}
void func4(int a, double b)
{
    cout << "func的调用 int double" << endl;
}
//函数的返回值不可以作为函数重载的条件
//int func4(int a, double b)
//{
//cout << "func的调用 int double" << endl;
//}


//引用作为函数重载的条件
void func5(int& a)
{
    cout << "func(int& a)调用" << endl;
}
void func5(const int& a)
{
    cout << "func(const int& a)调用" << endl;
}
void func6(int a,int b)
{
    cout << "func(int a,int b)调用" << endl;
}
//void func6(int a, int b=10)
//{
//    cout << "func(int a,默认int b)调用" << endl;
//}
int main()
{
    //函数的默认参数
    //函数的形参列表中的形参是可以有默认值的
    //返回值类型 函数名(参数=默认值){}
    cout << func1(10, 20, 30) << endl;
    cout << func2(10) << endl;
    cout << func2(10, 30) << endl;


    //函数的占位参数
    //函数形参列表中可以有占位参数,用来占位,调用函数时必须填补该位置
    //返回值类型 函数名(数据类型){}
    func3(10, 10);


    //函数重载
    //函数名可以相同,提高复用性
    //函数重载满足条件,在同一作用域下,函数名称相同,函数的参数(形参列表)类型不同或者个数不同或者顺序不同
    //函数的返回值不可以作为函数重载的条件
    func4(10);
    func4(10.1);
    func4(10,10);
    func4(10.1, 10);
    func4(10, 10.1);


    //函数重载的注意事项
    //引用作为函数重载的条件
    //函数重载碰到函数默认参数
    int a = 10;
    func5(a);
    func5(10);
    func6(10,20);//函数重载遇到默认参数经常出错,建议避免这种情况
    

    system("pause");

    return 0;
}

封装

#include<iostream>
using namespace std;
const double PI = 3.14;

//设计一个圆类,求周长
//2*PI*半径
//class代表设计一个类,类后面跟着类的名称
class circle
{
    //访问权限
public://公共权限
    //属性
    int m_r;
    //行为(通常用函数来代表行为)
    double calculateZC()
    {
        return 2 * PI * m_r;
    }
};
//设计一个学生类
class student
{
public:
    //属性
    string name;
    int id;
    //行为
    /*void cinshow()
    {
        cout << "请输入学生的名字:" << endl;
        cin >> name;
        cout << "请输入学生的学号:" << endl;
        cin >> id;
        cout << "学生的名字是:" << name << endl;
        cout << "学生的学号是:" << id << endl;
    }
    */
    void show()
    {
        cout << "学生的名字是:" << name << endl;
        cout << "学生的学号是:" << id << endl;
    }

    void setname(string NAME)
    {
        name = NAME;
    }
    void setid(int ID)
    {
        id = ID;
    }
};
  

//公共权限public    类内可以访问,类外可以访问
//保护权限protected 类内可以访问,类外不可以访问  (子类可以访问父类保护中的内容)
//私有权限private   类内可以访问,类外不可以访问  (子类不可以访问父类私有中的内容)
class person
{
    //公共权限
public:
    string name;
    //保护权限
protected:
    string car;
    //私有权限
private:
    int password;
public:
    void func()
    {
        name = "张三";
        car = "破车";
        password = 123;
    }
};


class C1
{
    int m_a;//默认权限私有
};
struct C2
{
    int m_a;
};


class people
{
public:
    //设置姓名
    void setname(string name)
    {
        m_name = name;
    }
    //获取姓名
    string getname()
    {
        return m_name;
    }
    //设置姓名
    void setage(int age)
    {
        if (age < 0 || age>120)
        {
            cout << "妖怪" << endl;
            return;
        }
        m_age = age;
    }
    //获取年龄
    int getage()
    {
        return m_age;
    }
    void setlover(string lover)
    {
        m_lover = lover;
    }
private:
    string m_name; //可读可写
    int m_age;     //可读可写
    string m_lover;//只写
};
int main()
{
    //c++面向对象的三大特性:封装,继承,多态
    //万物万事皆为对象,对象上有其属性和行为
    //具有相同性质的对象,抽象为类
    //封装的含义
    //将属性和行为作为一个整体,表现生活中的事物
    //将属性和行为加以权限控制
    //在设计类时,行为和属性写在一起,表现事物
    //calss 类名{ 访问权限:属性/行为};
    //类中的属性和行为统称为成员
    //属性 成员属性 成员变量
    //行为 成员函数  成员方法
    

    //通过圆类创造具体的圆(对象)实例化一个对象
    circle c;
    //给圆对象的属性进行赋值
    c.m_r = 10;
    cout << "圆的周长为:" << c.calculateZC() << endl;


    //学生类
    student s1;
    //s1.cinshow();
    s1.setname("张三");
    s1.setid(1);


    //类在设计时,可以把属性和行为放在不同的访问权限下,加以控制
    //访问权限有三种 public 公共访问权限  protected 保护访问权限  private 私有访问权限
    person p1;
    p1.name = "李四";
    //保护权限 不可访问  p1.car = "奔驰";
    //私有权限 不可访问  p1.password =123; 
    p1.func();//公共权限


    //struct和class的区别 
    //struct和class的唯一区别在于默认的访问权限不同
    //struct 默认公共权限  class默认私有权限
    C1 c1;
    //c1.m_a = 100; error 私有  类外不可访问
    C2 c2;
    c2.m_a = 100;//公共  类外可以访问


    //成员属性私有化
    //将所有成员属性设置为私有,可以自己控制读写权限
    //对于写权限,我们可以检测数据的有效性
    people P;
    P.setname("张三");
    cout << P.getname() << endl;
    P.setage(18);
    cout << P.getage() << endl;


    P.setlover("李四");
    system("pause");

    return 0;

}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值