您的位置:首页 > 其它

操作符重载举例

2016-03-19 20:06 423 查看
[b]一.输入输出[/b]

#include <iostream>
using namespace std;

class Complex
{
public:
Complex()
{
real = 0;
imag = 0;
}

Complex(double r)
{
real = r;
imag = 0;
}

Complex(double r, double i)
{
real = r;
imag = i;
}

Complex operator+(const Complex &c) const
{
Complex s;
s.real = real + c.real;
s.imag = imag + c.imag;
return s;
}

Complex operator-(const Complex &c)
{
Complex s;
s.real = real - c.real;
s.imag = imag - c.imag;
return s;
}

double real, imag;
};

istream& operator>>(istream &in, Complex &c)
{
return in >> c.real >> c.imag;
}
ostream& operator<<(ostream& out, Complex &c);
int main()
{
Complex s;
cin >> s;//1 2
cout << s.real << " " << s.imag << endl;//1 2
return 0;
}


[b]二.赋值[/b]

#include <iostream>
using namespace std;

class Point
{
public:

Point()
{
x = 0;
y = 0;
}

Point(int a, int b)
{
x = a;
y = b;
}

Point& operator=(const Point& p)
{
x = p.x;
y = p.y;
return *this;
}
int x, y;
};

ostream& operator<<(ostream &out, Point p)
{
return out << p.x << " " << p.y << endl;
}

int main()
{
Point p1, p2, p3(3, 4);
p1 = p2 = p3;
cout << p1 << p2;
/*
3 4
3 4
*/
return 0;
}


#include <iostream>
using namespace std;

class String
{
public:
String(const char *s)
{
length = strlen(s);
p = new char[length + 1];
strcpy(p, s);
}

private:
int length;
char *p;
};

void fun(String &aa)
{
String c("789");
c = aa;
}

int main()
{
String a("1"), b("x");
b = a;//内存泄漏,b.p没有释放
fun(a);//a.p将指向一个未定义区域,指针悬浮
a = a;//检测两边是否相等
return 0;
}


const String& String::operator=(const String &right)
{
if (this != &right)//防止自我赋值
{
delete[] p;//防止内存泄漏
length = right.length;//取新字符串长度
p = new char[length + 1];//分配内存
for (int i = 0; i < length; i++)
{
p[i] = right.p[i];//复制
}
}
return *this;
}


Complex& operator=(string str)
{
char *p = new char[str.length + 1];
strcpy(p, str.c_str());
r = atof(strtok(p, "+"));
i = atof(strtok(NULL, "i"));
return *this;
}


[b]三.下标操作符[/b]

#include <iostream>
using namespace std;

class CharArray
{
public:
CharArray(int len)
{
length = len;
p = new char[length];
}

char& operator[](int i)
{
static char ch = 0;
if (i >= 0 && i < length)
{
return p[i];
}
else
{
cout << "\nout" << endl;
return ch;
}
}

private:
int length;
char *p;
};

int main()
{
CharArray str1(6);
char *str2 = "abcdefg";
for (int i = 0; i < 7; i++)
{
str1[i] = str2[i];//str2[6]保存在ch里
}
for (int i = 0; i < 7; i++)
{
cout << str1[i];
}
cout << endl;
/*
out
abcdef
out
g
*/
return 0;
}


[b]四.函数调用操作符[/b]

#include <iostream>
using namespace std;

class F
{
public:
double operator()(double x, double y) const
{
return 2 * x + y;
}
};

int main()
{
F f;
cout << f(1.5, 3.2) << endl;//6.2
return 0;
}


#include <iostream>
using namespace std;

class  Array
{
public:
Array()
{
num = new int[1];
}
Array(int i, int j)
{
row = i;
col = j;
num = new int[row * col];
}
int* operator[](int i)
{
return &num[i * col];
}
int operator()(int i, int j)
{
return num[i * col + j];
}
~Array()
{
delete[] num;
}

private:
int *num, row, col;
};

int main()
{
Array a(3, 4);
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
a[i][j] = 1;
}
}
cout << a(1, 1) << endl; // 1
return 0;
}


[b]五.自增/自减[/b]

#include <iostream>
using namespace std;

class A
{
public:
A()
{
value = 0;
}
A(int t)
{
value = t;
}
A operator++()
{
value++;
return *this;
}
A operator++(int)
{
A t;
t.value = value++;
return t;
/*
A a = *this;
++(*this);
return a;
*/
}
int value;
};

ostream& operator<<(ostream &out, A a)
{
return out << a.value << endl;
}

int main()
{
A a(10);
cout << a++; // 10
cout << ++a; // 12
return 0;
}


[b]五.转型操作符[/b]

声明中不能包含形参和返回类型,但函数体中必须包含return语句,用来返回转型结果。

#include <iostream>
using namespace std;

class A
{
public:
A(float num)
{
value = num;
}
operator int()
{
return static_cast<int>(value) + 10;
}

float value;
};

int main()
{
A a(8.0);
int i = a;
cout << i << endl;//18
return 0;
}


[b]六.内存管理[/b]

new, new[], delete, delete[]

void *类::operator new(size_t size);第一个参数必须是size_t,数值等于将被创建的对象大小

void 类::operator delete(void *p);第一个参数必须是void *
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: