• 指针权限,new与delete,类与对象,函数模板,类模板的用法


    指针权限 用法

    void Print(const char* SecretPointer)
    {
    	cout << "绝密指令为:";
    	cout << SecretPointer << endl;
    }
    
    void Change(int& number, int* const FixedPointer)
    {
    	cout << "更换站台数字为:";
    	cin >> number;
    	cout << *FixedPointer << endl;
    }
    
    int main()
    {
    	char* SecretPointer_01 = (char*)malloc(sizeof(char) * 20);
    	cout << "请输入绝密指令:";
    	cin >> SecretPointer_01;
    	const char* SecretPointer = SecretPointer_01;
    	Print(SecretPointer);
    
    	int number = 100;
    	int* const FixedPointer = &number;
    	cout << "原站台数字为:" << *FixedPointer << endl;
    	Change(number, FixedPointer);
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    new与delete 用法

    #include 
    using namespace std;
    
    int* CreateSpace1(int*&  PointerOfSum1)
    {
    	PointerOfSum1 = new int(0);     // 1 
    	return new int[5]{ 1,2,3,4,5 };     //  2
    }
    
    void AddArray1(int*& PointerOfSum1, int*& PointerOfArray1)
    {
    	for (int i = 0; i < 5; i++)
    	{
    		(*PointerOfSum1) += (*(PointerOfArray1 + i));
    	}
    }
    
    // 
    
    class StupidPerson
    {
    public:
    	StupidPerson(int IQ = 10)
    		:_IQ(IQ)
    	{}
    	int GetIQ()
    	{
    		return _IQ;
    	}
    private:
    	int _IQ;
    };
    
    StupidPerson* CreateSpace2(int*& PointerOfSum2)
    {
    	PointerOfSum2 = new int(0);
    	return new StupidPerson[5];    // 3
    }
    
    void AddArray2(int*& PointerOfSum2, StupidPerson*& PointerOfArray2)
    {
    	for (int i = 0; i < 5; i++)
    	{
    		(*PointerOfSum2) += ((*(PointerOfArray2 + i)).GetIQ());
    	}
    }
    
    //
    
    class SmartPerson
    {
    public:
    	SmartPerson(int IQ = 100)
    		:_IQ(IQ)
    	{}
    	~SmartPerson()
    	{
    		_IQ = 0;
    	}
    	int GetIQ()
    	{
    		return _IQ;
    	}
    private:
    	int _IQ;
    };
    
    SmartPerson* CreateSpace3(int*& PointerOfSum3)
    {
    	PointerOfSum3 = new int(0);
    	return new SmartPerson[5]{ SmartPerson(),SmartPerson(101),SmartPerson(102) ,SmartPerson(99) ,SmartPerson(107) };   // 4
    }
    
    void AddArray3(int*& PointerOfSum3, SmartPerson*& PointerOfArray3)
    {
    	for (int i = 0; i < 5; i++)
    	{
    		(*PointerOfSum3) += ((*(PointerOfArray3 + i)).GetIQ());
    	}
    }
    
    int main()
    {
    	int* PointerOfSum1 = nullptr;
    	int* PointerOfArray1 = CreateSpace1(PointerOfSum1);
    	AddArray1(PointerOfSum1, PointerOfArray1);
    	cout << "总和为:" << (*PointerOfSum1) << endl;
    	delete PointerOfSum1;   // 5
    	delete[] PointerOfArray1;    //  6
    	//
    	int* PointerOfSum2 = nullptr;
    	StupidPerson* PointerOfArray2 = CreateSpace2(PointerOfSum2);
    	AddArray2(PointerOfSum2, PointerOfArray2);
    	cout << "IQ总和为:" << (*PointerOfSum2) << endl;
    	delete PointerOfSum2;
    	delete[] PointerOfArray2;
    	//
    	int* PointerOfSum3 = nullptr;
    	SmartPerson* PointerOfArray3 = CreateSpace3(PointerOfSum3);
    	AddArray3(PointerOfSum3, PointerOfArray3);
    	cout << "IQ总和为:" << (*PointerOfSum3) << endl;
    	delete PointerOfSum3;
    	delete[] PointerOfArray3;
    	//
    	SmartPerson* ps = (SmartPerson*)malloc(sizeof(SmartPerson) * 10);
    	cout << "内存池已经创建完成!" << endl;
    	new(ps)SmartPerson(110);    //  7
    	new(ps + 1)SmartPerson(103);
    	cout << "第一个人智商为:" << ps->GetIQ() << endl;
    	cout << "第二个人智商为:" << (ps + 1)->GetIQ() << endl;
    	ps->~SmartPerson();   // 8
    	(ps + 1)->~SmartPerson();
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114

    类与对象 用法

    test.h
    
    #include 
    using namespace std;
    
    class Country
    {
    	friend class Citizen;
    	friend istream& operator>>(istream& cin, Country& country);
    	friend ostream& operator<<(ostream& cout, const Country& country);
    public:
    	//构造函数
    	Country();
    	//拷贝构造函数
    	Country(const Country& other);
    	//析构函数
    	~Country();
    	//赋值运算符重载
    	Country& operator=(const Country& other);
    private:
    	char* _countryname;
    	char* _capital;
    };
    //流插入运算符重载
    istream& operator>>(istream& cin, Country& country);
    //流提取运算符重载
    ostream& operator<<(ostream& cout, const Country& country);
    
    //
    
    class Citizen
    {
    	friend int operator>(const Citizen& c1, const Citizen& c2);
    	friend istream& operator>>(istream& cin, Citizen& citizen);
    	friend ostream& operator<<(ostream& cout, const Citizen& citizen);
    public:
    	//构造函数
    	explicit Citizen(bool iscriminal = false);
    	//拷贝构造函数
    	Citizen(const Citizen& other);
    	//析构函数
    	~Citizen();
    	//赋值运算符重载
    	Citizen& operator=(const Citizen& other);
    	//前置++运算符重载
    	Citizen& operator++();
    	//后置++运算符重载
    	Citizen operator++(int);
    	//普通运算符重载
    	Citizen& operator-(int num);
    	//静态成员函数
    	static int GetTotalCitizen();
    	//友元类的跨类访问
    	void GetCountry();
    private:
    	//成员变量
    	char* _personalname;
    	int _age;
    	Country _nationality;
    	bool _iscriminal;
    	int _creditscore;
    	const int _maxcreditscore;
    	//类的静态区成员变量
    	static int _totalcitizen;
    };
    //普通运算符重载
    int operator>(const Citizen& c1, const Citizen& c2);
    //流插入运算符重载
    istream& operator>>(istream& cin, Citizen& citizen);
    //流提取运算符重载
    ostream& operator<<(ostream& cout, const Citizen& citizen);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    //func.cpp
    
    #include "test.h"
    
    //Country类相关
    Country::Country()
    {
    	_countryname = new char[20]{ '\0' };
    	_capital = new char[20]{ '\0' };
    }
    
    Country::Country(const Country& other)
    {
    	_countryname = new char[20]{ '\0' };
    	_capital = new char[20]{ '\0' };
    	memcpy(_countryname, other._countryname, sizeof(char) * 20);
    	memcpy(_capital, other._capital, sizeof(char) * 20);
    }
    
    Country::~Country()
    {
    	delete[] _countryname;
    	delete[] _capital;
    }
    
    Country& Country::operator=(const Country& other)
    {
    	if (&other != this)
    	{
    		memcpy(_countryname, other._countryname, sizeof(char) * 20);
    		memcpy(_capital, other._capital, sizeof(char) * 20);
    	}
    	return *this;
    }
    
    istream& operator>>(istream& cin, Country& country)
    {
    	cin >> country._countryname >> country._capital;
    	return cin;
    }
    
    ostream& operator<<(ostream& cout, const Country& country)
    {
    	cout << country._countryname << " " << country._capital;
    	return cout;
    }
    
    
    //Citizen类相关
    Citizen::Citizen(bool iscriminal)
    	:_age(0)
    	,_nationality()
    	,_iscriminal(iscriminal)
    	,_creditscore(0)
    	,_maxcreditscore(100)
    {
    	_personalname = new char[20]{ '\0' };
    	_totalcitizen++;
    }
    
    Citizen::Citizen(const Citizen& other)
    	:_age(other._age)
    	, _nationality(other._nationality)
    	, _iscriminal(other._iscriminal)
    	, _creditscore(other._creditscore)
    	, _maxcreditscore(other._maxcreditscore)
    {
    	_personalname = new char[20]{ '\0' };
    	memcpy(_personalname, other._personalname, sizeof(char) * 20);
    	_totalcitizen++;
    }
    
    Citizen::~Citizen()
    {
    	delete[] _personalname;
    	_totalcitizen--;
    }
    
    Citizen& Citizen::operator=(const Citizen& other)
    {
    	if (&other != this)
    	{
    		_age = other._age;
    		_nationality = other._nationality;
    		_iscriminal = other._iscriminal;
    		_creditscore = other._creditscore;
    		memcpy(_personalname, other._personalname, sizeof(char) * 20);
    	}
    	return *this;
    }
    
    Citizen& Citizen::operator++()
    {
    	if (_creditscore < _maxcreditscore)
    	{
    		_creditscore++;
    	}
    	return *this;
    }
    
    Citizen Citizen::operator++(int)
    {
    	Citizen tmp(*this);
    	if (_creditscore < _maxcreditscore)
    	{
    		_creditscore++;
    	}
    	return tmp;
    }
    
    Citizen& Citizen::operator-(int num)
    {
    	if (_creditscore > 0)
    	{
    		_creditscore -= num;
    		if (_creditscore < 0)
    		{
    			_creditscore = 0;
    		}
    	}
    	return *this;
    }
    
    int operator>(const Citizen& c1, const Citizen& c2)
    {
    	if (c1._creditscore > c2._creditscore)
    	{
    		return 1;
    	}
    	else if (c1._creditscore < c2._creditscore)
    	{
    		return 2;
    	}
    	else
    	{
    		return 0;
    	}
    }
    
    istream& operator>>(istream& cin, Citizen& citizen)
    {
    	cin >> citizen._personalname >> citizen._age >> citizen._nationality
    		>> citizen._iscriminal >> citizen._creditscore;
    	return cin;
    }
    
    ostream& operator<<(ostream& cout, const Citizen& citizen)
    {
    	cout << citizen._personalname << " " << citizen._age << " " << citizen._nationality
    		<< " " << citizen._iscriminal << " " << citizen._creditscore;
    	return cout;
    }
    
    int Citizen::GetTotalCitizen()
    {
    	return _totalcitizen;
    }
    
    void Citizen::GetCountry()
    {
    	cout << _nationality._countryname << " " << _nationality._capital << endl;
    }
    
    //类的静态区成员变量定义
    int Citizen::_totalcitizen = 0;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    //test.cpp
    
    #include "test.h"
    
    int main()
    {
    	Citizen Shen(0);
    	cin >> Shen;
    	cout << Shen << endl;
    
    	Citizen Jun(Shen);
    	cout << Jun << endl;
    
    	Citizen Elon(0);
    	cin >> Elon;
    	cout << Elon << endl;
    
    	cout << "此时人数:" << Citizen::GetTotalCitizen() << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    函数模板 用法

    // test.h
    
    #include 
    using namespace std;
    
    template <typename T>
    void Swap(T& a, T& b)
    {
    	T tmp = a;
    	a = b;
    	b = tmp;
    }
    
    template <typename T1, typename T2, typename T3>
    void Print(const T1& a, const T2& b, const T3& c)
    {
    	cout << a << " " << b << " " << c << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    // test.cpp
    #include "test.h"
    
    int main()
    {
    	int x = 1;
    	int y = 2;
    	char m = 'x';
    	char n = 'y';
    	cout << "交换前:" << endl;
    	cout << x << " " << y << endl;
    	cout << m << " " << n << endl;
    	Swap(x, y);
    	Swap(m, n);
    	cout << "交换后:" << endl;
    	cout << x << " " << y << endl;
    	cout << m << " " << n << endl;
    	//
    	const char* pointer = "根据ASCII码表对应的(值/字符)为";
    	char obj1 = 'A';
    	int obj2 = 65;
    	Print(obj1, pointer, obj2);
    	Print<int, const char*, char>(obj1, pointer, obj2);     
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    类模板 用法

    // test.h
    #include 
    using namespace std;
    
    template <typename T>
    class Person
    {
    	friend istream& operator>>(istream& cin, Person<T>& person)
    	{
    		cin >> person._name >> *(person._luckysymbol);
    		return cin;
    	}
    	friend ostream& operator<<(ostream& cout, Person<T>& person)
    	{
    		cout << person._name << " " << *(person._luckysymbol);
    		return cout;
    	}
    public:
    	Person();
    	Person(const Person& other);
    	~Person();
    	Person& operator=(const Person& other);
    private:
    	char* _name;
    	T* _luckysymbol;
    };
    
    
    template <typename T>
    Person<T>::Person()
    {
    	_name = new char[20]{ 0 };
    	_luckysymbol = new T(0);
    }
    
    template <typename T>
    Person<T>::Person(const Person& other)
    	:_name(new char[20]{ 0 })
    	, _luckysymbol(new T(0))
    {
    	memcpy(_name, other._name, sizeof(char) * 20);
    	*_luckysymbol = *(other._luckysymbol);
    }
    
    template <typename T>
    Person<T>::~Person()
    {
    	delete _luckysymbol;
    	delete[] _name;
    }
    
    template <typename T>
    Person<T>& Person<T>::operator=(const Person& other)
    {
    	if (&other != this)
    	{
    		memcpy(_name, other._name, sizeof(char) * 20);
    		*_luckysymbol = *(other._luckysymbol);
    	}
    	return  *this;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    // test.cpp
    #include "test.h"
    
    int main()
    {
    	Person<int> Shen;
    	Person<char> Hu;
    	Person<double> Ye;
    	cin >> Shen >> Hu >> Ye;
    	Person<int> Junyang(Shen);  
    	Person<char> Yao;   
    	Yao = Hu;   
    	cout << Shen << endl;
    	cout << Hu << endl;
    	cout << Ye << endl;
    	cout << Junyang << endl;
    	cout << Yao << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
  • 相关阅读:
    MySq修改配置文件
    Chapter 8 Intermediate Shell Tools II
    STM32 cubemx hal库huart串口接收不到第一帧数据或数据全为0的问题
    多语言商城系统哪个好 三大知名跨境电商厂商对比
    数据结构——图の选择题整理
    这几个点让我买了Watch Ultra
    在Android上JAVA的file.list不完整没有压缩包文件
    composer selfupdate或composer self-update不管用解决办法
    【刷题记录⑩】Java工程师丨字节面试真题(四)
    前端进击笔记第五节 JavaScript 如何实现继承?
  • 原文地址:https://blog.csdn.net/Elon_520/article/details/132789611