六十七. 自定义的数组类
第一个文件: MyArray.h
#ifndef MYARRAY_H
#define MYARRAY_H
using namespace std;
class MyArray
{
public:
MyArray();
MyArray(int len);
MyArray(const MyArray & another);
~MyArray();
void setData(int index,int data);
int getData(int index);
int getLen() const;
MyArray& operator= (const MyArray& another);
int& operator[](int index) const;
friend ostream& operator<< (ostream& os, const MyArray & array); //这里我们需要注意,如果左移操作符这里的array是const
//那么它用到的getLen与int& operator[](int index)后面都必须有const
//这里是基于安全级高的const MyArray & array不能在使用中被可能修改。
friend istream& operator>> (istream& is,MyArray& array);
friend bool operator==(MyArray& array1, MyArray& array2);
friend bool operator!=(MyArray& array1, MyArray& array2);
protected:
private:
int len;
int *space;
};
#endif // MYARRAY_H
第二个文件:MyArray. cpp
#include <iostream>
#include "MyArray.h"
using namespace std;
MyArray::MyArray()
{
cout<<" MyArray()..."<<endl;//ctor
this->len=0;
this->space= NULL;
}
MyArray::MyArray(int len)
{
if(len<0){
this->len=0;
return;
}
else{
this->len = len;
this->space= new int[this->len];
cout<<" MyArray::MyArray(int len)..."<<endl;//ctor
}
}
MyArray::MyArray(const MyArray & another)
{
if(another.len >= 0){
this->len = another.len;
//deep copy
this->space= new int[this->len];
for(int i=0; i<this->len;i++){
this->space[i] = another.space[i];
}
cout<<" MyArray::MyArray(const MyArray & another)..."<<endl;//ctor
}
}
MyArray& MyArray::operator=(const MyArray& another)
{
if(this == &another)
{
return *this;
}
if(this->space !=NULL)
{
delete [] this->space;
this->space= NULL;
this->len=0;
}
if(another.len >= 0){
this->len = another.len;
//deep copy
this->space= new int[this->len];
for(int i=0; i<this->len;i++){
this->space[i] = another.space[i];
}
cout<<" MyArray::MyArray(const MyArray & another)..."<<endl;//ctor
}
return *this;
}
MyArray::~MyArray()
{
if(this->space != NULL){//dtor
delete []this->space;
this->space = NULL;
len=0;
cout<<"MyArray::~MyArray()... "<<endl;
}
}
void MyArray::setData(int index, int data)
{
if(this->space !=NULL){
this->space[index] =data;
}
}
int MyArray::getData(int index)
{
return this->space[index];
}
int MyArray::getLen() const
{
return this->len;
}
int& MyArray::operator[](int index) const
{
return this->space[index];
}
ostream& operator<<(ostream& os, const MyArray & array)
{
os<<"Iteration"<<endl;
//array.getLen(); This means getLen(array);
for(int i=0; i<array.getLen();i++){
os<<array[i]<<" ";
}
os<<endl;
os<<"Use << operator";
return os;
}
istream& operator>> (istream& is,MyArray& array)
{
cout<<"Please enter "<<array.getLen()<<" numbers"<<endl;
for(int i=0; i<array.getLen();i++){
cin>>array[i];
}
return is;
}
bool operator==(MyArray& array1, MyArray& array2)
{
if(array1.len != array2.len)
return false;
for(int i=0;i<array1.len;i++){
if(array1.space[i] != array2.space[i])
return false;
}
return true;
}
bool operator!=(MyArray& array1, MyArray& array2)
{
return !(array1 == array2);
}
第三个文件:main.cpp
#include <iostream>
#include "MyArray.h"
using namespace std;
int main()
{
MyArray array1(10);
for(int i=0; i<10;i++){
array1.setData(i,i+10);
}
cout<<"-------------------------"<<endl;
cout<<"array1:"<<endl;
for(int i=0;i<10;i++){
cout<<array1.getData(i)<<" ";
}
cout<<endl;
cout<<"-------------------------"<<endl;
MyArray array2(5);
cin>>array2;
cout<<"array2:"<<endl;
cout<<array2<<" ";
cout<<"-------------------------"<<endl;
cout<<endl;
if(array2 != array1){
cout<<"Not Equal"<<endl;
}
else
cout<<" equal"<<endl;
return 0;
}
六十八. 重载小括号和new-delete操作符
#include <iostream>
using namespace std;
class Sqr
{
public:
Sqr(int a){
this->a=a;
}
int operator()(int value)
{
return value*value;
}
int operator()(int value1, int value2)
{
return value1 * value2;
}
private:
int a;
};
int main()
{
Sqr s(10);
int value= s(2);//这里的意思是求2的平方
//这里是将一个对象,当成一个普通函数来调用
//称这种对象时,仿函数,伪函数
cout<<"value:"<<value<<endl;
cout<<"----------------------"<<endl;
value= s(10,20);
cout<<"value:"<<value<<endl;
return 0;
}
new和delete操作符的重载
#include <iostream>
#include <stdlib.h>
using namespace std;
class A
{
public:
A(int a){
cout<<"A(int a)..."<<endl;
this->a=a;
}
//重载的new操作符,依然会触发构造函数
void* operator new(size_t size){ //void* 是“万能指针”,和void没关系
cout<<"重载了new操作符"<<endl;
return malloc(size);
}
//重载的delete操作符,依然会触发析构函数
void operator delete(void * p )
{
cout<<"重载了delete操作符"<<endl;
if(p != NULL)
{
free(p);
p = NULL;
}
}
~A(){
cout<<"~A()..."<<endl;
}
private:
int a;
};
int main()
{
A *ap=new A(10);
delete ap;
return 0;
}
运行结果:
重载了new操作符
A(int a)...
~A()...
重载了delete操作符
六十九. 重载小括号和new-delete操作符2
#include <iostream>
#include <stdlib.h>
using namespace std;
class A
{
public:
A(int a){
cout<<"A(int a)..."<<endl;
this->a=a;
}
A(){
cout<<"A()..."<<endl;
}
//重载的new操作符,依然会触发构造函数
void* operator new(size_t size){ //void* 是“万能指针”,和void没关系
cout<<"重载了new操作符"<<endl;
return malloc(size);
}
//重载的delete操作符,依然会触发析构函数
void operator delete(void * p )
{
cout<<"重载了delete操作符"<<endl;
if(p != NULL)
{
free(p);
p = NULL;
}
}
~A(){
cout<<"~A()..."<<endl;
}
void* operator new[](size_t size)
{
cout<<"重载了new[] 操作符"<<endl;
return malloc(size);
}
void operator delete[](void* p )
{
cout<<"重载了delete[] 操作符"<<endl;
if(p != NULL){
free(p);
p = NULL;
}
}
private:
int a;
};
int main()
{
A *ap=new A(10);//ap->operator new(sizeof(A));
delete ap;
cout<<"-------------------------------"<<endl;
A * array_p= new A[20]; //array_p->operator new[](sizeof(A[20]))
delete []array_p;
return 0;
}
运行结果