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

第一关

#include <iostream>
using namespace std;

/********* Begin *********/

class Complex
{
	//复数类的声明
public:
	float real;
	float image;
	Complex(float r,float i){
		real=r;
		image=i;
	}
	void Print(){
		if(image<0){
			cout<<real<<image<<'i'<<endl;
		}else{
			cout<<real<<'+'<<image<<'i'<<endl;
		}
	}  
	

};
//复数类的定义
Complex operator+ (Complex& t1,Complex& t2){
		Complex t(0,0);
		t.real=t1.real+t2.real;
		t.image=t1.image+t2.image;
		return t;
	}  
    Complex operator-(Complex& t1,Complex& t2){
		Complex t(0,0);
		t.real=t1.real-t2.real;
		t.image=t1.image-t2.image;
		return t;
	} 
	Complex operator*(Complex &t1,Complex &t2){
		Complex t(0,0);
		t.real=t1.real*t2.real-t1.image*t2.image;
		t.image=t1.image*t2.real+t1.real*t2.image;
		return t;
	}  


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

第二关

class Virus
{
	//病毒类的声明
public:
int Gen;
Virus(){
	Gen=0;
}
Virus (const Virus &v){
	this->Gen=v.Gen+1;
}
    
    
};
//病毒类的定义以及其他内容
bool operator==(const int& g,const Virus &v){
	if(v.Gen==g){
		return true;
	}else{
		return false;
	}
}

 第三关

#include <iostream>
#include <string>
using namespace std;

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

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




class Teacher
{
	//教师类的声明
 public:
   Teacher(const Student &t){
	   this->number=t.number;
	   this->name=t.name;
	   this->sex=t.sex;
   }
 Teacher(int num,string nam,string se){
	   number=num;
      name=nam;
     sex=se;
  }
  void Print(){
	  cout<<"教师:"<<name<<",编号:"<<number<<",性别:"<<sex<<endl;
  }     
  private:
  int number;
  string name;
  string sex;  
    
};
//教师类的定义

第四关

注意矩阵计算的一些知识,特别是乘法

#include <iostream>
#include <string>
using namespace std;

/********* Begin *********/
class Matrix
{
	//矩阵类的声明
    public:
	friend Matrix operator+(Matrix& m1,Matrix &m2);
	friend Matrix operator-(Matrix& m1,Matrix &m2);
	friend Matrix operator*(Matrix& m1,Matrix &m2);
	Matrix(int r=0,int c=0){
		row=r;
		col=c;
		/*for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				data[i][j]=0;
			}
		}*/
	}
	void Fill(int val){
		
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				data[i][j]=val;
			}
		}
	}
	void Set(int r,int c,int val){
		data[r][c]=val;
		
	}
	void Print(){
		for(int i=0;i<row;i++){
			for(int j=0;j<col;j++){
				cout<<data[i][j]<<' ';
			}
			cout<<endl;
		}
	}
	private:
	int row;
	int col;
    
	int data[100][100];
    
};
//矩阵类的定义



Matrix operator+(Matrix& m1,Matrix &m2)
{
    //实现矩阵加法
    Matrix t;
	t.row=m1.row;
	t.col=m1.col;
	for(int i=0;i<m1.row;i++){
		for(int j=0;j<m1.col;j++){
			t.data[i][j]=m1.data[i][j]+m2.data[i][j];
		}
	}
    return t;
    
}

Matrix operator-(Matrix& m1,Matrix &m2)
{
    //实现矩阵减法
    Matrix t;
	t.row=m1.row;
	t.col=m1.col;
	for(int i=0;i<m1.row;i++){
		for(int j=0;j<m1.col;j++){
			t.data[i][j]=m1.data[i][j]-m2.data[i][j];
		}
	}
    return t;
    
    
}

Matrix operator*(Matrix& m1,Matrix &m2)
{
    //实现矩阵乘法
    Matrix t;
	t.row=m1.row;
	t.col=m2.col;
	for(int i=0;i<m1.row;i++){
		for(int j=0;j<m2.col;j++){
			
			int temp=0;
			for(int k=0;k<m2.row;k++){
				temp+=m1.data[i][k]*m2.data[k][j];
			}
			
			t.data[i][j]=temp;
		}
	}
    return t;
    
    
}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值