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

第一题:复数运算

#include <iostream>
using namespace std;

/********* Begin *********/
class Complex
{friend Complex operator+(Complex &c1,Complex &c2);
 friend Complex operator-(Complex &c1,Complex &c2);
 friend Complex operator*(Complex &c1,Complex &c2);
 public:
    Complex()
	{ 
		real=0;
	   image=0;

	}
    Complex(float r,float i);//有参构造函数
	void Print();//打印函数
	//复数类的声明
 private:
	float real;
    float image;
    
};
//复数类的定义
Complex::Complex(float r,float i)//定义构造函数
{ 
	real=r;
    image=i;

}
void Complex::Print()//定义输出函数
{    if(image<0)
	    cout<<real<<image<<"i"<<endl;
		else 
		    cout<<real<<"+"<<image<<"i"<<endl;

}


/********* End *********/
// 重载加法运算符,实际对 Complex 类中的 c1,c2 成员变量进行加法运算  
Complex operator+(Complex &c1,Complex &c2)
{  Complex c; //定义一个临时变量c
    c.real=c1.real+c2.real;
	c.image=c1.image+c2.image;
	return c;


}
// 重载加法运算符,实际对 Complex类中的 c1,c2 成员变量进行减法运算
Complex operator-(Complex &c1,Complex &c2)
{  Complex c; //定义一个临时变量c
    c.real=c1.real-c2.real;
	c.image=c1.image-c2.image;
	return c;

}
//z1=a+bi,z2=c+di(a、b、c、d∈R)是任意两个复数,那么它们的积(a+bi)(c+di)=(ac-bd)+(bc+ad)i.
// 重载加法运算符,实际对 Complex 类中的 c1,c2 成员变量进行乘法运算
Complex operator*(Complex &c1,Complex &c2)
{   Complex c; //定义一个临时变量c
      c.real=c1.real*c2.real-c1.image*c2.image;
	  c.image=c1.image*c2.real+c1.real*c2.image;
	  return c;
}

第二题:学生信息转换

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

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

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



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


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

第三题:矩阵运算

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

/********* Begin *********/
class Matrix
{
	//矩阵类的声明
    private:
    int hight;//长
    int width;//宽
    int juzhen[10][10];//定义一个二元数组来存矩阵元素
    public:
    Matrix();
    Matrix(int r,int c);//r:行,c:列
    void Fill(int value);//函数将矩阵内所有的元素都设置为参数 value 的值。
    void Set(int r,int c,int value);//函数将矩阵第 r 行 c 列的元素设置为 value 的值。
    int Get(int r,int c);//函数返回矩阵第 r 行 c 列的元素。
    void Print();
    friend Matrix operator+(Matrix& m1,Matrix &m2);
    friend Matrix operator-(Matrix& m1,Matrix &m2);
    friend Matrix operator*(Matrix& m1,Matrix &m2);
};
//矩阵类的定义
Matrix::Matrix(){

}
Matrix::Matrix(int r,int c){
    hight=r;
    width=c;
}

void Matrix:: Fill(int value){
    for(int i =0;i<hight;i++){
        for(int j =0;j<width;j++){
           this -> juzhen[i][j]=value;//下面还有使用数组的地方,所有用this关键字去赋值
        }
    }
}
void Matrix:: Set(int r,int c,int value){
    this->juzhen[r][c]=value;
}
int Matrix:: Get(int r,int c){
    return this->juzhen[r-1][c-1];
}
void Matrix:: Print(){//矩阵的输出
    for(int i =0;i<hight;i++){
        for(int j =0;j<width;j++){
          cout<< this->juzhen[i][j] <<" ";//注意输出格式
        }
        cout<<endl;
    }
}
Matrix operator+(Matrix& m1,Matrix &m2)
{
    //实现矩阵加法
    Matrix temp(m1.hight,m2.width);//定义一个temp数组去存矩阵加法后的和值
    for(int i =0;i<m1.hight;i++){
        for(int j =0;j<m1.width;j++){//j<m1的长度是因为,加只加那一个值,而不是整列
            temp.juzhen[i][j]=m1.juzhen[i][j]+m2.juzhen[i][j];
        }
    }
    return temp;
}

Matrix operator-(Matrix& m1,Matrix &m2)
{
    //实现矩阵减法
    Matrix temp(m1.hight,m2.width);//定义一个temp数组去存矩阵加法后的和值
    for(int i =0;i<m1.hight;i++){
        for(int j =0;j<m1.width;j++){
            temp.juzhen[i][j]=m1.juzhen[i][j]-m2.juzhen[i][j];
        }
    }
       return temp;
    
}

Matrix operator*(Matrix& m1,Matrix &m2)
{
    //实现矩阵乘法
    Matrix temp(m1.hight,m2.width);//定义一个temp数组去存矩阵加法后的和值
    for (int i = 0; i < m1.hight; i++) {
			for (int j = 0; j < m2.width; j++) {
				long sum=0;
				for (int k = 0; k < m1.width; k++) {
					sum += m1.juzhen[i][k] * m2.juzhen[k][j];    //累加第i行与第j列的元素乘积
				}
				temp.juzhen[i][j] = sum;   //赋给i,j元素
			}
		}
		return temp;
    
}
/********* End *********/

  • 20
    点赞
  • 118
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值