当前位置: 首页> 文旅> 旅游 > 福田庆三鞠婧祎_小程序开发制作服务商_网络推广专员是做什么的_seo短视频网页入口引流下载

福田庆三鞠婧祎_小程序开发制作服务商_网络推广专员是做什么的_seo短视频网页入口引流下载

时间:2025/7/16 23:59:58来源:https://blog.csdn.net/2301_80035097/article/details/142799307 浏览次数:0次
福田庆三鞠婧祎_小程序开发制作服务商_网络推广专员是做什么的_seo短视频网页入口引流下载

【C++】类的封装


文章目录

  • 【C++】类的封装
  • 前言
  • 一、数组类封装
  • 二、字符串类封装
  • 总结


前言

本篇文章就类的封装,讲到数组类封装,字符串类封装。


一、数组类封装

设计类 myArray
属性
int m_Capacity数组容量
int m_Size 数组大小
int pAddress 维护真实在堆区创建的数组指针
行为
默认构造
有参构造
拷贝构造
析构
根据位置 设置数据
根据位置 获取数据
尾插
获取数组容量
获取数组大小

MyArray.h

#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;class MyArray
{
public:MyArray();  //默认构造 可以给100容量MyArray(int capacity);//有参构造MyArray(const MyArray& arr); //拷贝构造//尾插法void pushBack(int val);//根据位置设置数据void setData(int pos, int val);//根据位置获取数据int getData(int pos);//获取数组容量int getCapcity();//获取数组大小int getSize();//析构~MyArray();//重载[]运算符int& operator[](int index);private:int m_Capacity;  //数组容量int m_Size;  //数组大小int* pAddress;  //真实在堆区开辟的数组的指针
};

MyArray.cpp

#include "myArray.h"MyArray::MyArray()
{cout << "默认构造函数调用" << endl;this->m_Capacity = 100;this->m_Size = 0;this->pAddress = new int[this->m_Capacity];
}MyArray::MyArray(int capacity)
{cout << "有参构造函数调用" << endl;this->m_Capacity = 100;this->m_Size = 0;this->pAddress = new int[this->m_Capacity];
}MyArray::MyArray(const MyArray& arr)
{cout << "拷贝构造函数调用" << endl;this->m_Capacity = arr.m_Capacity;this->m_Size = arr.m_Size;//this->pAddress = arr.pAddress;this->pAddress = new int[this->m_Capacity];for (int i = 0; i < this->m_Size; i++){this->pAddress[i] = arr.pAddress[i];}
}//尾插法
void MyArray::pushBack(int  val)
{this->pAddress[this->m_Size] = val;this->m_Size++;
}//根据位置设置数据
void MyArray::setData(int pos, int val)
{this->pAddress[pos] = val;
}//根据位置获取数据
int MyArray::getData(int pos)
{return this->pAddress[pos];
}//获取数组容量
int MyArray::getCapcity()
{return this->m_Capacity;
}//获取数组大小
int MyArray::getSize()
{return this->m_Size;
}//析构
MyArray::~MyArray()
{if (this->pAddress != NULL){cout << "析构函数调用" << endl;delete[] this->pAddress;this->pAddress = NULL;}
}int& MyArray::operator[](int index)
{return this->pAddress[index];
}

数组类封装.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "myArray.h"void test01()
{MyArray arr;for (int i = 0; i < 10; i++){arr.pushBack(i);}for (int i = 0; i < arr.getSize(); i++){cout << arr.getData(i) << endl;}MyArray arr2(arr);for (int i = 0; i < arr.getSize(); i++){cout << arr2.getData(i) << endl;}arr.setData(0, 1000);cout << "arr 0号位置数据为: " << arr.getData(0) << endl;for (int i = 0; i < 10; i++){cout << arr.getData(i) << endl;}cout << "数组容量为: " << arr.getCapcity() << endl;cout << "数组大小为: " << arr.getSize() << endl;//利用[]方式去索引数组中的元素,可读可写cout << "---------------------" << endl;arr[0] = 10000;cout << arr[0] << endl;
}int main() {test01();system("pause");return EXIT_SUCCESS;
}

二、字符串类封装

myString类 实现自定义的字符串类
属性
char * pString; 维护 在堆区真实开辟的字符数组
int m_Size; 字符串长度
行为
有参构造 MyString(char * str)
拷贝构造 MyString(const MyString & str);
析构 ~MyString();
重载<< 运算符
重载 >> 运算符
重载 = 赋值运算
重载 [] str[0] 按照索引位置设置获取字符
重载 + 字符串拼接
重载 == 对比字符串

MyString.h

