动态数组类的操作符重载
要求
设计一个数组类,其成员变量包括数数组元素个数和数组变量;
然后定义成员函数包括构造函数、析构函数以及数组显示函数、数组排序函数和两个数组的加法函数;最后在主函数中定义数组类的对象,并调用这些函数显示数组、对一个数组进行排序以及计算两个数组的加。
代码
//cpp实现数组类Array
#include<iostream>
#include<string>
using namespace std;
#pragma warning(disable:4996)//防止安全报错
/******************************************/
class Array
{
private:
string name;
int count;//个数
int* data;
public:
Array();//默认构造函数
Array(int);//声明大小的构造函数
Array(string name, const int* data, int count);//构造函数
Array(string a, int count);
~Array();//析构函数
void print();//显示数组
void order();//数组排序
Array add(const Array& a);//加法
friend ostream& operator<<(ostream& out, const Array& a);
friend istream& operator>>(istream& in, const Array& a);
Array operator+(const Array& a);
};
Array::Array()
{
name = "";
count = 0;
data = NULL;
}
Array::Array(int count)
{
name = "";
this->count = count;
this->data = new int[this->count];
}
Array::Array(string name, const int* data, int count)//构造函数
{
this->name = name;
this->count = count;
this->data = new int[this->count];//分配空间
for (int i = 0; i < this->count; i++)
{
this->data[i] = data[i];
}//赋值
}
Array::Array(string name, int count)//构造函数
{
this->name = name;
this->count = count;
this->data = new int[this->count];//分配空间
}
Array::~Array()
{
if (data != NULL)
{
delete[] data;
count = 0;
}
}
void Array::print()
{
cout << name << ':';
for (int i = 0; i < count; i++)
cout << data[i] << " ";
cout << endl;
}
Array Array::add(const Array& a)
{
//两个数组的长度可能不一样
Array temp;
if (count >= a.count)
temp.count = count;
else
temp.count = a.count;
temp.data = new int[temp.count];
temp.name = name + "+" + a.name;
int max = (count > a.count ? count : a.count);//长数组的长度
int min = (count <= a.count ? count : a.count);//短数组的长度
for (int i = 0; i < min; i++)
temp.data[i] = this->data[i] + a.data[i];
int i = min;
for (; i < count; i++)
temp.data[i] = data[i];
for (; i < a.count; i++)
temp.data[i] = a.data[i];//这两个具体不清楚哪个长
return temp;
}
void Array::order()
{
int i, j;
for (int i = 0; i < count-1; i++)
{
for (int j = 0; j < count - 1 - i; j++)
{
int temp;
if (data[j] > data[j + 1])
{
temp = data[j];
data[j] = data[j + 1];
data[j + 1] = temp;
}
}
}
}
ostream& operator<<(ostream& out, const Array& a)
{
out << a.name << ":";
for (int i = 0; i < a.count; i++)
out << a.data[i] << " ";
out << endl;
return out;
}
istream& operator>>(istream& in, const Array& a)
{
for (int i = 0; i < a.count; i++)
in >> a.data[i];
return in;
}
Array Array:: operator+(const Array& a)
{
Array temp = add(a);
return temp;//直接把add复制过来也行,我这里是调用的
}
/****************************************************************/
int main()
{
cout << "请输入数组长度和数据" << endl;
int n;
cin >> n;
Array a(n);
cin >> a;
cout << a;//声明一个长度为n的数组,并输入输出
/******************************/
cout << "请输入数组名字和长度及数据" << endl;
string name;
cin >> name;
cin >> n;
Array b(name, n);
cin >> b;
cout << b;
cout << "排序后:" << endl;
b.order();
cout << b;
return 0;
}
结果
流式输入输出操作符的重载
- 使用友元函数实现
>>的重载
//数组类重载流式输入符
istream& operator>>(istream& in,Array &a)
{
for (int i = 0; i < a.count; i++)
in >> a.data[i];
return in;
}
//声明时用友元法
friend istream& operator>>(istream& in, const Array& a);
//调用时
cin>>array;//输入一个数组类的对象
<<的重载
//数组类重载流式输出符
ostream& operator <<(ostrea& out,const Array& a)
{
out<<array.name<<":";
for(int i=0;i<a.count;i++)
out<<a.data[i]<<" ";
out<<endl;
return out;
}
//类内声明时
friend ostream& operator<<(ostream& out, const Array& a);
//调用时
cout<<array;//输出该类的对象
总结
学会了友元函数方法重载流式输入输出操作符,复习了排序算法,虽然仅仅写了最简单的冒泡排序,但是顺便把别的算法又重新理解记忆了一遍;
- 冒泡排序
- 选择排序
- 插入排序
- 希尔排序
- 快速排序
- 归并排序
- 堆排序
- 基数排序
看了看这个博客