参考MOOC 北大c++ 郭伟老师课件
//可变长整型数组类的实现
#include
using namespace std;
class Carray{
private:
int *ptr;
int size;
public:
Carray(int s=0);
Carray(const Carray &a);
~Carray();
int length(){
return size;
}
void push_back(int v);
int & operator[](int i){
return ptr[i];
};
Carray & operator=(Carray &a);
};
Carray::Carray(int s):size(s){
if(s==0){
ptr=NULL;
}else{
ptr=new int[s];
}
}
Carray::Carray(const Carray &a){
if(!a.ptr){
ptr=NULL;
size=0;
return;
}
ptr=new int[a.size];
memcpy(ptr,a.ptr,sizeof(int)*a.size);
size=a.size;
}
Carray::~Carray(){
if(ptr){
delete [] ptr;
}
}
Carray & Carray::operator=(Carray &a){
if(this==&a){
return *this;//防止a=a这样的错误
}
if(!a.ptr){
if(ptr) delete []ptr;
ptr=NULL;
size=0;
return *this;
}
if(sizelength());
delete []ptr;
ptr=pptr;
}else{//原数组为空
ptr=new int[1];
}
ptr[size++]=v;//加入新元素
}
int main(void){
Carray a;
for(int i=0;i<5;++i){
a.push_back(i);
}
Carray a2,a3;
a2=a;
for(int i=0;i
//性能稍好一点的,自定义可变长数组类
#include
#include
using namespace std;
class Myarray {
private:
int *ptr;
int size;
int capacity;
public:
Myarray(int s = 0);
Myarray(const Myarray &arr);
~Myarray();
void push(int v);
int length() {
return size;
}
Myarray & operator =(Myarray & arr);
int & operator [](int index);
};
Myarray::Myarray(int s):size(s){
if (s == 0) {
ptr = NULL;
size = s;
}
else {
if (!ptr)
delete[] ptr;
ptr = new int[size];
}
capacity = size + sizeof(int) * 32;
}
Myarray::Myarray(const Myarray & arr) {
if (!arr.ptr) {
ptr = NULL;
size = 0;
}
else {
if (!ptr)
delete[] ptr;
ptr = new int[arr.size];
size = arr.size;
memcpy(ptr, arr.ptr, sizeof(int)*arr.size);
}
}
Myarray::~Myarray() {
if (ptr) {
delete[] ptr;
}
}
//每次多开点空间
void Myarray::push(int v) {
if (!ptr) {
ptr = new int[32];
capacity = 32;
}
else {
if (size + 1>capacity) {
int *pptr = new int[size + sizeof(int) * 32];
capacity += sizeof(int) * 32;
memcpy(pptr, ptr, sizeof(int)*size);
ptr = pptr;
}
}
ptr[size++] = v;
}
Myarray & Myarray::operator =(Myarray & arr) {
if(this!=&arr)
if (!arr.ptr) {
ptr = NULL;
size = 0;
}else{
ptr = new int[arr.size];
memcpy(ptr, arr.ptr, sizeof(int)*arr.size);
size=arr.size;
capacity=arr.capacity;
}
return *this;
}
int & Myarray::operator[](int index) {
if(index
//流插入运算符的重载
#include
using namespace std;
class Student{
private:
int sage;
public:
Student(int sa=18):sage(sa){
}
int GetAge() const{
return sage;
}
};
ostream & operator<<(ostream & op,const Student & s){
op<
#include
#include
#include
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r = 0, double i = 0) :real(r), imag(i) {
}
friend istream &operator >> (istream &op, Complex &c);
friend ostream &operator << (ostream &op, Complex &c);
};
ostream &operator << (ostream &op, Complex &c) {
cout << c.real << "+" << c.imag << "i" << endl;
return op;
}
istream &operator >> (istream & ip, Complex &c) {
string s;
ip >> s;
int pos = s.find("+", 0);
string stemp = s.substr(0, pos);
c.real = atof(stemp.c_str());
stemp = s.substr(pos + 1,s.length() -pos-2);
c.imag= atof(stemp.c_str());
return ip;
}
int main(void) {
Complex cc;
int n;
cin>>cc>>n;
cout << cc << n << endl;
return 0;
}
//重载类型转换运算符
#include
using namespace std;
class Complex {
private:
double real, imag;
public:
Complex(double r,double i):real(r),imag(i){
}
operator double(){
return real;
}
};
int main(void){
Complex c(1.2,3.4);
cout<
自增自建操作符重载
//重载自增 自减运算符 为成员函数
#include
using namespace std;
class Cdemo{
private:
int num;
public:
Cdemo(int n):num(n){
}
//前置++
Cdemo & operator++(){
++num;
return *this;
}
//后置++
Cdemo operator++(int c){
Cdemo tep(*this);
num++;
return tep;
}
//前置--
Cdemo & operator--(){
--num;
return *this;
}
//后置--
Cdemo operator--(int c){
Cdemo tep(*this);
num--;
return tep;
}
operator int(){
return num;
}
};
int main(void){
Cdemo d(5);
cout<<(d++)<<",";
cout<
//重载自增 自减运算符 为全局函数
#include
using namespace std;
class Cdemo{
private:
int num;
public:
Cdemo(int n):num(n){
}
//前置++
friend Cdemo & operator++(Cdemo &c);
//后置++
friend Cdemo operator++(Cdemo &c,int e);
//前置--
friend Cdemo & operator--(Cdemo &c);
//后置--
friend Cdemo operator--(Cdemo &c,int e);
operator int(){
return num;
}
};
//前置++
Cdemo & operator++(Cdemo &c){
++c.num;
return c;
}
//后置++
Cdemo operator++(Cdemo &c,int e){
Cdemo tep(c);
c.num++;
return tep;
}
//前置--
Cdemo & operator--(Cdemo &c){
--c.num;
return c;
}
//后置--
Cdemo operator--(Cdemo &c,int e){
Cdemo tep(c);
c.num--;
return tep;
}
int main(void){
Cdemo d(5);
cout<<(d++)<<",";
cout<