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

第一题:复数运算

#include 
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<

第二题:学生信息转换

#include 
#include 
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<<"学生:"<

第三题:矩阵运算

#include 
#include 
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 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;ijuzhen[i][j] <<" ";//注意输出格式
        }
        cout<

你可能感兴趣的:(c++,蓝桥杯,开发语言)