1.设计一个复数Complex类,用友元函数重载乘法运算符(*),用成员函数重载除法运算符(/),完成两个复数对象的乘除法。

#include<iostream>
using namespace std;
#include<string>
class complex//类定义
{
public:
complex(double re = 0, double im = 0)//构造函数有实部和虚部的参数
//给出默认值,默认实部和虚部都为0
//看到这样的构造函数时,在写对象时、在初始化创建时,参数可以写三种
//即实例化对象的方式有三种:实部和虚部都给,实部和虚部都不给,只给实部
//因为数据只给第一个参数,第一个参数是实部,此时虚部默认为0
{
real = re;
imag = im;
}
//友元函数做双目运算符重载,运算符重载的实质就是函数重载
friend complex operator+(complex a, complex b);
//operator+的重载是用友元friend来做的
//函数名叫 operator+,返回值是一个complex类型,参数有两个a,b做加法的就是a+b
friend complex operator++(complex& a);
//成员函数做双目运算符重载,做减法的重载
complex operator-(complex a)//a此时是减数
{
complex t;
t.real = real - a.real;//此时real相当于this->real,指当前对象是被减数
t.imag = imag - a.imag;//此时imag相当于this->imag,指当前对象是被减数
return t;
}
//自减运算,成员函数做单目运算符重载,注意this指针
complex operator--()
{
real--;
imag--;
return *this;
}
void show()//对当前复数进行输出
{
cout << real;
if (imag > 0)cout << "+" << imag << "i" << endl;
else
if (imag < 0)cout << imag << "i" << endl;
}
private:
double real, imag;//两个私有成员
};
//友元函数实现的加法的运算符重载
complex operator+(complex a, complex b)
{
complex t;//临时变量t
t.real = a.real + b.real;//两个实部的和
t.imag = a.imag + b.imag;//两个虚部的和
return t;
}
complex operator++(complex& a)
{
a.real++;
a.imag++;
return a;
}
int main() {
complex c1(1, 2), c2(0, 2), c3, c4, c5, c6;
//创建对象有三种方式:无参,两个参数,一个参数
c3 = c1 + c2;
c3 = operator+(c1, c2);
c3.show();
c4=c1.operator-(c2);
c4=c1-c2;
c4.show();
c5 = operator++(c1);
c5=++c1;
c5.show();
c6=c2.operator--();
c6.show();
return 0;
}
运行结果:


2.编写一个程序,用成员函数重载运算符“+”,友元函数重载运算符“-”,完成两个二维数组 (两行三列的二维数组)相加、相减。要求第一个二维数组有构造函数设置,另一个二维数组的值由键盘输入。
#include<iostream>
#include<iomanip>
using namespace std;
const int i = 2;
const int j = 3;//定义两个常变量
class arr
{
public:
void get_arr()
{
for(int i=0;i<2;i++)
for (int j = 0; j < 3; j++)
{
cin >> a[i][j];//完成数组的输入
}
}
arr(int a1 = 0, int a2 = 0, int a3 = 0, int b1 = 0, int b2 = 0, int b3 = 0)
//六个参数并对二维数组的六个元素进行赋值
{
a[0][0] = a1; a[0][1] = a2; a[0][2] = a3;
a[1][0] = b1; a[1][1] = b2; a[1][2] = b3;
}
friend arr operator + (arr & a1, arr & a2);//加法用友元函数实现
arr operator-(arr& x)//减法做成员函数
{
arr t;
for(int i=0;i<2;i++)
for (int j = 0; j < 3; j++)
{
t.a[i][j] = a[i][j] - x.a[i][j];
}//对应元素相减在return
return t;
}
void show()//输出,要分行分列
{
for(int i=0;i<2;i++)//外循环控制行,两行
for (int j = 0; j < 3; j++) //内循环控制每行输出三个值
{
cout << setw(5) << a[i][j];
//setw中的w是指宽度,setw是指现在的a[i][j]输出要占五列
if (j==2)cout << endl;//换行
}
}
private:
int a[i][j];//此时i=2,j=3
};
arr operator+(arr& a1, arr& a2)
{
arr t;//新增临时对象t
for(int i=0;i<2;i++)
for (int j = 0; j < 3; j++)
{
t.a[i][j] = a1.a[i][j] + a2.a[i][j];
}//双重循环
return t;
}
int main() {
arr a1(1, 2, 3, 4, 5, 6), a2, a3, a4;//a1输出就是123在第一行,456在第二行
//a2, a3, a4都是无参,默认两行三列都是0
cout << "请输入一个2行3列的二维数组:" << endl;//给a2输入
a2.get_arr();//把默认全零的数据改成从键盘输入数据
cout << "第一个数组如下:" << endl;
a1.show();//输出第一个数组的信息
cout << "第二个数组如下:" << endl;
a2.show();//输出第二个数组的信息
cout << "二个数组相加结果如下:" << endl;
//有两种调用方法:隐式调用和显示调用
//a3=a1+a2; 隐式,直接相加,会去找到友元函数来完成加法的计算
a3 = operator+(a1, a2);//显示,相当于是用函数调用的方式直接写出来
//operator+为函数名,a1, a2是实参
a3.show();
cout << "二个数组相减结果如下:" << endl;
//a4=a1-a2; 隐式
a4 = a1.operator-(a2);//显示
a4.show();
return 0;
}运行结果:

版权声明:本文为Yutouxn0原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。