【C++】类的封装
文章目录
前言
本篇文章就类的封装,讲到数组类封装,字符串类封装。
一、数组类封装
设计类 myArray
属性
int m_Capacity数组容量
int m_Size 数组大小
int pAddress 维护真实在堆区创建的数组指针
行为
默认构造
有参构造
拷贝构造
析构
根据位置 设置数据
根据位置 获取数据
尾插
获取数组容量
获取数组大小
MyArray.h
cpp
#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
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
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
cpp
#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
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
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;
}
总结
到这里这篇文章的内容就结束了,谢谢大家的观看,如果有好的建议可以留言喔,谢谢大家啦!