栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++面向对象---封装(2)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++面向对象---封装(2)

C++面向对象—封装(2)
Date:2021.9.30
Author:lqy

文章目录
    • C++面向对象---封装(2)
      • 一、初始化列表:
      • 二、类对象作为类成员:
      • 三、静态成员变量static
      • 四、静态成员方法
      • 五、成员变量和成员函数分开存储
      • 六、面向对象之This指针
      • 七、空指针访问成员函数
      • 八、面向对象class的const修饰符

一、初始化列表:
  1. 初始化列表简介:

    C++面向对象编程提供初始化列表完成类似于有参构造函数的功能

  2. 代码示例:

    class Person
    {
    public:
    	int a;
    	int b;
    	int c;
    public:
    	Person(int a, int b, int c)
    	{
            
    		this->a = a;
    		this->b = b;
    		this->c = c;
    	}
         
    	Person() :a(10), b(10), c(10) 
    	{}
    	Person(int aa, int bb, int cc) :a(aa), b(bb), c(cc) 
    	{}
    };
    
二、类对象作为类成员:
  1. 类对象作为类成员简介:

    • 构造函数->先创建内部成员,进而创建外部对象

    • 析构函数->先析构外部对象,进而析构内部成员

  2. 代码解析:

    class Phone
    {
    public:
    	string brand;
    public:
        
    	Phone() {}
    	Phone(string name)
    	{
    		this->brand = name;
    	}
    };
    class Person
    {
    public:
        
    	string name;
    	Phone p;
    public:
        
    	Person(string name, string pName)
    	{
    		this->name = name;
    		this->p.brand = pName;   // 赋值给对象的特定属性
    	}
        
        
    	Person(string Personname, string pName) :name(Personname), p(pName)  
    	{}
    };
    
    三、静态成员变量static
    1. 静态成员变量简介:

      1. 静态成员变量特点:
      • 所有对象共享同一份数据
      • 在编译阶段分配内存
      • 类内声明,类外进行初始化
      1. 静态成员变量的访问形式:
      • 借助于对象进行访问
      • 借助于类名进行访问
    2. 代码声明:

      class Person
      {
      
      
      public:
      	static int a;
      private:
      	static int b;  
      };
      
      
      int Person::a = 100; 
      int Person::b = 600;
      
      void test01()
      {
      	Person p;
      	Person p2;
      	
      	cout << p.a << endl;   //output->100
      	p2.a = 200;  
      	cout << p.a << endl;   //output->200
      }
      
      void test02()
      {
      	Person p;
          
      	cout << p.a << endl;
      	cout << Person::a << endl;  
      }
      
      四、静态成员方法
      1. 静态成员方法简介:

        • 与静态成员变量类似,所有对象共享同一个静态函数
        • 静态成员函数仅能访问静态成员变量
      2. 代码示例:

        class Person
        {
        public:
        	
        	static void func()
        	{
        		cout << "函数调用" << endl;
        		a = 100;  // 静态成员函数可以访问静态成员变量
        		// b = 100;  // 静态成员函数不可以访问非静态成员变量
                
        	}
        	static int a;   //静态成员变量
        	int b;
        
        };
        int Person::a = 0;   //类外初始化
        
        void test01()
        {
        	Person p;
        	
        	p.func();
        	Person::func();
        }
        
        五、成员变量和成员函数分开存储
        1. 内部存储简介:

          • 只有非静态成员变量属于类的实例化对象上
          • 静态成员变量本质上仅有一份,不属于任何特定对象,当然也不占用本对象内存
          • 静态成员函数和非静态成员函数均不占用本对象内部空间,不属于特定的对象
        2. 代码示例:

          class Person
          {
              
          	int a;
          	static int b;
          	void func(){}
          	static void func(){}
          };
          void test01()
          {
          	Person p;
          	cout << sizeof(p) << endl;   // output->1
              
          	
          }
          void test02()
          {
          	Person p;  
          	cout << sizeof(p) << endl;   //output->4
          	
              
          }
          
        六、面向对象之This指针
        1. this指针简介:

          • this指针指向被调用函数所属的对象
          • this指针隐含在每一个非静态成员函数的内部,不需要定义,直接使用即可
        2. this指针应用场景:

          • 解决形参与实参名称冲突问题
          • 返回对象本身
        3. 代码示例:

          class Person
          {
          public:
          	int age;
          	Person(int age)
          	{
                  
          		this->age = age;
          	}
          	Person& PersonAddage(Person& p)
          	{
          		this->age += p.age;
          		
                  return *this;   
          	}
          };
          void test01()
          {
          	Person p(10);
          	Person p2(10);
          	
          	p2.PersonAddage(p).PersonAddage(p).PersonAddage(p);
          	cout << p2.age << endl;	
          }
          

          Tips:

          PersonAddage函数的返回值方式会影响

          p2.PersonAddage(p).PersonAddage(p).PersonAddage(p);
          

          代码的输出:

          • 假设以引用的方式返回,每次返回对象引用,即可以完成本对象的年龄age叠加
          • 假设以值的方式返回,每次会多次调用拷贝构造函数,创建诸多副本,无法完成原始对象年龄的多次叠加
        七、空指针访问成员函数
        1. 内容简介:

          创建Person对象指向空指针NULL,则内置的this指针指向为空,因此程序运行过程中代码会崩溃,为提高程序健壮性,可以设置检测代码,避免程序崩溃。

        2. 代码示例:

          class Person
          {
          public:
          	void showclassName()
          	{
          		cout << "this is Person class" << endl;
          	}
          	void showPersonage()
          	{
                  
          		if (this == NULL)
          		{
          			return;
          		}
          		cout << age << endl;
          	}
          public:
          	int age;
          };
          void test01()
          {
          	Person* p = NULL; // this指针未指向任何对象(Person指针为NULL)
          	
              p->showclassName();
              
              
          	p->showPersonage();
          }
          
        八、面向对象class的const修饰符
        1. const修饰简介:

          • const修饰成员函数,为常函数,常函数内不能修改成员属性,如果想在常函数内修改成员属性,需要添加mutable修饰符
          • const修饰class对象,常对象仅能调用常函数
        2. 代码示例:

          class Person
          {
          	
          	
          public:
              
          	void showPerson() const
          	{
                  
          		//a = 100;   //failed
          		b = 100; 
          	}
          	void func(){}
          public:
          	int a;
          	mutable int b;
          };
          
          void test01()
          {
          	
          	const Person p{};
          
              
          	// p.a = 100;   //failed
          	p.b = 100;
          
          	
          	p.showPerson();
          	//p.func();   //failed
          }
          
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/290274.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号