1 运算符重载
#include <iostream>
using namespace std;
class Pen
{
public:
friend ostream & operator<<(ostream& cout, const Pen& p1);
friend istream& operator>>(istream& cin, Pen& p1);
Pen(int a, int b) :m_a(a), m_b(b)
{}
void display()
{
cout << "(" << m_a << "," << m_b << ")" << endl;
}
const Pen operator+ ( const Pen &p2)const//保证返回值不能复赋值
{
cout << "Pen operator+" << endl;
return Pen(m_a + p2.m_a, m_b + p2.m_b);
}
const Pen operator- (const Pen& p2)const
{
cout << "Pen operator-" << endl;
return Pen(m_a - p2.m_a, m_b - p2.m_b);
}
const Pen operator* (const Pen& p2)const
{
cout << "Pen operator*" << endl;
return Pen(m_a * p2.m_a, m_b * p2.m_b);
}
const Pen operator/ (const Pen& p2)const
{
cout << "Pen operator*" << endl;
return Pen(m_a / p2.m_a, m_b / p2.m_b);
}
Pen &operator+=(const Pen& p2)//使返回值可以赋值
{
cout << "Pen operator+=" << endl;
m_a += p2.m_a;
m_b += p2.m_b;
return *this;
}
bool operator!=(const Pen& p1)
{
return (m_a != p1.m_a) || (m_b != p1.m_b);
}
bool operator==(const Pen& p1)
{
return (m_a == p1.m_a) && (m_b == p1.m_b);
}
const Pen operator-()const
{
return Pen(-m_a, -m_b);
}
Pen &operator++()//后置++
{
++m_a; ++m_b;
return *this;
}
Pen &operator++(int)//前置++,语法规定加一个int
{
Pen old(m_a,m_b);
++m_a; ++m_b;
return old;
}
private:
int m_a;
int m_b;
//int m_c;
};
ostream &operator<<(ostream &cout, const Pen &p1) //系统自带的cout必须是非const
{
cout << "(" << p1.m_a << "," << p1.m_b<<")";
return cout;
}
istream& operator>>(istream& cin, Pen& p1)//p1是需要改变值 所以不能用const
{
cin >> p1.m_a >> p1.m_b;
return cin;
}
int main()
{
Pen p1(10, 20);
Pen p2(20, 30);
Pen p3(30, 40);
//Pen p4 = p1++;
//p1 += p2;
//cout << (p1 == p2 ) ;
// p1++;
//++p1 = p2;
//p1.display();
cout << p1 << p2;
cin >> p1 >> p2;
cout << p1 << p2;
//system("pause");
return 0;
}
2 模板
#include <iostream>
#include<vector>
using namespace std;
template<typename Ele>
class Array
{
public:
//Array() :m_data(0), m_size(0), m_capacity(0) {};
Array(int capacity):m_size(0),m_capacity(1)
{
m_capacity = (capacity > 10) ? capacity : 10;
m_data = new int[m_capacity];
}
~Array() {
if (m_data == NULL)
{
return;
}
delete[]m_data;
}
void add(Ele value) {
if (m_size == m_capacity)
{
//扩容
//m_capacity = m_size+1;
//int *new_data = new int[m_capacity];
//memcpy(new_data, m_data, m_size * sizeof(int));
for (int i = 0; i < m_size; i++)
{
new_data[i] = m_data[i];
m_data[i] = 0;
}
//
//new_data = m_data;
//delete[]m_data;
//
cout << "空间不够";
return;
}
m_data[m_size++] = value;
}
Ele get(int index) {
if (index < 0 || index >= m_size) {
//报错:抛异常
throw "数组下标越界";
}
return m_data[index];
}
int size() {
return m_size;
}
Ele operator[](int index) {
return get(index);
}
//ostream &operator<<(ostream& cout,const Array<>)
private:
Ele * m_data;
int m_size;
int m_capacity;
};
class Point
{
public:
Point(int x,int y):m_x(x),m_y(y){}
private:
int m_x;
int m_y;
};
int main()
{
/*Array<int> arr(8);
arr.add(11);
arr.add(12);
arr.add(13);
arr.add(14);
arr.add(15);
arr.add(16);
arr.add(18);
arr.add(10);
*/
Array<void *> arr1(4);
arr1.add(& Point(1, 2));
arr1.add(new Point(1, 2));
/*cout << arr.get(0)<<endl;
cout << arr.size()<<endl<<arr[2];*/
//system("pause");
return 0;
}
4 异常
6 智能指针
存在调用两次析构函数,有异常。