C++程序设计(第3版)谭浩强 第10章 习题

1.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。

【解】

//第10章运算符重载 习题
//10.1
//1.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
//  将运算符函数重载为非成员、非友元的普通函数。编写程序,求两个复数之和。

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex() { real = 0; imag = 0; }
	Complex(double r, double i) { real = r; imag = i; }
	double get_real();                  		//声明get_real函数 
	double get_imag();                 		//声明get_imag函数
	void display();
private:
	double real;
	double imag;
};
double Complex::get_real()               		//取数据成员real(实部)的值
{
	return real;
}
double Complex::get_imag()               	//取数据成员imag(虚部)的值
{
	return imag;
}
void Complex::display()
{
	cout << "(" << real << "," << imag << "i)" << endl;
}
Complex operator+(Complex &c1, Complex &c2)
{
	return Complex(c1.get_real() + c2.get_real(), c1.get_imag() + c2.get_imag());
}
int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	c3 = c1 + c2;
	cout << "c3=";
	c3.display();
	return 0;
}

//c3 = (8, – 6i)

2.定义一个复数类Complex,重载运算符“+”,“-”,“[^”,“/”,使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。编写程序,分别求两个复数之和、差、积和商。

【解】

//10.2
//2.定义一个复数类Complex,重载运算符“+”,“-”,“”,“/”,
//  使之能用于复数的加、减、乘、除。运算符重载函数作为Complex类的成员函数。
//  编写程序,分别求两个复数之和、差、积和商。

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex() { real = 0; imag = 0; }
	Complex(double r, double i) { real = r; imag = i; }
	Complex operator+(Complex &c2);
	Complex operator-(Complex &c2);
	Complex operator*(Complex &c2);
	Complex operator/(Complex &c2);
	void display();
private:
	double real;
	double imag;
};
Complex Complex::operator+(Complex &c2)//重载运算符“+”
{
	Complex c;
	c.real = real + c2.real;//计算实部
	c.imag = imag + c2.imag;//计算虚部
	return c;
}
Complex Complex::operator-(Complex &c2)//重载运算符“–”
{
	Complex c;
	c.real = real-c2.real;//计算实部
	c.imag = imag-c2.imag;//计算虚部
	return c;
}
Complex Complex::operator*(Complex &c2)//重载运算符“”
{
	Complex c;
	c.real = real * c2.real - imag * c2.imag;//计算实部
	c.imag = imag * c2.real + real * c2.imag;//计算虚部
	return c;
}
Complex Complex::operator/(Complex &c2)//重载运算符“/”
{
	Complex c;
	c.real = (real / c2.real + imag / c2.imag) / (c2.real / c2.real + c2.imag / c2.imag);//计算实部
	c.imag = (imag / c2.real + real / c2.imag) / (c2.real / c2.real + c2.imag / c2.imag);//计算虚部
	return c;
}
void Complex::display()
{
	cout << "(" << real << "," << imag << "i)" << endl;//输出复数
}
int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	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();
	return 0;
}

//c1 + c2 = (8, -6i)
//c1 - c2 = (–2, 14i)
//c1 * c2 = (55, –10i)
//c1 / c2 = (-0.2, 0.4i)

3.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。例如,c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编写程序,分别求两个复数之和、整数和复数之和。

【解】

//10.3
//3.定义一个复数类Complex,重载运算符“+”,使之能用于复数的加法运算。
//  参加运算的两个运算量可以都是类对象,也可以其中有一个是整数,顺序任意。
//  例如,c1+c2,i+c1,c1+i均合法(设i为整数,c1,c2为复数)。编写程序,
//  分别求两个复数之和、整数和复数之和。

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex() { real = 0; imag = 0; }
	Complex(double r, double i) { real = r; imag = i; }
	Complex operator+(Complex &c2);                    	//运算符重载为成员函数
	Complex operator+(int &i);                         	//运算符重载为成员函数
	friend Complex operator+(int&, Complex &);          	//运算符重载为友元函数
	void display();
private:
	double real;
	double imag;
};
Complex Complex::operator+(Complex &c)               	//定义成员运算符函数
{
	return Complex(real + c.real, imag + c.imag);
}
Complex Complex::operator+(int &i)                  	//定义成员运算符函数
{
	return Complex(real + i, imag);
}
void Complex::display()
{
	cout << "(" << real << "," << imag << "i)" << endl;
}
Complex operator+(int &i, Complex &c)               	//定义友元运算符函数
{
	return Complex(i + c.real, c.imag);
}
int main()
{
	Complex c1(3, 4), c2(5, -10), c3;
	int i = 5;
	c3 = c1 + c2;
	cout << "c1+c2=";
	c3.display();
	c3 = i + c1;
	cout << "i+c1=";
	c3.display();
	c3 = c1 + i;
	cout << "c1+i=";
	c3.display();
	return 0;
}

