15-【每天亿点点C++,简单又有趣儿】封装

知识点:

  • 对象的初始化和清理
  • 构造函数的分类及调用
  • 结构体与类
  • 访问权限
#include<iostream>
#include<string>
using namespace std;


/* 设计一个圆的类,求周长 */
const double PI = 3.14;
class Circle
{
    //访问权限
public:
    int r;
    double calculateZC()
    {
        return 2*PI*r;
    }
};

/* 设计一个学生类 */
class Student
{
public:
    // 类中的属性和方法 统一称为成员
    // 属性 / 成员属性 / 成员变量
    // 成员函数 / 成员方法
    string name;
    int id; //学号
    void showStudent()
    {
        cout << "学生 " <<name<<"  "<< id << endl;
    }
    void setName(string setname)
    {
        name = setname;
    }
};

/*访问权限*/
// 公共权限 类内类外访问         public
// 保护权限 类内访问 参与继承    protected
// 私有权限 类内访问 不参与继承  private
class Person
{
public:
    //公共权限
    string name;
    void pri()
    {
        cout << "姓名 " <<name<<"  "<< id << endl;
    }

protected:
    string car;
private:
    int id;
};

/*struct 和 class区别:struct默认是公有,class默认是私有*/
class C_01
{
    int a;
};
struct C_02
{
    int a;
};


/*成员属性私有化*/
// 1-可以自己控制读写权限
// 2-对于写,可以检测数据有效性

class Dog
{
public:
    void setName(string namer)//设置
    {
        name = namer;
    }
    string getname() //获取
    {
        return name;
    }
    int getage() //获取
    {
        return age;
    }
    void setage(int sage) //设置
    {
        if ((sage<0)or (sage>150))
        {
             cout << "年龄超出有效范围 " <<endl;
             return ;
        }

        age = sage;
    }
private:
    string name;
    int age = 10;
    string lover;
};

/*对象的初始化和清理*/
//构造 创建时编译器自动运行   类名(){}
//析构 对象销毁前自动运行    ~类名(){}
void test_03();
class Cat
{
    //构造函数                                     析构函数
    // 1- 没有返回值 不用写void                      相同
    // 2- 函数名=类名                                相同
    // 3- 可以有参数 可以重载                      不能有参数
    // 4- 创建对象时 会自动调用 只调用一次         结束时自动调用
 public:
     Cat()
     {
         cout << "构造函数" <<endl;
     }
     // 析构函数

     ~Cat()
     {
         cout << "析构函数" <<endl;
     }

};

/*构造函数的分类及调用*/
//分类
class Pig
{
public:

    Pig()
    {
         cout << "Pig 无参(默认)构造函数" <<endl;
    }
    Pig(int a)
    {
         age = a;
         cout << "Pig 有参构造函数" <<endl;
    }
    Pig(const Pig &p)
    {
        //把一个对象复制一份新的
        age = p.age;
        cout << "Pig 拷贝构造函数" <<endl;
    }
    ~Pig()
    {
         cout << "Pig 析构函数" <<endl;
    }
    int age;
};

//调用
void test_04()
{

    //括号法
    Pig p_01;
    Pig p_02(10);
    Pig p_03(p_02);

    //---注意事项
    Pig p_04(); //编译器会认为这是函数声明





    //显式法
    Pig p_05;
    Pig p_06 = Pig(10);
    Pig p_07 = Pig(p_02);

    //---注意事项
    Pig(10);//匿名对象,词句执行完马上销毁
    Pig(p_08); //编译器会认为这是对象声明





    //隐式法
    Pig p_09 = 10 ;// === Pig p_09 == Pig(10)
    Pig p_10 = p_02 ;// === Pig p_10 == Pig(p_02)

}


/*构造函数调用场景*/
// 1、使用一个已经创建完毕的对象创建新的对象
// 2、值传递方式给函数参数传值
// 3、值方式返回局部对象
void test_06(Pig p)
{

}
Pig test_07()
{
    Pig p_l;
    p_l.age = 77;
    return p_l;
}

void test_05()
{
    Pig p_01(20);
    Pig p_02(p_01);
    cout << "p2 age:" << p_02.age << endl;//[1]
    test_06(p_02);     //[2]
    Pig pp = test_07();//[3]
    cout << "pp age:" << pp.age << endl;//[1]
}

/* 构造函数调用规则

  创建一个类 自动提供
                      默认构造 (空实现)
                     默认析构 (空实现)
                     拷贝构造 (值拷贝)
*/

class SanDi
{
public:
    int age=0;
};



void test_08()
{
    SanDi s_1;
    s_1.age = 90;
    SanDi s_2(s_1); //没有写拷贝构造函数,但是编译器提供了拷贝构造函数

    //当我们写了有参构造,系统就不提供无参构造了
    //当我们写了拷贝构造函数,系统不提供其他的构造函数

    //简单的说,我们自定义了更难的,编译器不提供简单的构造了


}
int main()
{

    Circle c; //创建一个对象
    c.r = 10;
    cout <<"周长:" << c.calculateZC()<<endl;



    Student s;//创建一个对象
    s.id = 10;
    s.name = "海绵宝宝";
    s.showStudent();

    Student s2;
    s2.id = 20;
    s2.setName("派大星");
    s2.showStudent();






    /*权限学习*/
    cout <<"\n\n********************************* 权限学习"<<endl;
    Person p;
    p.name = "章鱼哥";
    // 无权限 p.car = "car"
    // 无权限 p.id = 10;
    p.pri();






    /*struct 和 class区别:struct默认是公有,class默认是私有*/

    C_01 c01;
    // 权限错误 c01.a = 10

    C_02 c02;
    c02.a = 10;






    /*成员属性私有化*/
    Dog d;
    d.setName("泡芙阿姨");
    cout << "姓名 " <<d.getname()<<"  "<< d.getage() << endl;
    d.setage(20);
    cout << "姓名 " <<d.getname()<<"  "<< d.getage() << endl;


    /*对象的初始化和清理*/
    cout <<"\n\n********************************* 对象的初始化和清理"<<endl;
    Cat cat ;
    test_03();

    /*构造函数的分类及调用*/
    cout <<"\n\n********************************* 构造函数的分类及调用"<<endl;
    test_04();

    /*构造函数调用场景*/
    cout <<"\n\n********************************* 构造函数调用场景"<<endl;
    test_05();

    /* 构造函数调用规则*/
    cout <<"\n\n********************************* 构造函数调用规则"<<endl;
    test_08();
    system("pause");
    return 0;

}

void test_03()
{
    Cat cat ;
}

输出

在这里插入图片描述
知识点 :
初始化参数列表


#include<iostream>
#include<string>
using namespace std;
class Cutebird
{
public:
    //基础写法:
    /*
    Cutebird(int a,int b,int c)
    {
        C_A = a;
        C_B = b;
        C_C = c;
    }*/
    //初始化列表
    Cutebird(int a,int b,int c):C_A(a),C_B(b),C_C(c)
    {

    }
    int C_A;
    int C_B;
    int C_C;
};
void test_10()
{
    Cutebird cb(10,20,30);
    cout << "C_A C_B C_C:" << cb.C_A <<" " <<  cb.C_B << " " << cb.C_C << endl;
}

int main()
{
    test_10();

}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/magic_shuang/article/details/107590938
今日推荐