C++实例——运算符重载

文章转自:http://yhbspace.blog.163.com/blog/static/2651609420076100155751/

 

 

C++实例——运算符重载

一、两个复数的加法运算
二、复数的加减乘除运算
三、复数与标准型数据之间的运算,顺序任意
四、两个矩阵间的运算与输出(行列任意)
五、复数与double型数据的运算
六、不同类对象的转换


一、定义一复数类complex,重载运算符+,用于复数的加法运算,将运算符重载函数定义为非成员、非友元
函数,编程求两复数之和。

#include<iostream>
using namespace std;
class complex
{
   public:
      int real,imag;
      complex(int r=0,int i=0):real(r),imag(i){}
      void display()
        {
          cout<<real<<","<<imag;
        }
};
complex operator+(complex& c1,complex& c2)
{
   return complex(c1.real+c2.real,c1.imag+c2.imag);
}
int main()
{
   complex c1(12,9),c2(2,10),c3;
   c3=c1+c2;
   cout<<"c1=(";c1.display();cout<<")"<<endl;;
   cout<<"c2=(";c2.display();cout<<")"<<endl;
   cout<<"c3=(";c3.display();cout<<")"<<endl;
   return 0;
}

输出:
c1=(12,9)
c2=(2,10)
c3=(14,19)
Press any key to continue...


二、定义一个复数类complex,重载运算符+-*/,用于复数的加减乘除运算,将运算符函数重载为complex类
的成员函数,求两个复数的和、差、积、商。

#include<iostream>
using namespace std;
class complex
{
   public:
      complex(int r=0,int i=0):real(r),imag(i){}
      complex operator+(complex&);
      complex operator-(complex&);
      complex operator*(complex&);
      complex operator/(complex&);
      void display()
        {
          cout<<real<<","<<imag;
        }
   private:
      int real,imag;
};
complex complex::operator+(complex& c2)
{
   return complex(real+c2.real,imag+c2.imag);
}
complex complex::operator-(complex& c2)
{
   return complex(real-c2.real,imag-c2.imag);
}
complex complex::operator*(complex& c2)
{
   return complex(real*c2.real,imag*c2.imag);
}
complex complex::operator/(complex& c2)
{
   return complex(real/c2.real,imag/c2.imag);
}
int main()
{
   complex c1(12,9),c2(2,3),c3;
   cout<<"c1=(";c1.display();cout<<")"<<endl;;
   cout<<"c2=(";c2.display();cout<<")"<<endl;
   c3=c1+c2;
   cout<<"C1+C2=(";c3.display();cout<<")"<<endl;
   c3=c1-c2;
   cout<<"c1-c2=(";c3.display();cout<<")"<<endl;
   c3=c1*c2;
   cout<<"c1*c2=(";c3.display();cout<<")"<<endl;
   c3=c1/c2;
   cout<<"c1/c2=(";c3.display();cout<<")"<<endl;
   return 0;
}

输出:
c1=(12,9)
c2=(2,3)
C1+C2=(14,12)
c1-c2=(10,6)
c1*c2=(24,27)
c1/c2=(6,3)
Press any key to continue...

三、定义一个复数类,重载运算符+,用于复数的加法,参加运算的可是类对象,也可以是只有一个整数,
顺序任意,如c1+c2,c1+i,i+c1,设i为整数,编程求两个复数之和,整数和复数之和。

#include<iostream>
using namespace std;
class complex
{
   public:
      complex(){real=0;imag=0;}
      complex(int r,int i):real(r),imag(i){}
      complex(int r){real=r;imag=0;}
      friend complex operator+(const complex&,const complex&);
      friend ostream& operator<<(ostream&,complex&);
   private:
       int real,imag;
};
complex operator+(const complex& c1,const complex& c2)
{
   return complex(c1.real+c2.real,c1.imag+c2.imag);
}
ostream& operator<<(ostream& output,complex& c)
{
  output<<"("<<c.real<<","<<c.imag<<")";
  return output;
}
int main()
{
   complex c1(12,9),c2(2,3),c3;
   cout<<"c1="<<c1<<endl;
   cout<<"c2="<<c2<<endl;
   c3=c1+c2;
   cout<<"c1+c2="<<c3<<endl;
   c3=c2+c1;
   cout<<"c2+c1="<<c3<<endl;
   c3=c1+8;
   cout<<"c1+8="<<c3<<endl;
   c3=8+c1;
   cout<<"8+c1="<<c3<<endl;
   return 0;
}

输出:
c1=(12,9)
c2=(2,3)
c1+c2=(14,12)
c2+c1=(14,12)
c1+8=(20,9)
8+c1=(20,9)
Press any key to continue...

四、设有两个2*3矩阵a,b,重载运算符+,用于两个矩阵的加法运算,如c=a+b,设c也为2*3的矩阵。重载<<
和>>,用于输入输出矩阵。

#include <iostream>
using namespace std;
class matrix
{
  public:
     matrix(int r=0,int c=0);
     friend istream& operator>>(istream&,matrix&);
     friend matrix operator+(const matrix&,const matrix&b);
     friend ostream& operator<<(ostream&,matrix&);
  private:
     int* p;
     int row,col;
};
matrix::matrix(int r,int c)
{
   row=r;
   col=c;
   p=new int[row*col];
   for(int i=0;i<row*col;i++)
     p[i]=0;
}

istream& operator>>(istream& input,matrix& c)
{
  cout<<"Matrix("<<c.row<<"*"<<c.col<<")";
  for(int i=0;i<c.row*c.col;i++)
    input>>c.p[i];
  return input;
}

matrix operator+(const matrix& a,const matrix& b)
{
  matrix temp(a.row,a.col);
  for(int i=0;i<a.row*a.col;i++)
    temp.p[i]=a.p[i]+b.p[i];
  return temp;
}

ostream& operator<<(ostream& output,matrix& c)
{
   for(int i=0;i<c.row*c.col;i++)
     output<<c.p[i]<<"\t";
   delete []c.p;
   return output;
}

int main()
{
   matrix a(2,3);
   matrix b(2,3);
   matrix c(2,3);
   cout<<"a:";
   cin>>a;
   cout<<endl;
   cout<<"b:";
   cin>>b;
   cout<<endl;
   c=a+b;
   cout<<a<<endl<<b<<endl<<c<<endl;
   return 0;
}

输出:
a:Matrix(2*3)1 2 3 4 5 6

b:Matrix(2*3)4 5 6 7 8 9

1       2       3       4       5       6
4       5       6       7       8       9
5       7       9       11      13      15
Press any key to continue...

五、定义一复数类complex,编程计算complex类对象与一double型数据d1的和,将结果放在d1中并输出,再
以复数形式输出。
#include <iostream.h>
class complex
{
  public:
      complex(){real=0;imag=0;}  //默认构造函数
      complex(int r,int i):real(r),imag(i){}   //重载构造函数
      operator double(){return real;}  //类型转换函数,用于将对象转换为double型数据。
      friend complex operator+(const complex&,const complex&); +运算符重载
      friend ostream& operator<<(ostream&,complex&);  <<运算符重载,用于输出对象
  private:
      int real,imag;
};

complex operator+(const complex& c1,const complex& c2)
{
  return complex(c1.real+c2.real,c1.imag+c2.imag);
}

ostream& operator<<(ostream& output,complex& c)
{
  output<<"("<<c.real<<","<<c.imag<<")";
  return output;
}

int main()
{
  double d1=3.14159;
  complex c1(5,6),c2(3,9);
  d1=d1+c2;
  cout<<d1<<endl;
  complex c3(d1,0);
  cout<<c3<<endl;
  return 0;
}


六、定义两个类,techer类和student类,将student类对象转换为techer类对象,假设学生毕业后留校,其相关
数据仍然有用,如编号num,姓名name,性别sex。转换后输出相关数据。

#include <iostream>
#include<string>
using namespace std;
class techer;//类的提前引用声明
class student
{
   public:
       friend techer;//将techer类声明为本类的友元类,以便可以访问本类的数据成员。
       student(){num=0;name="null";sex='F';score=0;}//默认构造函数
       student(int n,string nam,char s,float sco):num(n),name(nam),sex(s),score(sco){}
       friend ostream& operator<<(ostream&,student&);//重载<<,以输出student类
   private:
       int num;
       string name;
       char sex;
       float score;
};

ostream& operator<<(ostream& output,student& t)
{
   output<<t.num<<" "<<t.name<<" "<<t.sex<<" "<<t.score;
   return output;
}

class techer
{
  public:
      techer(){num=0;name="null";sex='F';}
      techer(int n,string nam,char s):num(n),name(nam),sex(s){}
      techer(student& s){num=s.num;name=s.name;sex=s.sex;}  //转换构造函数,将学生类转换为教师类
      friend ostream& operator<<(ostream&,techer&);
  private:
      int num;
      string name;
      char sex;
};

ostream& operator<<(ostream& output,techer& t)
{
   output<<t.num<<" "<<t.name<<" "<<t.sex;
   return output;
}

int main()
{
   student s(1008,"zhangning",'F',98);
   techer t(s);
   cout<<"techer : "<<t<<endl;
   cout<<"student: "<<s<<endl;
   return 0;
}

输出:

techer : 1008 zhangning F
student: 1008 zhangning F 98
Press any key to continue...

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值