//c1 + c2 = (8, – 6i)
//i + c1 = (8, 4i)
//c1 + i = (8, 4i)

4.有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加(如c=a+b)。

【解】

//10.4
//4.有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,
//  使之能用于矩阵相加(如c=a+b)。

#include <iostream>
using namespace std;
class Matrix                                        	//定义Matrix类
{
public:
	Matrix();                                     	//默认构造函数
	friend Matrix operator+(Matrix &, Matrix &);      		//重载运算符“+”
	void input();                                    	//输入数据函数
	void display();                                  	//输出数据函数
private:
	int mat[2][3];
};
Matrix::Matrix()                                     	//定义构造函数
{
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			mat[i][j] = 0;
}
Matrix operator+(Matrix &a, Matrix &b)               	//定义重载运算符+函数
{
	Matrix c;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
		{
			c.mat[i][j] = a.mat[i][j] + b.mat[i][j];
		}
	return c;
}
void Matrix::input()                                  	//定义输入数据函数
{
	cout << "input value of matrix:" << endl;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			cin >> mat[i][j];
}
void Matrix::display()                              	//定义输出数据函数
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			cout << mat[i][j] << " ";
		}
		cout << endl;
	}
}
int main()
{
	Matrix a, b, c;
	a.input();
	b.input();
	cout << endl << "Matrix a:" << endl;
	a.display();
	cout << endl << "Matrix b:" << endl;
	b.display();
	c = a + b;                                       	//用重载运算符“+”实现两个矩阵相加
	cout << endl << "Matrix c = Matrix a + Matrix b :" << endl;
	c.display();
	return 0;
}

//input value of matrix : 11 22 33 44 55 66↙
//input value of matrix : 12 13 14 15 16 17↙
//
//Matrix a :
//11 22 33
//44 55 66
//
//Matrix b :
//12 13 14
//15 16 17
//
//Matrix c = Matrix a + Matrix b :
//23 35 47
//59 71 83

5.在第4题的基础上,重载流插入运算符“<<”和流提取运算符“>>”,使之能用于该矩阵的输入和输出。

【解】

//10.5
//5.在第4题的基础上,重载流插入运算符“<<”和流提取运算符“>>”,
//  使之能用于该矩阵的输入和输出。

#include <iostream>
using namespace std;
class Matrix
{
public:
	Matrix();
	friend Matrix operator+(Matrix &, Matrix &);   		//重载运算符“+”的函数声明
	friend ostream& operator<<(ostream&, Matrix&); 	//重载运算符“<<”的函数声明
	friend istream& operator>>(istream&, Matrix&);  	//重载运算符“>>”的函数声明
private:
	int mat[2][3];
};
Matrix::Matrix()
{
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			mat[i][j] = 0;
}
Matrix operator+(Matrix &a, Matrix &b)            	//定义运算符“+”的重载函数
{
	Matrix c;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
		{
			c.mat[i][j] = a.mat[i][j] + b.mat[i][j];
		}
	return c;
}
istream& operator>>(istream &in, Matrix &m)       	//定义运算符“>>”的重载函数
{
	cout << "input value of matrix:" << endl;
	for (int i = 0; i < 2; i++)
		for (int j = 0; j < 3; j++)
			in >> m.mat[i][j];
	return in;
}
ostream& operator<<(ostream &out, Matrix &m)       	//定义运算符“<<”的重载函数
{
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			out << m.mat[i][j] << " ";
		}
		out << endl;
	}
	return out;
}
int main()
{
	Matrix a, b, c;
	cin >> a;                                     	//用cin输入矩阵
	cin >> b;
	cout << endl << "Matrix a:" << endl << a << endl;      	//用cout输出矩阵
	cout << endl << "Matrix b:" << endl << b << endl;
	c = a + b;
	cout << endl << "Matrix c = Matrix a + Matrix b :" << endl << c << endl;
	return 0;
}

6.请编写程序,处理一个复数与一个double数相加的运算,结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。定义Complex(复数)类,在成员函数中包含重载类型转换运算符: operator double(){return real;}

【解】

//10.6
//6.请编写程序,处理一个复数与一个double数相加的运算,
//  结果存放在一个double型的变量d1中,输出d1的值,再以复数形式输出此值。
//  定义Complex(复数)类,在成员函数中包含重载类型转换运算符:
//  operator double(){return real;}

