文章目录
复数的运算符重载。重载对象与整型相加。运算符的重载详细实例重载输入输出
运算符重载,尤其是
前置自增和
后置自增。
#include<iostream>
using namespace std;
class Fraction {
public:
Fraction() {
num1=0,num2=0;
}
Fraction(int a,int b):num1(a),num2(b) { }
Fraction operator+ (const Fraction &f)const;
Fraction operator- (const Fraction &f)const;
Fraction operator* (const Fraction &f)const;
Fraction operator/ (const Fraction &f)const;
Fraction operator++(); // 前置自增
Fraction operator++(int); // 后置自增
void print() {
if (num1==0&&num2==0)
cout<<"0"<<endl;
else {
// 重载内化简改为输出时化简
int g=gcd(num1,num2);
num1/=g;
num2/=g;
cout<<num1<<"/"<<num2<<endl;
}
}
// 化简分数
int gcd(int m1,int m2)const { //求最大公约数
while(m2) {
long m=m1;
m1=m2;
m2=m%m2;
}
return m1>0? m1:-m1; // 返回正数公约数
}
//
private:
int num1,num2;
};
Fraction Fraction::operator+ (const Fraction &f)const {
int a = num1*f.num2+f.num1*num2;
int b = num2*f.num2;
return Fraction(a,b);
}
Fraction Fraction::operator- (const Fraction &f)const {
int a = num1*f.num2-f.num1*num2;
int b = num2*f.num2;
return Fraction(a,b);
}
Fraction Fraction::operator* (const Fraction &f)const {
int a = num1*f.num1;
int b = num2*f.num2;
return Fraction(a,b);
}
Fraction Fraction::operator/ (const Fraction &f)const {
int a = num1*f.num2;
int b = num2*f.num1;
return Fraction(a,b);
}
Fraction Fraction::operator++() {
num1=num1+num2;
return *this;
}
Fraction Fraction::operator++(int) {
Fraction old = Fraction(*this);
num1=num1+num2;
return old;
}
int main() {
// cout<<"下面是一组测试数据:"<<endl;
int a1,a2,a3,a4;
cin>>a1>>a2>>a3>>a4;
cout<<"Default:";
Fraction r0;
r0.print();
cout<<"Input original :\n";
Fraction r1(a1,a2),r2(a3,a4);
r1.print();
r2.print();
cout<<"+:";
Fraction r4=r1+r2;
r4.print();
cout<<"-:";
r4=r1-r2;
r4.print();
cout<<"*:";
r4=r1*r2;
r4.print();
cout<<"/:";
r4=r1/r2;
r4.print();
cout<<"++(int):";
(r4++).print();
cout<<"++:";
(++r4).print();
return 0;
}
复数的运算符重载。
// 复数运算符重载
#include<iostream>
using namespace std;
class complex{
public:
complex(double real=0,double imag=0):m_real(real),m_imag(imag){}
friend complex operator+ (const complex &A,const complex &B);
friend complex operator- (const complex &A,const complex &B);
friend complex operator* (const complex &A,const complex &B);
friend complex operator/ (const complex &A,const complex &B);
friend istream &operator>> (istream &is,complex &A);
friend ostream &operator<< (ostream &os,const complex &A);
private:
double m_real,m_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;
}
//重载乘法
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;
}
//重载输入运算符
istream &operator>> (istream &is, complex &A){
is>>A.m_real>>A.m_imag;
return is;
}
//重载输出运算符
ostream &operator<< (ostream &os, const complex &A){
os<<"Real: "<<A.m_real<<" "<<"Imag: "<<A.m_imag<<endl;
return os;
}
int main(){
complex c1(4.3,2.7),c2,c3;
cin>>c2;
c3 = c1 + c2;
cout<<"c1 + c2 = "<<c3<<endl;
c3 = c1 - c2;
cout<<"c1 - c2 = "<<c3<<endl;
c3 = c1 * c2;
cout<<"c1 * c2 = "<<c3<<endl;
c3 = c1 / c2;
cout<<"c1 / c2 = "<<c3<<endl;
return 0;
}
重载对象与整型相加。
#include<iostream>
using namespace std;
class A
{
private:
int a;
public:
A();
A(int n);
A operator+(const A & obj);
A operator+(const int b);
friend A operator+(const int b, A obj);
void display();
} ;
A::A()
{
a=0;
}
A::A(int n)//构造函数
{
a=n;
}
A A::operator +(const A& obj)//重载+号用于 对象相加
{
return this->a+obj.a;
}
A A::operator+(const int b)//重载+号用于 对象与数相加
{
return A(a+b);
}
A operator+(const int b, A obj)
{
return obj+b;//友元函数调用第二个重载+的成员函数 相当于 obj.operator+(b);
}
void A::display()
{
cout<<a<<endl;
}
int main ()
{
A a1(1);
A a2(2);
A a3,a4,a5;
a1.display();
a2.display();
int m=1;
a3=a1+a2;//可以交换顺序,相当于3=a1.operator+(a2);
a3.display();
a4=a1+m;//因为加了个友元函数所以也可以交换顺序了。
a4.display();
a5=m+a1;
a5.display();
}
运算符的重载详细实例
一元运算符——成员函数( = ()-> 必须)
二元运算符——友元函数(<< >> 必须)
运算时,有数与对象的混合运算时,必须使用友元函数
二元运算中,第一个操作数为非对象时,必须使用友元函数
友元函数重载比成员函数重载多一个参数
重载输入输出
//输入/输出运算符重载
friend ostream &operator<< (ostream &os,const Site &s){
os<<"This is output."<<endl;
return os;
};
// const 保护内容 引用提高效率
friend istream &operator>> (istream &is, Site &s){
is>>s.name;
return is;
}
// 修改内容所以不用 const
int main(){
Site site();
cin>>site;
cout<<site;
}