仅作个人笔记学习交流使用
进击的C++(四)
一、类中函数重载
1.1 函数重载
- 本质为相互独立的不同函数
- C++通过函数名和函数参数确定函数调用
- 无法直接通过函数名得到函数的入口地址
- 函数重载必然发生在同一个作用域中
1.2 重载的意义
- 通过函数名对函数功能进行提示
- 通过参数列表对函数用法进行提示
- 扩展系统中已经存在的函数功能
1.3类中函数重载
- 构造函数的重载
- 普通成员函数的重载
- 静态成员函数的重载
ps: 全局函数和成员函数不能构成重载关系
二、操作符重载
通过operator关键字定义特殊函数,实现对操作符进行函数重载。
// 全员操作符重载函数,对于type类型的变量
type operator sign(const type &p1,const type &p2)
{
type ret;
/*。。。*/
return ret;
}
//sign为系统预定义的操作符,如‘+’,‘-’,‘*’,‘/’,‘==’。。。
//调用 p1 sign p2 等价于 operator sign(p1,p2)
可以将操作符重载函数定义为类的成员函数
- 比全局操作符重载函数少一个参数(左操作数)
- 不需要依赖友元
- 编译器优先在成员函数中寻找操作符重载函数
操作符重载注意
C++规定赋值操作符(=)只能重载为成员函数
不能改变原操作符的优先级
不能改变操作数的个数
不应改变操作符的原有语义
2.1 复数类定义
class Complex
{
double a;
double b;
public:
Complex(double a = 0, double b = 0);
double getA();
double getB();
double getModulus();
Complex operator + (const Complex& c);
Complex operator - (const Complex& c);
Complex operator * (const Complex& c);
Complex operator / (const Complex& c);
bool operator == (const Complex& c);
bool operator != (const Complex& c);
Complex& operator = (const Complex& c);
};
2.2 复数类函数功能
Complex::Complex(double a, double b)
{
this->a = a;
this->b = b;
}
double Complex::getA()
{
return a;
}
double Complex::getB()
{
return b;
}
double Complex::getModulus()
{
return sqrt(a * a + b * b);
}
Complex Complex::operator + (const Complex& c)
{
double na = a + c.a;
double nb = b + c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator - (const Complex& c)
{
double na = a - c.a;
double nb = b - c.b;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator * (const Complex& c)
{
double na = a * c.a - b * c.b;
double nb = a * c.b + b * c.a;
Complex ret(na, nb);
return ret;
}
Complex Complex::operator / (const Complex& c)
{
double cm = c.a * c.a + c.b * c.b;
double na = (a * c.a + b * c.b) / cm;
double nb = (b * c.a - a * c.b) / cm;
Complex ret(na, nb);
return ret;
}
bool Complex::operator == (const Complex& c)
{
return (a == c.a) && (b == c.b);
}
bool Complex::operator != (const Complex& c)
{
return !(*this == c);
}
Complex& Complex::operator = (const Complex& c)
{
if( this != &c )
{
a = c.a;
b = c.b;
}
return *this;
}
2.3 复数类使用
Complex c1(1, 2);
Complex c2(3, 6);
Complex c3 = c2 - c1;
Complex c4 = c1 * c3;
Complex c5 = c2 / c1;
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
printf("c4.a = %f, c4.b = %f\n", c4.getA(), c4.getB());
printf("c5.a = %f, c5.b = %f\n", c5.getA(), c5.getB());
Complex c6(2, 4);
printf("c3 == c6 : %d\n", c3 == c6);
printf("c3 != c4 : %d\n", c3 != c4);
(c3 = c2) = c1;
printf("c1.a = %f, c1.b = %f\n", c1.getA(), c1.getB());
printf("c2.a = %f, c2.b = %f\n", c2.getA(), c2.getB());
printf("c3.a = %f, c3.b = %f\n", c3.getA(), c3.getB());
三、字符串类
C不支持真正意义上的字符串,而是用字符数组和一组函数实现字符串操作,C不支持自定义类型,因此无法获得字符串类型。从C到C++引入了自定义类型,在C++中可以通过类完成字符串类型的定义,但C++中没有原生的字符串类型。
C++标准库提供了 string 类型,最大限度的考虑了C字符串的兼容性,并直接支持
功能 | 函数 |
---|---|
字符串拼接 | + |
字符串的大小比较 | < > == |
子串查找和提取 | find() substr() |
字符串的插入和替换 | insert() replace() |
字符串流类(sstream)用于 string 的转换
istringstream 字符串输入流
ostringstream 字符串输出流
#include<string>
#include<sstream>
#include<iostream>
using namespace std;
string str ="12.34";
double num;
//字符串转数字
istringstream(str)>>num;
cout<<num<<endl;
//数字转字符串
ostringstream ostr;
ostr<<num;
cout<<ostr.str()<<endl;
四、数组类和数组访问操作符[ ]
数组访问符**[ ]**是C/C++的内置操作符,原生意义是数组访问和指针运算,以下访问方式等价
a[n] <--> *(a+n) <--> *(n+a) <--> n[a]
4.1 数组类定义
class IntArray
{
private:
int m_length;
int* m_pointer;
IntArray(int len);
IntArray(const IntArray& obj);
bool construct();
public:
static IntArray* NewInstance(int length);
int length();
bool get(int index, int& value);
bool set(int index ,int value);
int& operator [] (int index);
IntArray& self();
~IntArray();
};
4.2 数组类函数功能
IntArray::IntArray(int len)
{
m_length = len;
}
bool IntArray::construct()
{
bool ret = true;
m_pointer = new int[m_length];
if( m_pointer )
{
for(int i=0; i<m_length; i++)
{
m_pointer[i] = 0;
}
}
else
{
ret = false;
}
return ret;
}
IntArray* IntArray::NewInstance(int length)
{
IntArray* ret = new IntArray(length);
if( !(ret && ret->construct()) )
{
delete ret;
ret = 0;
}
return ret;
}
int IntArray::length()
{
return m_length;
}
bool IntArray::get(int index, int& value)
{
bool ret = (0 <= index) && (index < length());
if( ret )
{
value = m_pointer[index];
}
return ret;
}
bool IntArray::set(int index, int value)
{
bool ret = (0 <= index) && (index < length());
if( ret )
{
m_pointer[index] = value;
}
return ret;
}
int& IntArray::operator [] (int index)
{
return m_pointer[index];
}
IntArray& IntArray::self()
{
return *this;
}
IntArray::~IntArray()
{
delete[]m_pointer;
}
4.3 数组类使用
IntArray* a = IntArray::NewInstance(5);
if( a != NULL )
{
IntArray& array = a->self();
cout << "array.length() = " << array.length() << endl;
array[0] = 1;
for(int i=0; i<array.length(); i++)
{
cout << array[i] << endl;
}
}
delete a;
4.4 数组访问操作符实现字典操作
#include <iostream>
#include <string>
using namespace std;
class Dict
{
int a[5];
public:
int& operator [] (int i)
{
return a[i];
}
int& operator [] (const string& s)
{
if( s == "1st" )
{
return a[0];
}
else if( s == "2nd" )
{
return a[1];
}
else if( s == "3rd" )
{
return a[2];
}
else if( s == "4th" )
{
return a[3];
}
else if( s == "5th" )
{
return a[4];
}
return a[0];
}
int length()
{
return 5;
}
};
Dict t;
for(int i=0; i<t.length(); i++)
{
t[i] = i;
cout << t[i] << endl;
}
cout << t["5th"] << endl;
cout << t["4th"] << endl;
cout << t["3rd"] << endl;
cout << t["2nd"] << endl;
cout << t["1st"] << endl;