5.C++特殊成员

  1. const成员

    1. const数据成员

      • const类型变量是不可以修改,只读模式

      • 必须采用初始化参数列表方式进行初始化

    2. const成员函数

      • 写法上, const写在函数后面

      • 常成员函数是不能够修改数据成员,只读数据成员

      • 常成员函数可以与普通函数同时存在

        • 普通函数和常成员函数相同时,普通对象优先调用普通函数

        • 普通对象可以调用常成员函数

    3. const对象: const修饰的对象

      • 常对象只能调用常成员函数

    4. #include<iostream>
      #include <string>
      using namespace std;
      
      class Heroes {
      public:
      	Heroes(string name, int levels) :m_levels(levels) {//必须要用初始化参数列表方式初始化
      		m_name = name;
      		//m_levels = levels; //错误,该变量是只读模式,不能修改
      	}
      	void print() {
      		cout << "普通函数" << endl;
      		cout << m_name << " " << m_levels << endl;
      	}
      	//常成员函数
      	void print()const {
      		cout << "常成员函数" << endl;
      		cout << m_name << " " << m_levels << endl;
      	}
      	void printData() {
      		cout << "普通函数" << endl;
      		cout << m_name << " " << m_levels << endl;
      	}
      
      protected:
      	string m_name;
      	const int m_levels;
      private:
      
      };
      
      int main() {
      	Heroes Mid("辛德拉",16);
      	Mid.print();//普通对象调用普通函数
      	const Heroes Top("盖伦",17);
      	Top.print();//常对象调用常成员函数
      	//Top.printData();//常对象只能调用常成员函数
      	return 0;
      }

  2. static成员

    1. static成员是不属于对象,是属于类的,意味着是所有对象共有的,调用可以不需要对象,当然你可以用对象调用。

    2. static成员依旧受权限限定。

    3. static数据成员

      • 必须在类外初始化,不再需要static修饰,但是需要类名限定

      • 类中初始化是错误的,不能采用初始化参数列表方式初始化

    4. static成员函数

      • static写在函数前面即可

      • 调用非静态成员必须要指定对象

    5. static对象

      • 释放是最后释放的

    6. #include<iostream>
      #include <string>
      using namespace std;
      
      class Heroes {
      public:
      	static int m_levels;
      	Heroes(string name="") :m_name(name) {
      		m_levels++;
      	}
      	static void print() {
      		//调用非静态数据成员,必须要指定对象
              //cout << m_name <<endl; 当这个函数不采用对象去调用,name没有来源
              //静态调用静态,没什么要求
      		cout << m_levels << endl;
      		cout << "静态成员函数" << endl;
      	}
      	static void printData(const Heroes& hero);
      
      protected:
      	string m_name;
      private:
      
      };
      //类外初始化,不再需要static修饰,但是需要类名限定
      int Heroes::m_levels = 0;
      //类外初始化,不再需要static修饰
      void Heroes::printData(const Heroes& hero) {
      	cout << hero.m_name << " " << m_levels/*mm.m_levels*/ << endl;
      }
      
      int main() {
      	//静态数据成员访问,可以不需要对象
      	cout << Heroes::m_levels << endl;
      	Heroes Mid("辛德拉");
      	//静态数据成员可以通过对象去访问
      	cout << Mid.m_levels << endl;
      	Heroes array[3];			     
      	Heroes* p = new Heroes("盖伦");	 
      	cout << Heroes::m_levels << endl;
      	cout << p->m_levels << endl;
      	cout << Mid.m_levels << endl;
      	delete p;
      	p = nullptr;
      	//静态成员函数
      	Heroes::print();
      	Mid.print();
      	Heroes::printData(Mid);
      	return 0;
      }
  3. 友元

    1. 友元? friend描述的关系。友元只是提供一个场所,赋予对象具有打破类的权限(无视权限)

    2. 友元函数

      • 普通友元函数

      • 以另一个类的成员函数充当友元函数,顺序如下:

        • B 类

        • A类

        • A类的友元函数(B类的成员函数)

    3. #include<iostream>
      #include <string>
      using namespace std;
      void printData();
      class Heroes {
      public:
      	
      	Heroes(string name = "",int levels=0) :m_name(name),m_levels(levels) {
      	}
      	void print(){
      		cout << m_name << " " << m_levels << endl;
      	}
      
      	friend void printData() {
      		//不属于类,不能直接访问成员
      		//cout << m_name << " " << m_levels << endl;
      		Heroes Top("盖伦", 17);
      		//友元函数提供一个场所,让对象无视权限
      		cout << Top.m_name << " " << Top.m_levels << endl;
      	}
      	friend void printData1(const Heroes&hero);
      
      protected:
      	string m_name;
      	int m_levels;
      private:
      
      };
      //不需friend修饰,不需要类名限定
      void printData1(const Heroes& hero) {
      	cout << hero.m_name << " " << hero.m_levels << endl;
      }
      //B以A中的函数作为友员函数
      class A
      {
      public:
      	void printB();
      protected:
      private:
      };
      class B {
      public:
      	B(string name="张三") :m_name(name) {
      
      	}
      	friend void A::printB();
      protected:
      	string m_name;
      private:
      };
      void A::printB() {
      	B b;
      	cout << b.m_name << endl;
      }
      
      int main() {
      	Heroes Jug("赵信",15);
      	Jug.print();
      	printData();
      	printData1(Jug);
      	A a;
      	a.printB();
      
      	return 0;
      }
    4. 友元类

      • 友员类无视权限

      • #include<iostream>
        #include <string>
        using namespace std;
        void printData();
        class Heroes {
        public:
        	friend class Heroines;
        	Heroes(string name = "", int levels = 0) :m_name(name), m_levels(levels) {
        	}
        	void print() {
        		cout << m_name << " " << m_levels << endl;
        	}
        protected:
        	string m_name;
        	int m_levels;
        private:
        
        };
        
        class Heroines
        {
        public:
        	void print()
        	{
        		Heroes Mid(" 劫 ", 18);
        		cout << Mid.m_name << " " << Mid.m_levels << endl;
        	}
        	void print1(Heroes& hero)
        	{
        		cout << hero.m_name << " " << hero.m_levels << endl;
        	}
        	Heroes& returnHero(Heroes& hero)
        	{
        		return hero;
        	}
        protected:
        };
        //互为友元类的写法
        class A
        {
        	friend class B;
        public:
        	void printData();
        protected:
        	string data = "A";
        };
        class B
        {
        public:
        	friend class A;
        	void printData()
        	{
        		A a;
        		cout << a.data << endl;
        	}
        protected:
        	string data = "B";
        };
        void A::printData()
        {
        	B b;
        	cout << b.data << endl;
        }
        
        int main() {
        	Heroes Jug("赵信", 15);
        	Jug.print();
        	Heroines Sup;
        	Sup.print();
        	Sup.print1(Jug);
        	//Sup.returnHero().m_name;//错误,出了友元类,没有权限
        	B b;
        	b.printData();
        	A a;
        	a.printData();
        	return 0;
        }

  4. this指针与explicit

    1. explicit修饰构造函数使用,不让隐式转换构造

    2. this指针

      • 避免形参名和数据成员同名,通指对象的地址

      • 充当函数返回值,返回对象自身,用*this表示对象本身

      • 静态成员函数中是不能使用this指针

    3. #include <iostream>
      using namespace std;
      class Heroes
      {
      public:
      	explicit Heroes(string name="", int levels=0) :name(name), levels(levels) {}
      	void print()
      	{
      		cout << name << " " << levels << endl;
      	}
      protected:
      	string name;
      	int levels;
      };
      
      class Heroines
      {
      public:
      	Heroines(string name = "", int levels = 0){
      		this->levels = levels;//Heroines::levels=levels;
      		this->name = name;//Heroines::name=name;	
      	}
      	void print()
      	{
      		cout << name << " " << levels << endl;
      	}
      	void printThis()
      	{
      		cout << this << endl;
      	}
      	Heroines& returnThis()
      	{
      		return *this;
      	}
      protected:
      	string name;
      	int levels;
      };
      
      int main() {
      	//explicit 不让隐式转换构造
      	//Heroes Top = { "盖伦",17 };
      	Heroes Top("盖伦",17 );
      	Top.print();
      
      	Heroines Mid("辛德拉",18);
      	Mid.print();
      	Mid.printThis();
      	cout << &Mid << endl;
      
      	return 0;
      }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值