桂电C++面向对象程序设计实验 : 实验四 运算符重载

桂电C++面向对象程序设计实验: 实验四 运算符重载

实验内容:

(1)声明一个复数类Complex,重载运算符“+”,“-”,“*”,“/”,使之能用于复数的加、减、乘、除,运算符重载函数作为Complex 类的成员函数。编程序,分别求两个复数之和、差、积和商。本题是《C++面向对象程序设计》第4章第2题。
请思考:你编的程序能否用于一个整数与一个复数的算术运算?如4 + ( 5 - 2i )。
(2)声明一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如:c1 + c2,i + c1 , c1 + i均合法(设i为整数,c1 , c2 为复数)。
运行程序,分别求两个复数之和、整数和复数之和。本题是《C++面向对象程序设计》 第4章第3题。
(3)有两个矩阵a 和b,均为2行3列,求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加。如:c = a + b。本题是《C++面向对象程序设计》第4章第4题。
(4)声明一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为:一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。本题是《C++面向对象程序设计》第4章第7题。

源码

"1.cpp“

#include<iostream>
using namespace std;
class Complex
{
public:
	Complex(){ real = 0; image = 0; }
	Complex(double r, double i){ real = r; image = i; };
	Complex operator +(Complex &c2);
	Complex operator -(Complex &c2);
	Complex operator *(Complex &c2);
	Complex operator /(Complex &c2);
	void display();
private:
	double real;
	double image;
};
Complex Complex::operator +(Complex &c2)
{
	Complex c;
	c.real = real + c2.real;
	c.image = image + c2.image;
	return c;
}
Complex Complex::operator -(Complex &c2)
{
	Complex c;
	c.real = real - c2.real;
	c.image = image - c2.image;
	return c;
}
Complex Complex::operator *(Complex &c2)
{
	Complex c;
	c.real = real * c2.real-image*c2.image;
	c.image = image * c2.real+real*c2.image;
	return c;
}
Complex Complex::operator /(Complex &c2)
{
	Complex c;
	c.real = (real * c2.real + image*c2.image) / (c2.real*c2.real + c2.image*c2.image);
	c.image = (image * c2.real - real*c2.image) / (c2.real*c2.real + c2.image*c2.image);
	return c;
}
void Complex::display()
{
	cout << "(" << real << "," << image << "i)" << endl;
}
int main()
{
	Complex c1(1, 2), c2(-3, -4), c3;
	cout<<"c1:";
	c1.display();
	cout<<"c2:";
	c2.display();
	c3 = c1 + c2;
	cout << "c1+c2=";
	c3.display();
	c3 = c1 - c2;
	cout << "c1-c2=";
	c3.display();
	c3 = c1*c2;
	cout << "c1*c2=";
	c3.display();
	c3 = c1 / c2;
	cout << "c1/c2=";
	c3.display();
}

"2.cpp"

#include<iostream>//2
using namespace std;
class Complex
{
public:
	Complex(){ real = 0; image = 0; }
	Complex(double r, double i){ real = r; image = i; };
	Complex operator +(Complex &c2);
	Complex operator +(int &i);
	friend Complex operator+(int &, Complex&);
	void display();
private:
	double real;
	double image;
};
Complex Complex::operator +(Complex &c2)
{
	Complex c;
	c.real = real + c2.real;
	c.image = image + c2.image;
	return c;
}
Complex Complex::operator +(int &i)
{
	Complex c;
	c.real = real + i;
	c.image = image;
	return c;
}
Complex operator+(int &i, Complex&c1)
{
	Complex c;
	c.real = c1.real + i;
	c.image = c1.image;
	return c;
}
void Complex::display()
{
	cout << "(" << real << "," << image << ")" << endl;
}
int main()
{
	int i = 4;
	Complex c1(3, 4), c2(5, -6), c3;
	cout <<"i="<<i << endl;
	c3 = c1 + c2;
	cout << "c1+c2=";
	c3.display();
	c3 = i + c1;
	cout << "i+c1=";
	c3.display();
	c3 = c1 + i;
	cout << "c1+i=";
	c3.display();

"3.cpp"

#include<iostream>
using namespace std;
class  jz
{
public:
	jz();
	friend jz operator+(jz&, jz&);
	void input();
	void display();
private:
	int A[2][3];
};
jz::jz()
{
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			A[i][j] = 0;
}
jz operator+(jz&a, jz&b)
{
	jz c;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			c.A[i][j] = a.A[i][j] + b.A[i][j];
	return c;
}
void jz::input()
{
	cout << "请输入矩阵元素:"<<endl;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			cin >> A[i][j];
}
void jz::display()
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
			cout << A[i][j] << " ";
		cout << endl;
	}
}
int main()
{
	jz a, b, c;
	a.input();
	cout<<"矩阵a为:"<<endl;
	a.display();
	b.input();
	cout<<"矩阵b为:"<<endl;
	b.display();
	cout << "两个矩阵相加之后的矩阵c为:" << endl;
	c = a + b;
	c.display();
}

"4.cpp"

#include <iostream>
#include <string>
using namespace std;
class Student
{
public:
	Student(string, string, string, float);
	string get_num(){return num;}
	string get_name(){return name;}
	string get_sex(){return sex;}
	void display()
	{
		cout<<"号码:"<<num<<endl;
		cout<<"姓名:"<<name<<endl;
		cout<<"性别:"<<sex<<endl; 
		cout<<"成绩:"<<score<<endl;
	}
private:
	string num;
	string name;
	string sex;
	float score;
};

Student::Student(string n, string nam, string s, float so)
{
	num = n;
	name= nam;
	sex = s;
	score = so;
}

class Teacher
{
public:
	Teacher(){}
	Teacher(Student&);
	Teacher(string n, string nam, string sex, float pay);
	void display();
private:
	string num;
	string name;
	string sex;
	float pay;
};

Teacher::Teacher(string n, string nam, string s, float p)
{
	num = n;
	name=nam;
	sex = s;
	pay = p;
}

Teacher::Teacher(Student& s)
{
	num = s.get_num();
	name=s.get_name();
	sex =s.get_sex(); 
	pay = 6666.6;
}

void Teacher::display()
{
	cout<<"号码:"<< num<<endl; 
	cout<<"姓名:"<< name<<endl; 
	cout<<"性别:"<< sex<<endl; 
	cout<<"工资:"<< pay << endl;
}


int main()
{
	Teacher t1("110", "张三", "男", 3333.3), t2;
	Student s1("120", "李四", "男", 88.8);
	cout << "学生" << endl;
	s1.display();
	t2 = Teacher(s1);
	cout << "老师" << endl;
	t2.display();
	return 0;
}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值