#pragma  once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;class MyString
{friend ostream& operator <<(ostream& cout, MyString& str);friend istream& operator >>(istream& cin, MyString& str);public://有参构造MyString(const char* str);//拷贝构造MyString(const MyString& str);//析构~MyString();//重载=运算符MyString& operator=(const char* str);MyString& operator=(const MyString& str);//重载[]运算符char& operator[](int index);//重载+运算符MyString operator+(const char* str);MyString operator+(const MyString& str);//重载==运算符bool operator==(const char* str);bool operator==(const MyString& str);private:char* pString; //维护在堆区开辟的字符数组int m_Size; //字符串长度 不统计\0
};

MyString.cpp

#include "myString.h"//重载左移运算符
ostream& operator <<(ostream& cout, MyString& str)
{cout << str.pString;return cout;
}//重载右移运算符
istream& operator >>(istream& cin, MyString& str)
{//先清空原来堆区数据if (str.pString != NULL){delete[] str.pString;str.pString = NULL;}char buf[1024];  //开辟临时数组 记录用户输入内容cin >> buf;str.pString = new char[strlen(buf) + 1];strcpy(str.pString, buf);str.m_Size = strlen(buf);return cin;
}MyString::MyString(const char* str)
{//cout << "MyString有参构造函数调用" << endl;this->pString = new char[strlen(str) + 1];strcpy(this->pString, str);this->m_Size = strlen(str);
}MyString::MyString(const MyString& str)
{//cout << "拷贝构造函数调用" << endl;this->pString = new char[strlen(str.pString) + 1];strcpy(this->pString, str.pString);this->m_Size = str.m_Size;
}MyString::~MyString()
{if (this->pString != NULL){//cout << "析构调用" << endl;delete[] this->pString;this->pString = NULL;}}//重载=运算符
MyString& MyString::operator=(const char* str)
{//先判断原来堆区释放有内容,如果有先释放if (this->pString != NULL){delete[] pString;this->pString = NULL;}this->pString = new char[strlen(str) + 1];strcpy(this->pString, str);this->m_Size = strlen(str);return *this;
}MyString& MyString::operator=(const MyString& str)
{//先判断原来堆区释放有内容,如果有先释放if (this->pString != NULL){delete[] pString;this->pString = NULL;}this->pString = new char[strlen(str.pString) + 1];strcpy(this->pString, str.pString);this->m_Size = strlen(str.pString);return *this;
}//重载[]运算符
char& MyString::operator[](int index)
{return this->pString[index];
}//重载加号运算符
MyString MyString::operator+(const char* str)
{//本身abc 传入 def//计算开辟内存大小int newSize = this->m_Size + strlen(str) + 1;char* temp = new char[newSize];memset(temp, 0, newSize);strcat(temp, this->pString);strcat(temp, str);MyString newString = temp;delete[] temp;return newString;
}MyString MyString::operator+(const MyString& str)
{//本身abc 传入 def//计算开辟内存大小int newSize = this->m_Size + strlen(str.pString) + 1;char* temp = new char[newSize];memset(temp, 0, newSize);strcat(temp, this->pString);strcat(temp, str.pString);MyString newString = temp;delete[] temp;return newString;
}//重载==运算符
bool MyString::operator==(const char* str)
{return !strcmp(this->pString, str);
}bool MyString::operator==(const MyString& str)
{return !strcmp(this->pString, str.pString);}

字符串类封装.cpp

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
//#include <string>
#include "myString.h"void test01()
{MyString str = "abc";cout << str << endl;cout << "请重新给str赋值:" << endl;cin >> str; cout << "str 新的值为: " << str << endl; MyString str2 = str;cout << "str2 = " << str2 << endl;
}void test02()
{MyString str = "abcd";MyString str2 = "efgh";str2 = str;cout << "str2 = " << str2 << endl;cout << "str2[0] = " << str2[0] << endl;str2[0] = 'z';cout << "str2[0]改为z后输出:" << str2 << endl;MyString str3 = "abc";MyString str4 = "efg";MyString str5 = str3 + str4;MyString str6 = str5 + "ghe";cout << "str5 = " << str5 << endl;cout << "str6 = " << str6 << endl;if (str5 == str6){cout << "str5 == str6" << endl;}else{cout << "str5 != str6" << endl;}if (str6 == "abcefgghe"){cout << "str6 = abcefgghe" << endl;}else{cout << "str6 != abcefgghe" << endl;}}int main() {//test01();test02();system("pause");return EXIT_SUCCESS;
}

总结

到这里这篇文章的内容就结束了,谢谢大家的观看,如果有好的建议可以留言喔,谢谢大家啦!

关键字:福田庆三鞠婧祎_小程序开发制作服务商_网络推广专员是做什么的_seo短视频网页入口引流下载

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: