重载运算符的运用:c++课本153课后习题1.2.4
1.复数相加:
题目:定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。讲运算符函数重载为非成员,非友元的普通函数。编程序,求两个复数之和。
#include <iostream>
#include<stdio.h>
#include <algorithm>
#include <string>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex complex_add(Complex &c2);
void display();
private:
double real;
double imag;
};
Complex Complex::complex_add(Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1.complex_add(c2);
cout << "c1=";
c1.display();
cout << "c2=";
c2.display();
cout << "c1+c2=";
c3.display();
return 0;
}
2." + - * / "的实现:
题目描述:定义一个复数类Complex,重载运算符“+ - * / ”,使之能用于复数的加减乘除。运算符重载函数作为Complex类的成员函数。编程序,分别求两个复数的和差积商。
#include <iostream>
#include<stdio.h>
#include <algorithm>
#include <string>
using namespace std;
class Complex
{
public:
Complex(){real=0;imag=0;}
Complex(double r,double i){real=r;imag=i;}
Complex operator + (Complex &c2);
Complex operator - (Complex &c3);
Complex operator * (Complex &c4);
Complex operator / (Complex &c5);
void display();
private:
double real;
double imag;
};
Complex Complex::operator + (Complex &c2)
{
Complex c;
c.real=real+c2.real;
c.imag=imag+c2.imag;
return c;
}
Complex Complex::operator - (Complex &c3)
{
Complex c1;
c1.real=real-c3.real;
c1.imag=imag-c3.imag;
return c1;
}
Complex Complex::operator *(Complex &c4)
{
Complex c2;
c2.real=real*c4.real;
c2.imag=imag*c4.imag;
return c2;
}
Complex Complex::operator / (Complex &c5)
{
Complex c3;
c3.real=real/c5.real;
c3.imag=imag/c5.imag;
return c3;
}
void Complex::display()
{
cout << "(" << real << "," << imag << "i)" << endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3,c4,c5,c6;
c3=c1+c2;
c4=c1-c2;
c5=c1*c2;
c6=c1/c2;
cout << "c1=";c1.display();
cout << "c2=";c2.display();
cout << "c1+c2=";c3.display();
cout << "c1-c2=";c4.display();
cout << "c1*c2=";c5.display();
cout << "c1/c2=";c6.display();
return 0;
}
4.矩阵求和:
题目描述:有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如c=a+b.
#include <iostream>
using namespace std;
class Matrix//定义Matri类
{
public:
Matrix();//默认构造函数
friend Matrix operator +(Matrix &,Matrix &);//重载运算符“+”
void input();//输入数据函数
void display();//输出数据函数
private:
int mat[2][3];
};
Matrix::Matrix()//定义构造函数
{
for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
mat[i][j]=0;
}
Matrix operator +(Matrix &a,Matrix &b)//定义重载运算符“+”函数
{
Matrix c;
for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
{
c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
}
return c;
}
void Matrix::input()//定义输入数据函数
{
cout << "input value of matrix:" << endl;
for(int i=0; i<2; i++)
for(int j=0; j<3; j++)
{
cin >> mat[i][j];
}
}
void Matrix::display()//定义输出数据函数
{
for(int i=0; i<2; i++)
{
for(int j=0; j<3; j++)
{
cout << mat[i][j] << " ";
}
cout << endl;
}
}
int main()
{
Matrix a,b,c;
a.input();
b.input();
cout << endl;
cout << "Matrix.a:" << endl;
a.display();
cout << endl;
cout << "Matrix.b:" << endl;
b.display();
c=a+b;///用重载运算符“+”实现两个矩阵相加
cout << endl;
cout << "Matrix.c:" << endl;
c.display();
return 0;
}