C++学习记录与心得(二) 运算符重载

学习任务中,有几道程序题,自己写的拿出来记录一下。

1.)声明一个复数类 Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、
除,运算符重载函数作为 Complex 类的成员函数。编程序,分别求两个复数之和、差、积和商。
            思考:你编的程序能否用于一个整数与一个复数的算术运算?如 4+(5-2i)。

#include<iostream>
#include<string>
using namespace std;
class Complex {
public:
	Complex()  { real = 0; imag = 0; };  //默认构造函数,无形参
	Complex(double r, double i) {
		real = r;
		imag = i; //构造函数重载
   }
	friend Complex operator +(Complex& x1, Complex& x2);
	friend Complex operator - (Complex& x1, Complex& x2);
	friend Complex operator * (Complex& x1, Complex& x2);
	friend Complex operator / (Complex& x1, Complex& x2);
	void dispplay();
private:
	double real; //实部
	double imag;//虚部
};
Complex operator +(Complex& x1, Complex& x2) {
	Complex x;
	x.real = x1.real + x2.real;
	x.imag = x1.imag + x2.imag;
	return x;
 }
Complex operator -(Complex &x1, Complex &x2) {
	Complex x;
	x.real = x1.real - x2.real;
	x.imag = x1.imag - x2.imag;
	return x;
}
Complex operator *(Complex &x1, Complex &x2) {
	Complex x;
	x.real = x1.real * x2.real - x1.imag * x2.imag;
	x.imag = x1.real * x2.imag + x1.imag * x2.real;
	return x;
}
Complex operator /(Complex& x1, Complex& x2) {
	Complex x;
	x.real = (x1.real * x2.real + x1.imag * x2.imag) / (x2.real * x2.real + x2.imag * x2.imag);
	x.imag=(x1.imag*x2.real-x1.real*x2.imag)/ (x2.real * x2.real + x2.imag * x2.imag);
	return x;
}
void Complex::dispplay() {
	if (imag <= 0) {
		cout << real << imag <<"i"<< endl;
	}
	else
		cout << real << "+" << imag << "i" << endl;
 }
void main() {
	double r1, i1,r2,i2;
	cout << "输入复数的实部和虚部"<<endl;
	cin >> r1>>  i1>>r2>>i2;
	Complex    c1(r1, i1), c2(r2, i2),c3,c4,c5,c6;
	c3 = c1 + c2; 
	c4 = c1 - c2; 
	c5 = c1 * c2; 
	c6 = c1 / c2;
	cout << "c1+c2="; c3.dispplay();
	cout << "c1-c2="; c4.dispplay();
	cout << "c1*c2="; c5.dispplay();
	cout << "c1/c2="; c6.dispplay();
}

运算结果如下

 

2.)声明一个复数类 Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量
可以都是类对象,也可以其中有一个是整数,顺序任意。例如: c1+c2, i+ c1, c1+i 均合法(设 i 为整数,
c1, c2 为复数)。运行程序,分别求两个复数之和、整数和复数之和。

#include<iostream>
#include<string>
using namespace std;
class Complex {
public:
	Complex() { real = 0; imag = 0; };  //默认构造函数,无形参
	Complex(double r, double i) {
		real = r;
		imag = i; //构造函数重载
	}
	Complex(double i) { real = i ; imag = 0; }  //转换构造函数
	friend Complex operator +(Complex x1, Complex x2);

	void display();
private:
	double real; //实部
	double imag;//虚部
};
Complex operator +(Complex x1, Complex x2) {
	Complex x;
	x.real = x1.real + x2.real;
	x.imag = x1.imag + x2.imag;
	return x;
}
void Complex::display() {
	if (imag <= 0) {
		cout << real << imag << "i" << endl;
	}
	else
		cout << real << "+" << imag << "i" << endl;
}
void main() {
	double r,i, r1, i1, r2, i2;
	double b;
	cout << "输入复数的实部和虚部" << endl;
	cin >> r1 >> i1 >> r2 >> i2;
	Complex   c1(r1, i1), c2(r2, i2), c3, c4,c5;
	c3 = c1 + c2;
	cout << "c1+c2="; c3.display();
	cout << "复数和整数相加,先输入复数c,再输入整数b";
	cin >>r>>i>> b;
	Complex c(r, i);
	c4 = b+c ;
	c5 = c + b;
	cout << "b+c="; c4.display();
	cout << "c+b="; c5.display();
}

执行结果为

3.)有两个矩阵 a 和 b,均为 2 行和 3 列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。
如: c=a+b。

#include<iostream>
#include<string>
using namespace std;
class zhen23 {
public:
	zhen23() {};
	void input();
	friend zhen23 operator + (zhen23 x1, zhen23 x2);   //重载+
	void display();
private:
	int x[2][3];
};
void zhen23::input() {
	int i, j;
	cout << "输入2x3矩阵的值" << endl;
	for (i = 0; i < 2; i++) {
		for (j = 0; j < 3; j++) {
			cin >> x[i][j];
		}
	}
}
zhen23 operator +(zhen23 x1, zhen23 x2) {
	zhen23 c;
	int i, j;
	for (i = 0; i < 2; i++) {
		for (j = 0; j < 3; j++) {
			c.x[i][j] = x1.x[i][j] + x2.x[i][j];
		}
	}
	return c;
}
void zhen23::display() {
	int i, j;
	cout << "输出矩阵为" << endl;
	for (i = 0; i < 2; i++) {
		for (j = 0; j < 3; j++) {
			cout << x[i][j] << " ";
			if (j == 2)
				cout << endl;
		}
	}
}
void main() {
	zhen23 a, b, c;
	a.input(); b.input();
	c = a + b;
	c.display();
}

 执行结果为

4.)声明一个 Teacher(教师)类和一个 Student(学生)类,二者有一部分数据成员是相同的,例如 num
(号码), name(姓名), sex(性别)。编写程序,将一个 Student 对象(学生)转换为 Teacher(教师)
类,只将以上 3 个相同的数据成员移植过去。可以设想为:一个学生大学毕业了,留校担任教师,他原有
的部分数据对现在的教师身份来讲仍然有用,应当保留并成为其教师数据的一部分。

#include<iostream>
#include<string>
using namespace std;
class Student {
public:
	Student() {};
	Student(string a, int h, string n ,string s) {
		duanwei = a;
		num = h;
		name = n;
		sex = s;
	}
	void display();
	string duanwei;
	int num;
	string name;
	string sex;
};

class Teacher {
public:
	Teacher();
	Teacher(Student &x) {
		num = x.num;
		name= x.name;
		sex= x.sex;
	}
	void display2();
private:
	int num;
	string name;
	string sex;
};
void Student::display() {
	cout << "学生资料" << endl;
	cout << "duanwei" << duanwei<< endl;
	cout << "num" << num << endl;
	cout << "name" << name << endl;
	cout << "sex" << sex<< endl;
}
void Teacher::display2() {
	cout << "老师的信息:" << endl;
	cout << "num" << num << endl;
	cout << "name" << name<< endl;
	cout << "sex" << sex<< endl;
}
void main() {
	Student c( "黑铁", 111, "王二", "男");
	c.display();
	Teacher t = Teacher(c);
	t.display2();
}

 执行结果为

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值