补充:c++里::的作用
声明了一个类A,类A里声明了一个成员函数void f(),但没有在类的声明里给出f的定义,那么在类外定义f时, 就要写成void A::f(),表示这个f()函数是类A的成员函数。::域名解析符
返回值类型名 类名::函数成员名(参数表)
{
函数体
}
这个是在类内声明函数后在类外定义的一种方法!
如果不加"类名::"的话,编译系统就不会知道你的函数属于哪个类;
另外,这样定义函数一定要在类中声明之后,说明它是类的成员函数才可以!
在类内声明的时候就不需要::了,直接 返回值类型 函数名(参数表) 就可以了!
运算符的重载
类的加法(+)运算符重载
类加法运算符重载原理如下:
classA+classB==classA.operator+(classB);
当两个类相加时,相当于调用上述重载函数
返回值 operator+()
{
}
operator是关键字
#include<iostream>
using namespace std;
class complex
{
public:
complex();
complex(double real,double imag);
void display();
/*complex operator+(const complex &A)const;*///加法
complex operator-(const complex &A)const;//减法
private:
double m_real;
double m_imag;
};
complex::complex():m_real(0.0),m_imag(0.0)
{}
complex::complex(double real,double imag):m_real(real),m_imag(imag)
{}
/*complex complex::operator+(const complex&A)const
{
complex B;
B.m_real = this->m_real + A.m_real;//shi bu jia shi bu
B.m_imag = this->m_imag + A.m_imag;//xu bu jia xu bu
return B;
}*/
complex complex::operator-(const complex&A)const
{
complex B;
B.m_real = this->m_real - A.m_real;
B.m_imag = this->m_imag - A.m_imag;
return B;
}
/*void complex::display()
{
cout<<m_real<<"+"<<m_imag<<"i"<<endl;
}*/
void complex::display()
{
cout<<m_real<<"-"<<m_imag<<"i"<<endl;
}
/*int main()
{
complex c1(4.3,5.8);
complex c2(2.4,3.7);
complex c3;
c3 = c1.operator+(c2);
c3.display();
return 0;
}*/
int main()
{
complex c1(4.3,5.8);
complex c2(2.4,3.7);
complex c3;
c3 = c1-c2;
c3.display();
return 0;
}
全局的运算符重载
(1)不是所有的运算符都可以进行重载的
eg:sizeof ? :. ::
(2)运算符重载不改变运算符的优先级
(3)运算符重载不会改变运算符的操作数量
(4)运算符重载不能有默认参数
(5)成员函数和全局函数(全局函数需要写友元函数:传入的参数需要两个)
(6)-> [] () = 只能作为成员函数重载,不能作为别的
#include<iostream>
#include<iomanip>
using namespace std;
class Stopwatch
{
public:
Stopwatch():m_min(0),m_sec(0)
{
}
void setzero()
{
m_min = 0;
m_sec = 0;
}
Stopwatch run();
Stopwatch operator++();//前置(prev),运行之前加一
Stopwatch operator++(int);//后置(next)需要带参数,运行过后加一
friend ostream & operator << (ostream &,const Stopwatch &A);
private:
int m_min;
int m_sec;
};
Stopwatch Stopwatch::run()
{
++m_sec;
if(m_sec == 60)
{
m_min++;
m_sec = 0;
}
return *this;
}
Stopwatch Stopwatch::operator++()
{
return run();
}
Stopwatch Stopwatch::operator++(int n)
{
Stopwatch s = *this;
run();
return s;
}
ostream & operator << (ostream & out,const Stopwatch &A)
{
out << setfill('0') << setw(2) << A.m_min << ":" << setw(2) << A.m_sec;
return out;
}
int main()
{
Stopwatch s1,s2;
s1 = s2++;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
s1.setzero();
s2.setzero();
s1 = ++s2;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
return 0;
}
/*int main()
{
complex c1(4.3,5.8);
complex c2(2.4,3.7);
complex c3;
c3 = operator+(c1,c2);
cout<<c3;
c3 = c1 - c2;
cout<<c3;
c3 = c1 * c2;
cout<<c3;
c3 = c1/c2;
cout<<c3;
return 0;*/
}
#include <iostream>
using namespace std;
class complex
{
public:
complex();
complex(double real,double imag);
void display();
friend complex operator+(const complex &A,const complex &B);
friend complex operator-(const complex &A,const complex &B);
friend istream &operator>>(istream &in,complex &A);
friend ostream &operator<<(ostream &out,complex &A);
private:
double m_real;
double m_imag;
};
complex::complex():m_real(0.0),m_imag(0.0){}
complex::complex(double real,double imag):m_real(real),m_imag(imag){}
complex operator+(const complex &A,const complex &B)
{
complex C;
C.m_real = A.m_real + B.m_real;
C.m_imag = A.m_imag + B.m_imag;
return C;
}
complex operator-(const complex &A,const complex &B)
{
complex C;
C.m_real = A.m_real - B.m_real;
C.m_imag = A.m_imag - B.m_imag;
return C;
}
void complex::display()
{
cout<<m_real<<"+"<<m_imag<<"i"<<endl;
}
istream &operator>>(istream &in,complex &A)
{
in>>A.m_real>>A.m_imag;
return in;
}
ostream &operator<<(ostream &out,complex &A)
{
out<<A.m_real<<"+"<<A.m_imag<<"i"<<endl;
return out;
}
int main()
{
complex c1(4.3,5.8);
complex c2(2.4,3.7);
complex c3;
complex c4;
c3 = operator+(c1,c2);
cout<<c3;
c4 = operator-(c1,c2);
cout<<c4;
return 0;
}
指针的浅层拷贝:单纯的指针地址赋值
深度拷贝:对指针指向的内存逐个进行拷贝
作业:将复数类和秒表类的运算符重载补充完整