• C嘎嘎 - 类和对象


    1. 封装

    (1)语法

            class 类名{ 访问权限 : 属性 / 行为 };

    1. class Circle
    2. {
    3. public: // 访问权限
    4. int r;
    5. double s;
    6. double sq(){
    7. return 2*r*s;
    8. }
    9. };
    10. int main()
    11. {
    12. Circle c1;
    13. c1.r = 2;
    14. c1.s = 2.1;
    15. c1.sq();
    16. }

    (2)构造函数和析构函数

        01 构造函数语法:类名() {};可以重载;没有返回值也不写void;

        02 析构函数语法:~类名(){};不可以重载;没有返回值也不写void;

        03 对象的初始化和清理:构造函数进行初始化操作,析构函数进行清理操作。

    1. class Person
    2. {
    3. Person()
    4. {
    5. cout << "构造函数的调用" << endl;
    6. }
    7. ~Person()
    8. {
    9. cout << "析构函数的调用" << endl;
    10. }
    11. };

    (3)构造函数的分类和调用

        01 分类:

            001 按参数分为:有参构造和无参构造(默认)

            002 按类型分为:普通构造和拷贝构造

    1. class Person
    2. {
    3. Person (int a)
    4. {
    5. age = a;
    6. cout << "有参构造函数的调用" << endl;
    7. }
    8. // 拷贝构造函数
    9. Person(const Person &p)
    10. {
    11. age = p.age;
    12. }
    13. int age;
    14. };

        02 三种调用方式:

            001 括号法

            002 显示法

            003 隐式转换

    1. class Person
    2. {
    3. Person()
    4. {
    5. cout << "无参构造函数的调用" << endl;
    6. }
    7. Person (int a)
    8. {
    9. age = a;
    10. cout << "有参构造函数的调用" << endl;
    11. }
    12. // 拷贝构造函数
    13. Person(const Person &p)
    14. {
    15. age = p.age;
    16. cout << "拷贝构造函数的调用" << endl;
    17. }
    18. int age;
    19. };
    20. //括号法
    21. void test01()
    22. {
    23. Person p1; //默认构造函数调用
    24. Person p2(10); //有参构造函数调用
    25. Person p3(p2); //拷贝构造函数调用
    26. };
    27. //显示法
    28. void test01()
    29. {
    30. Person p1; //默认构造函数调用
    31. Person p2 = Person p2(10); //有参构造函数调用
    32. Person p3 = Person p3(p2); //拷贝构造函数调用
    33. };

     (4)初始化列表

        01 语法:构造函数() : 属性1(值1),  属性2(值2)……{}

        

    1. class Person
    2. {
    3. // 传统初始化方式
    4. Person (int a, int b, int c)
    5. {
    6. M_a =a;
    7. M_b =b;
    8. M_c =c;
    9. }
    10. // 初始化列表初始化属性
    11. Person(int a, int b, int c) : M_a(a), M_b(b), M_c(c) {}
    12. int M_a;
    13. int M_b;
    14. int M_c;
    15. };
    16. void test()
    17. {
    18. Person p(10, 20, 30);
    19. cout << M_a << M_b << M_c << endl;
    20. };

    2. 继承

    (1)语法

         class 子类名称 : 继承方式 父类名称{……};

    1. class Person
    2. {
    3. public:
    4. int age;
    5. string name;
    6. };
    7. class Student : public Person
    8. {
    9. public:
    10. cout<< "独有的" << endl;
    11. };

    (2)多继承语法

        class 子类 : 继承方式 父类1,   继承方式 父类2,  ……

    1. class Person
    2. {
    3. public:
    4. int age;
    5. string name;
    6. };
    7. class People
    8. {
    9. public:
    10. int age;
    11. string name;
    12. };
    13. class Student : public Person, public People
    14. {
    15. public:
    16. cout<< "独有的" << endl;
    17. };

  • 相关阅读:
    Adobe收购的Figma,是如何发展起来的
    跨平台应用开发进阶(三十三) :Android上架准备材料及流程介绍
    “富二代”极氪,辉煌过后总要独自长大
    【JavaScript-25】js中深浅拷贝及其方法
    Java集合框架(二)List
    Scrapy-Center——spa7,spa10,spa11,spa12分析
    windows实用功能快捷入口
    关于2023年汉字小达人市级比赛的填空题,及孩子如何提高打字速度
    FH6908A负极关断同步整流模拟低压降二极管控制IC芯片TSOT23-6超低功耗整流器 1w功耗 <100uA静态 替代MP6908
    Go开源世界主流成熟ORM框架gorm实践分享
  • 原文地址:https://blog.csdn.net/fengbaobaola/article/details/127801565