C++ 面向对象 - 运算符重载与友元函数

C++ 面向对象 - 运算符重载与友元函数

C++ 预定义的运算符的操作对象只能是基本数据类型,但是实际上许多用户自定义类型(例如类),也需要类似的运算符操作,C++ 可以通过运算符重载来满足这种需求。运算符重载本质就是函数重载,也是 C++ 多态的一种形式。

同样 C++ 的封装性和数据隐藏性,只有类自身的函数才可以不受限制的访问类的非公有成员;任何非成员函数直接访问类中非公有数据成员会导致编译错误。于是针对这一情况 C++ 推出了友元函数。

本实训我们就来学习运算符重载和友元函数的使用。

第1关 复数运算

/********* Begin *********/
class Complex
{
	//复数类的声明
    private:
		float real;
		float image;
    public:
	    Complex(float r,float i){
			real = r;
			image = i;
		}

		
		Complex operator + (Complex &a){
			return Complex (real + a.real, image + a.image);
		}
		Complex operator - (Complex &a){
			return Complex (real - a.real, image - a.image);
		}
		Complex operator * (Complex &a){
			return Complex(real*a.real - image*a.image, image*a.real + a.image*real);
		}

		
		void Print(){
			if(image < 0)
				cout << real << image << 'i' << "\n";
			else
				cout << real << '+' << image << 'i' << "\n";
		};
    
};
//复数类的定义



/********* End *********/

第2关 学生信息转换

/********* Begin *********/
// 前置声明 Teacher 类
class Teacher;

class Student
{
	//学生类的声明
    private:
		int number;
		string name;
		string sex;
	
	public:
		Student(int num,string nam,string se){
			number = num;
    		name = nam;
    		sex = se;
		}
		void Print(){
			cout<< "学生:" << name << ",编号:" <<number<< ",性别:" << sex << "\n";
		}
		friend class Teacher; 
    
    
};
//学生类的定义




class Teacher
{
	//教师类的声明
    private:
		int number;
		string name;
		string sex;
	public:
		Teacher(int num,string nam,string se){
			number = num;
    		name = nam;
    		sex = se;
		}
		Teacher (Student &stu){
			number = stu.number;
    		name = stu.name;
    		sex = stu.sex;
		}

		void Print(){
			cout<< "教师:" << name << ",编号:" << number << ",性别:" << sex << "\n";
		}
    
    
};
//教师类的定义


/********* End *********/

第3关 矩阵运算

/********* Begin *********/
class Matrix
{
	//矩阵类的声明
    private:
    	int r, c; 
		int p[99][99];
	
	public:
		Matrix(int r,int c){
			this -> r = r;
			this -> c = c;
		}
		void Fill(int value){
			for (int i = 0; i < r; i++)
				for (int j = 0; j < c; j++)
					p[i][j] = value;
		}
		void Set(int r,int c,int value){
			p[r][c] = value;
		}
		int Get(int r,int c){
			return p[r][c];
		}
		void Print(){
			for(int i = 0; i < r; i++)
			{
				for (int j = 0; j < c; j++)
					cout << p[i][j] << " ";
				cout << "\n";
			}
		}

		friend Matrix operator + (Matrix &m1, Matrix &m2){
			int R = m1.r, C = m1.c;
			Matrix t(R, C);
			for(int i = 0; i < R; i++)
				for (int j = 0; j < C; j++)
					t.p[i][j]  = m1.p[i][j] + m2.p[i][j];
			return t;
		}
		friend Matrix operator - (Matrix &m1, Matrix &m2){
			int R = m1.r, C = m1.c;
			Matrix t(R, C);
			for(int i = 0; i < R; i++)
				for (int j = 0; j < C; j++)
					t.p[i][j]  = m1.p[i][j] - m2.p[i][j];
			return t;
		}
		friend Matrix operator * (Matrix &m1, Matrix &m2){
			int R = m1.r, C = m2.c, x = m2.r;
			Matrix t(R, C);
	    	for (int i = 0; i < R; i++) {
				for (int j = 0; j < C; j++) {
					int s = 0;
					for (int k = 0; k < x; k++) 
						s += m1.p[i][k] * m2.p[k][j]; 
					t.p[i][j] = s; 
					//cout << t.p[i][j] << " ";
				}
			}
			return t;
		}

};
//矩阵类的定义




/********* End *********/
  • 15
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码不会敲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值