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 *********/

你可能感兴趣的:(头歌C++面向对象实训,c++)