o掌握C++语言多态性的基本概念
o掌握运算符重载函数的声明和定义方法
C++语言多态性的基本概念
在面向对象方法中,所谓多态性就是不同对象收到相同的消息时,产生不同的行为(即方法)。在C++程序设计中,多态性是指用一个名字定义不同的函数,这些函数执行不同但又类似的操作,这样就可以用同一个函数名调用不同内容的函数。也就是说,可以用同样的接口访问功能不同的函数,从而实现“一个接口,多种方法”。
在C++中,多态性的实现和联编(也叫绑定)这一概念有关。一个源程序经过编译、连接,成为可执行文件的过程是把可执行代码联编(或称装配)在一起的过程。
静态联编是指系统在编译时就决定如何实现某一动作。
动态联编是指系统在运行时动态实现某个动作。
静态联编支持的多态性称为编译时多态性,也称静态多态性。
动态联编所支持的多态性称为运行时多态性,也称动态多态性。
运算符重载函数的声明和定义方法
函数类型 operator 运算符名称(形参表){
对运算符的重载处理
}
以下代码为:类外定义运算符重载函数
#include<iostream>
using namespace std;
class Complex {
public:
double real;
double image;
Complex(double x = 0, double y = 0) {
real = x;
image = y;
}
};
Complex operator*(Complex co1, Complex co2) {
Complex temp;
temp.real = co1.real * co2.real;
temp.image = co1.image * co2.image;
return temp;
}
int main() {
Complex com1(1.1, 2.2), com2(3.3, 4.4), total1, total2;
total1 = com1 * com2;
cout << "real1=" << total1.real << " " << "image1=" << total1.image << endl;
total2 = operator*(com1, com2);
cout << "real2=" << total2.real << " " << "image2=" << total2.image << endl;
return 0;
}
其结果为:
用友元运算符重载函数定义:
(1)在类内定义友元运算符重载函数的格式如下:
friend 函数类型 operator 运算符(形参表){
函数体
}
(2)友元运算符重载函数也可以在类中声明友元函数的原型,在类外定义。
在类中
class X{
friend 函数类型 operator 运算符(形参表);
};
在类外
函数类型 operator 运算符(形参表){
函数体
}
#include"sj.h"
using namespace std;
class Complex {
double real;
double image;
public:
Complex(double x = 0, double y = 0) {
real = x;
image = y;
}
void print();
friend Complex operator*(Complex co1, Complex co2);
};
Complex operator*(Complex co1, Complex co2) {
Complex temp;
temp.real = co1.real * co2.real;
temp.image = co1.image * co2.image;
return temp;
}
void Complex::print() {
cout << "total real=" << real <<" " << "total image" << image << endl;
}
int main() {
Complex com1(1.1, 2.2), com2(3.3, 4.4), total1, total2;
total1 = com1 * com2;
total1.print();
//cout << "real1=" << total1.real << " " << "image1=" << total1.image << endl;
// total2 = operator*(com1, com2);
//cout << "real2=" << total2.real << " " << "image2=" << total2.image << endl;
return 0;
}
其结果如下:
成员运算符重载函数
(1)对双目运算符而言,成员运算符重载的形参表中仅有一个参数,它作为运算符的右操作数。
(2)另一个操作数(左操作数)是隐含的,是该类的当前对象,他是通过this指针隐含传递给函数。
#include<iostream>
using namespace std;
class Complex {
double real;
double image;
public:
Complex(double x = 0, double y = 0) {
real = x;
image = y;
}
void print();
Complex operator*(Complex co2);
};
Complex Complex::operator*(Complex co2) {
Complex temp;
temp.real = real * co2.real;
temp.image = image * co2.image;
return temp;
}
void Complex::print() {
cout << "total real=" << real << " " << "total image" << image << endl;
}
int main() {
Complex com1(1.1, 2.2), com2(3.3, 4.4), total1, total2;
total1 = com1 * com2;
total1.print();
//cout << "real1=" << total1.real << " " << "image1=" << total1.image << endl;
// total2 = operator*(com1, com2);
//cout << "real2=" << total2.real << " " << "image2=" << total2.image << endl;
return 0;
}
其结果为:
实验要点:
对运算符重载制定了一下一些规则。
(1)C++中绝大部分的运算符允许重载,不能重载的运算符只有以下几个:
. 成员访问运算符 .* 成员指针访问运算符 :: 作用域运算符
?: 条件运算符 Sizeof 长度运算符
(2)C++语言中只能对已有的C++运算符进行重载,不允许用户自己定义新的运算符。
(3)运算符重载函数的参数至少应有一个是类对象(或类对象的引用)。
(4)重载不能改变运算符原有的结合特性。
(5)重载不能改变运算符的操作对象(即操作数)的个数。
(6)重载不能改变运算符原有的优先级。