10.1 重载云算符“+”。非成员、非友元的普通函数。
#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();double get_imag();void display();
private:double real;double imag;
};double Complex::get_real()
{return real;
}double Complex::get_imag()
{return imag;
}void Complex::display()
{if(imag>=0)cout << "(" << real << "+" << imag << "i)" << endl;elsecout << "(" << 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;
}
10.2 重载运算符 +-*/ 作为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()
{if(imag>=0)cout << "(" << real << "+" << imag << "i)" << endl;elsecout << "(" << 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;
}
10.3 重载运算符“+”,参加运算的两个运算量可以都是类对象,也可以有整数,顺序任意。
#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;
}
10.4 有两个矩阵a,b,均为2行3列,求矩阵和。重载运算符“+”
#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();cout << endl << "Matrix a:" << endl;a.display();b.input();cout << endl << "Matrix b:" << endl;b.display();c = a + b; //用重载运算符“+”实现两个矩阵相加cout << endl << "Matrix c = Matrix a + Matrix b :" << endl;c.display();return 0;
}
10.5 基于10.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;cout << endl << "Matrix a:" << endl << a << endl;cin >> b;cout << endl << "Matrix b:" << endl << b << endl;c = a + b;cout << endl << "Matrix c = Matrix a + Matrix b :" << endl << c << endl;return 0;
}
10.6 处理一个复数与一个double数相加的运算,结果存放在一个double型的变量d1中输出,再以复数形式输出。成员函数中包含重载类型转换运算符。
#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;cout << "d1=" << d1 << endl;c2 = Complex(d1);cout << "c2=";c2.display();return 0;
}
10.7 定义一个teacher(教师)类和一个student(学生)类,二者有一份数据成员是相同的,例如num,name,sex。编写程序,将一个student对象(学生)转换为teacher(教师)类,只将以上3个相同的数据成员移植过去。可以设想为:一个学生大学毕业了,留校担任教师,他原有的部分数据对现在的教师身份来说仍然是有用的,应当保留并成为其教师的数据的一部分。
#include<iostream>
#include<cstring>
using namespace std;class Student
{
public:Student(int n, const char nam[], char s, float sco){num = n;strcpy(name, nam);sex = s;score = sco;}int get_num() {return num;}char* get_name() {return name;}char get_sex() {return sex;}void display(){cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\nscore:" << score << "\n\n";}private:int num;char name[20];char sex;float score;
};class Teacher
{
public:Teacher(){}Teacher(int n, const char nam[], char s, float p){num = n;strcpy(name, nam);sex = s;pay = p;}Teacher(Student&);void display(){cout << "num:" << num << "\nname:" << name << "\nsex:" << sex << "\npay:" << pay << "\n\n";}
private:int num;char name[20];char sex;float pay;
};Teacher::Teacher(Student& stud)
{num = stud.get_num();strcpy(name, stud.get_name());sex = stud.get_sex();pay = 1500;
}int main()
{Student student1(20010, "Wang", 'm', 89.5);Teacher teacher1(10001, "Li", 'f', 1234.5), teacher2;cout << "student1:" << endl;student1.display();cout << "teacher1:" << endl;teacher1.display();teacher2 = Teacher(student1);cout << "teacher2:" << endl;teacher2.display();return 0;
}