• 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. };

  • 相关阅读:
    36-Java方法的案例:求和、判断奇偶数、求最值
    创新工具 | 快速创作高质量SEO博文的6个技巧
    创建符合 Web 可访问性标准的 HTML 布局
    sql语句去掉括号及中括号中的内容,大型数据库与access数据库
    Go 语言中 defer 使用时有哪些陷阱?
    南理工在线交流群
    机器学习第十一课--K-Means聚类
    Linux的screen工具库实现多终端
    【无标题】
    Windows下安装Mindspore CPU版本时出现warning
  • 原文地址:https://blog.csdn.net/fengbaobaola/article/details/127801565