运算符重载定义格式:
<类型><类名>:::operator<操作符>(参数表)
举例如下:重载加法运算符的虚数运算
Complex operator+(Complex&);//返回的类型也是虚数类,圆括号里是对另一个虚数的引用
Complex operator+(double);// 右边是一个双精度的引用对象
bool operator==(Complex);
#include<iostream>
#include<string.h>
using namespace std;
class Complex{
private:
double real,imag;
public:
Complex(double r=0,double i=0):real(r),imag(i){}
double Real(){return real;}
double Imag(){return imag;}
Complex operator+(Complex&);//返回的类型也是虚数类,圆括号里是对另一个虚数的引用
Complex operator+(double);// 右边是一个双精度的引用对象
bool operator==(Complex);
~Complex(){};
};
Complex Complex::operator+(Complex&c){
Complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}
Complex Complex::operator+(double d){
Complex temp;
temp.real=real+d;
temp.imag=imag;
return temp;
}
bool Complex::operator==(Complex c){
if(real==c.real&&imag==c.imag){
return true;
}
else{
return false;
}
}
int main(){
Complex c1(3,4),c2(5,6),c3;
cout<<"c1="<<c1.Real()<<"+j"<<c1.Imag()<<endl;
cout<<"c2="<<c2.Real()<<"+j"<<c2.Imag()<<endl;
c3=c1+c2;
cout<<"c3="<<c3.Real()<<"+j"<<c3.Imag()<<endl;
c3=c3+6.5;
cout<<"c3+6.5="<<c3.Real()<<"+j"<<c3.Imag()<<endl;
if(c1==c2){
cout<<"两个复数相等"<<endl;
}
else{
cout<<"两个复数不相等"<<endl;
}
return 0;
}
2、发现一道题融合了运算符重载以及动态分配空间(姐要拿下!!)(最难就考到这吧)
重载矩阵加法运算
题目内容:
编写一个矩阵类,重载矩阵加法运算。设 A,B,C 均为 m 行,n 列的矩阵,要求程序能实现 C=A+B 的操作。
#include<iostream>
#include<string.h>
using namespace std;
class Matrix {
private:
int m,n,index;
double *data;
public:
Matrix():m(0),n(0),index(0),data(NULL){};
Matrix(int mm,int nn):m(mm),n(nn),index(0){
data=new double[m*n];
}
~Matrix(){};
void intdata(){
for(int i=0;i<m*n;i++){
cin>>data[i];
}
}
void showdata(){
index=0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(j<n-1){
cout<<data[index]<<" ";
}
else{
cout<<data[index]<<endl;
}
index++;
}
}
}
Matrix operator+(Matrix &mat){
Matrix temp(m,n);
for(int i=0;i<m*n;i++){
temp.data[i]=data[i]+mat.data[i];
}
return temp;
}
};
int main(){
int m,n;
cin>>m>>n;
Matrix m1(m,n);
Matrix m2(m,n);
m1.intdata();
m2.intdata();
Matrix *m3=new Matrix(m1+m2);
m3->showdata();
delete m3;
return 0;
}
3、omg发现了一道更难的(流泪)
纯虚函数与基类指针数组的应用
题目内容:
定义抽象基类 Shape, 其中纯虚函数 printName()输出几何图形的名称和相应的成员数据、纯虚函数 printArea()计算几何图形的面积。
并由 Shape 类派生出 5 个派生类:Circle(圆形),数据成员为半径、Square(正方形) ,数据成员为边长、Rectangle(长方形) ,数据成员为长和宽、Trapezoid(梯形) ,数据成员为上底、下底和高、Triangle(三角形) ,数据成员为底和高。
测试过程,定义一个指向基类的指针数组,使其每个元素指向一个动态产生的派生类对象,分别调用相应的成员函数显示各个几何图形的属性及面积,最终输出总面积值。
#include<iostream>
#include<cmath>
using namespace std;
const double pi = 3.14159;
class Shape {
public:
Shape() {};
virtual ~Shape() {};
virtual void printName() = 0;
virtual void printArea() = 0;
virtual double area() = 0;
};
class Circle : public Shape {
private:
double R;
double s;
public:
Circle(double r) : R(r) {}
virtual void printName() {
cout << "圆的半径是" << R << endl;
}
virtual void printArea() {
cout << "圆的面积是" << area() << endl;
}
virtual double area() {
s = pi * R * R;
return s;
}
virtual ~Circle() {}
};
class Rectangle : public Shape {
private:
double H, W;
double s;
public:
Rectangle(double h, double w) : H(h), W(w) {}
virtual void printName() {
cout << "长方形的长是" << H << ", 宽是" << W << endl;
}
virtual void printArea() {
cout << "长方形的面积是" << area() << endl;
}
virtual double area() {
s = H * W;
return s;
}
virtual ~Rectangle() {}
};
class Trapezoid : public Shape {
private:
double upside, downside, h;
double s;
public:
Trapezoid(double up, double down, double hh) : upside(up), downside(down), h(hh) {}
virtual void printName() {
cout << "梯形的上底是" << upside << ", 下底是" << downside << ",高是" << h << endl;
}
virtual void printArea() {
cout << "梯形的面积是" << area() << endl;
}
virtual double area() {
s = (upside + downside) * h / 2;
return s;
}
virtual ~Trapezoid() {}
};
int main() {
Shape* shapes[3];
shapes[0] = new Circle(5.0);
shapes[1] = new Rectangle(3.0, 4.0);
shapes[2] = new Trapezoid(2.0, 4.0, 3.0);
for (int i = 0; i < 3; i++) {
shapes[i]->printName();
shapes[i]->printArea();
delete shapes[i];
}
return 0;
}