C++ Special Members

  1. const member

    1. const data member

      • const type variable is not modifiable, read-only mode

      • Initialization must be done in a list of initialization parameters

    2. const member function

      • In writing, const is written after a function

      • A constant member function is a read-only data member that cannot be modified

      • Constant member functions can coexist with ordinary functions

        • Ordinary object calls ordinary function first when normal function and constant member function are the same

        • Normal objects can call constant member functions

    3. Const object: object decorated by const

      • A constant object can only call a constant member function

    4. #include<iostream>
      #include <string>
      using namespace std;
      
      class Heroes {
      public:
      	Heroes(string name, int levels) :m_levels(levels) {//Must be initialized with an initialization parameter list
      		m_name = name;
      		//m_levels = levels; // Error, the variable is read-only and cannot be modified
      	}
      	void print() {
      		cout << "General function" << endl;
      		cout << m_name << " " << m_levels << endl;
      	}
      	//Constant member function
      	void print()const {
      		cout << "Constant member function" << endl;
      		cout << m_name << " " << m_levels << endl;
      	}
      	void printData() {
      		cout << "General function" << endl;
      		cout << m_name << " " << m_levels << endl;
      	}
      
      protected:
      	string m_name;
      	const int m_levels;
      private:
      
      };
      
      int main() {
      	Heroes Mid("Syndra",16);
      	Mid.print();//Normal object calls normal function
      	const Heroes Top("Galen",17);
      	Top.print();//Constant object calls a constant member function
      	//Top.printData();// A constant object can only call a constant member function
      	return 0;
      }

  2. static member

    1. static members are not objects and belong to classes, which means they are common to all objects. Calls can be made without objects, of course you can call with objects.

    2. static members are still restricted by permissions.

    3. static data members

      • Must be initialized out of class, no static modifications are required, but class name qualification is required

      • Initialization in a class is incorrect and cannot be initialized as a list of initialization parameters

    4. static member function

      • static is written in front of the function

      • Invoking a non-static member must specify an object

    5. static object

      • Release is final

    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() {
      		//To invoke a non-static data member, you must specify an object
              //Cout << m_ Name < endl; When this function is called without an object, name has no source
              //Static call static, no requirement
      		cout << m_levels << endl;
      		cout << "Static member function" << endl;
      	}
      	static void printData(const Heroes& hero);
      
      protected:
      	string m_name;
      private:
      
      };
      //Out-of-class initialization, static modification is no longer required, but class name qualification is required
      int Heroes::m_levels = 0;
      //Out-of-class initialization, no longer requires static modifications
      void Heroes::printData(const Heroes& hero) {
      	cout << hero.m_name << " " << m_levels/*mm.m_levels*/ << endl;
      }
      
      int main() {
      	//Static data member access, objects may not be required
      	cout << Heroes::m_levels << endl;
      	Heroes Mid("Syndra");
      	//Static data members can be accessed through objects
      	cout << Mid.m_levels << endl;
      	Heroes array[3];			     
      	Heroes* p = new Heroes("Galen");	 
      	cout << Heroes::m_levels << endl;
      	cout << p->m_levels << endl;
      	cout << Mid.m_levels << endl;
      	delete p;
      	p = nullptr;
      	//Static member function
      	Heroes::print();
      	Mid.print();
      	Heroes::printData(Mid);
      	return 0;
      }
  3. Friends

    1. Friends? The relationship described by friend. Friends simply provide a place to give objects the privilege to break classes (regardless of privileges)

    2. friend function

      • Ordinary friend function

      • Use member functions of another class as friend functions in the following order:

        • Class B

        • Class A

        • Friend function of class A (member function of class 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() {
      		//Not a class, no direct access to members
      		//cout << m_name << " " << m_levels << endl;
      		Heroes Top("Galen", 17);
      		//Friend functions provide a place for objects to ignore permissions
      		cout << Top.m_name << " " << Top.m_levels << endl;
      	}
      	friend void printData1(const Heroes&hero);
      
      protected:
      	string m_name;
      	int m_levels;
      private:
      
      };
      //No friend modifiers required, no class name qualifications required
      void printData1(const Heroes& hero) {
      	cout << hero.m_name << " " << hero.m_levels << endl;
      }
      //B uses functions in A as friend functions
      class A
      {
      public:
      	void printB();
      protected:
      private:
      };
      class B {
      public:
      	B(string name="Zhang San") :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("Zhao Xin",15);
      	Jug.print();
      	printData();
      	printData1(Jug);
      	A a;
      	a.printB();
      
      	return 0;
      }
    4. friend class

      • Friends ignore permissions

      • #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(" Hijacking ", 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:
        };
        //Writing of mutual friend classes
        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("Zhao Xin", 15);
        	Jug.print();
        	Heroines Sup;
        	Sup.print();
        	Sup.print1(Jug);
        	//Sup.returnHero().m_name; // Error, out of friend class, no permission
        	B b;
        	b.printData();
        	A a;
        	a.printData();
        	return 0;
        }

  4. this pointer and explicit

    1. explicit modifier constructor used to prevent implicit conversion constructs

    2. this pointer

      • Avoid having formal and data members with the same name, referring generally to the address of the object

      • Acts as the function return value, returns the object itself, and represents the object itself with *this

      • this pointer cannot be used in static member functions

    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 does not allow implicit conversion constructs
      	//Heroes Top = {Galen, 17};
      	Heroes Top("Galen",17 );
      	Top.print();
      
      	Heroines Mid("Syndra",18);
      	Mid.print();
      	Mid.printThis();
      	cout << &Mid << endl;
      
      	return 0;
      }

Tags: C++

Posted on Wed, 24 Nov 2021 17:01:19 -0500 by ozman26