#include <iostream>
using namespace std;
class Complex
{
public:
	Complex() { real = 0; imag = 0; }
	Complex(double r) { real = r; imag = 0; }
	Complex(double r, double i) { real = r; imag = i; }
	operator double() { return real; }	  //重载类型转换运算符
	void display();
private:
	double real;
	double imag;
};

void Complex::display()
{
	cout << "(" << real << ", " << imag << ")" << endl;
}

int main()
{
	Complex c1(3, 4), c2;
	double d1;
	d1 = 2.5 + c1;				  //将c1转换为double型数, 与2.5相加, 结果为double型数
	cout << "d1=" << d1 << endl; 		  //输出double型变量d1的值
	c2 = Complex(d1);         	  //将d1再转换为复数
	cout << "c2=";
	c2.display();           		  //输出复数c2 
	return 0;
}

//d1 = 5.5
//c2 = (5.5, 0)

7. 定义一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是相同的,例如num(号码),name(姓名),sex(性别)。编写程序,将一个Student对象(学生)转换为Teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为: 一位学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师数据的一部分。

【解】

//10.7
//7.定义一个Teacher(教师)类和一个Student(学生)类,二者有一部分数据成员是相同的,
//  例如num(号码),name(姓名),sex(性别)。编写程序,
//  将一个Student对象(学生)转换为Teacher(教师)类,
//  只将以上3个相同的数据成员移植过去。
//  可以设想为: 一位学生大学毕业了,留校担任教师,
//  他原有的部分数据对现在的教师身份来说仍然是有用的,
//  应当保留并成为其教师数据的一部分。

#include <iostream>
using namespace std;
class Student//定义学生类
{
public:
	Student(int, char[], char, float);//构造函数声明
	int get_num() { return num; }//返回num的值
	char get_name() { return name; }//返回name的值
	char get_sex() { return sex; }//返回sex的值
	void display()
	{
		cout << "num:" << num << " \nname:" << name << " \nsex:" 
			<< sex << " \nscore:" << score << " \n\n";
	}
private:
	int num;
	char name[20];
	char sex;
	float score;//成绩
};
Student::Student(int n, char nam[], char s, float sco)//定义Student构造函数
{
	num = n;
	strcpy(name, nam);
	sex = s;
	score = sco;
}
class Teacher//定义教师类
{
public:
	Teacher() {}//默认构造函数
	Teacher(Student&);//转换构造函数
	Teacher(int n, char nam[], char sex, float pay);//构造函数重载
	void display();
private:
	int num;
	char name[20];
	char sex;
	float pay;//工资
};
Teacher::Teacher(int n, char nam[], char s, float p)//定义Teacher构造函数
{
	num = n;
	strcpy(name, nam);
	sex = s;
	pay = p;
}
Teacher::Teacher(Student& stud)//定义转换构造函数
{
	num = stud.get_num();//将Student类对象的num成员转换为Teacher类对象的num
	strcpy(name, stud.get_name());
	sex = stud.get_sex();
	pay = 1500;//假定试用期临时工资一律为1500元
}
void Teacher::display()//输出教师的信息
{
	cout << "num:" << num << " \nname:" << name << " \nsex:" 
		<< sex << " \npay:" << pay << " \n\n";
}

int main()
{
	Teacher teacher1(10001, "Li", 'f ', 1234.5), teacher2;
	Student student1(20010, "Wang", 'm', 89.5);
	cout << "student1:" << endl;
	student1.display();//输出学生student1的信息
	teacher2 = Teacher(student1);//将student1转换为Teacher类对象  
	cout << "teacher2:" << endl;
	teacher2.display();//输出教师teacher2的信息
	return 0;
}

//student1 :
//num:20010
//name : Wang
//sex : m
//score : 89.5
//
//Teacher2 :
//num : 20010
//name : Wang
//sex : m
//pay : 1500

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-23 23:04:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-23 23:04:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-23 23:04:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-23 23:04:02       18 阅读

热门阅读

  1. flume案例

    2024-01-23 23:04:02       34 阅读
  2. Kafka-服务端-API层

    2024-01-23 23:04:02       35 阅读
  3. linux-ubuntu-apt 2--各种配置文件详解

    2024-01-23 23:04:02       37 阅读
  4. Redis面试题25

    2024-01-23 23:04:02       27 阅读
  5. BOSS直聘推荐搜索系统工程师校招面经

    2024-01-23 23:04:02       33 阅读
  6. leetcode 2788按分隔符拆分字符串

    2024-01-23 23:04:02       38 阅读
  7. 参数校验: spring-boot-starter-validation

    2024-01-23 23:04:02       31 阅读
  8. GPT只是开始,Autonomous Agents即将到来

    2024-01-23 23:04:02       33 阅读
  9. 结构体(C语言)

    2024-01-23 23:04:02       32 阅读