第8章--多态性1

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第8章 多态性
例8.1 (续一)
void CComplex::Print() {
cout << "(" << real << "," << imag << ")" << endl; } CComplex CComplex::operator +(CComplex c) {
CComplex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } CComplex CComplex::operator -(CComplex c) { CComplex temp; temp.real = real - c.real; temp.imag = imag - c.imag; return temp; }
第8章 多态性
8.1 运算符重载 8.2 运算符重载为类的成员函数 8.3 运算符重载为类的友元函数 8.4 虚函数
第8章 多态性
8.1 运算符重载
8.1.1 问题的提出
例6.3的复数类
#include "iostream.h" class CComplex { private:
double real; double imag; public: CComplex(double r, double i); void Print(); CComplex Add(CComplex c); CComplex Sub(CComplex c);
程序运行结果为:
a: i=6 b: i=6 c: i=5 d: i=6
第8章 多态性
8.2 运算符重载为类的成员函数
8.2.3 赋值运算符重载
如果类中只包含简单数据类型的数据成员,则使用C++提供的 赋值运算符“=”就可以实现将一个对象赋给另一个对象。如前面复 数类的对象,就可以将一个对象直接赋给另一个对象。但如果类 的数据成员比较复杂(如含有指针),这样直接赋值就会产生问 题,我们必须重载赋值运算符“=”才能正确使用“=”。
d.Print();
}
程序运行结果为:
c = (4, 6) d = (-2, -2)
第8章 多态性
8.1 运算符重载
8.1.2 运算符重载的格式与规则
1. 运算符重载的格式 ➢ 运算符重载为类的成员函数 ➢ 运算符重载为类的友元函数 运算符重载的为类的成员函数,在类中声明的格式为: 函数类型 operator 运算符(参数表); 定义该函数的格式: 函数类型 类名::operator 运算符(参数表) { 函数体; } 也可以将重载运算符函数的定义直接写在类中。
第8章 多态性
例8.1 (续二)
void main(void)
{
CComplex a(1, 2), b(3.0, 4.0), c,d;
c = a+b;
d = a-b; cout << "c = "; c.Print(); cout << "d = ";
该语句相当于对函数operator +(CComplex c) 的调用:“c=a.operator +(b)”,实现两个复数 的加法运算。
第8章 多态性
8.1 运算符重载
8.1.2 运算符重载的格式与规则(续)
2. 运算符重载的规则 (1)除“.”、“*”、“::”、“?:”和“sizeof”等几个运算符不能 重载外,C++中几乎所有的运算符都可以重载。 (2)运算符被重载后,其优先级和结合性不会改变。 (3)不能改变运算符操作对象的个数。
c = a+b;
oprd2 相当于 oprd1.operator
d = a-b; e = a*b; f = a+1;
B(oprd2),注意重载双目运算符只 需要一个参数。
cout << "c = ";
c.Print();
cout << "d = ";
d.Print(); cout << "e = "; e.Print();
第8章 多态性
例8.2 复数乘法运算源程序
#include "iostream.h" class CComplex { private:
double real; double imag; public: CComplex(double r=0, double i=0); void Print(); CComplex operator +(CComplex c); CComplex operator -(CComplex c); CComplex operator *(CComplex c); }; CComplex::CComplex (double r, double i) { real = r; imag = i; }
该语句只是将p所指向的对象数据成员 str赋给对象a1的数据成员str,即两个 对象的str指向了同一个单元
a1.print();
调用析构函数,同时将str所指向的单元释放了,
}
再执行a1.print()时,就会出现错误。
第8章 多态性
例8.5 带有重载赋值运算(续一)
void CComplex::Print() {
cout << "(" << real << "," << imag << ")" << endl; } CComplex CComplex::operator +(CComplex c) {
CComplex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } CComplex CComplex::operator -(CComplex c) { CComplex temp; temp.real = real - c.real; temp.imag = imag - c.imag; return temp; }
第8章 多态性
例8.3 定义一个CInt类,类中只有一个数据成员i,两个运算 符“++”的重载函数,一个没有参数,实现的是前置运算符重 载,另一个有一个整型参数,实现后置运算符重载。
#include "iostream.h" class CInt { private:
int i; public:
CInt(int a=0); void Print(); CInt operator ++(); CInt operator ++(int); }; CInt::CInt (int a) { i = a; }
void CInt::Print() {
cout << "i=" << i << endl; } CInt CInt::operator ++() {
CInt temp; temp.i = ++i; return temp; } CInt CInt::operator ++(int) { CInt temp; temp.i = i++; return temp; }
第8章 多态性
例8.2 (续二)
CComplex CComplex::operator *(CComplex c)
{
CComplex temp;
总结:设有双目运算符 B,如果要
temp.real = real * c.real - imag * c.imag; temp.imag = real * c.imag + imag * c.real; return temp; } void main(void) {
第8章 多态性
例8.3 (续)
void main(void) {
CInt a(5), b(5), c, d; c = a++; d = ++b; cout << "a: "; a.Print(); cout << "b: "; b.Print(); cout << "c: "; c.Print(); cout << "d: "; d.Print(); }
重载 B 为类的成员函数,使之能够 实现表达式 oprd1 B oprd2,其中 oprd1 为A 类对象,则 B 应被重载 为 A 类的成员函数,形参类型应该 是 oprd2 所属的类型。
CComplex a(1, 2), b(3.0, 4.0), c, d, e, f;
经重载后,表达式oprd1 B
程序运行结果为:
c = (4, 6)
cout << "f = "; d = (-2, -2)
f.Print(); }
e = (-5,10) f = (2, 2)
第8章 多态性
8.2 运算符重载为类的成员函数
8.2.2 单目运算符重载
单目运算符,如果重载为类的成员函数,不需要参数。 为区分前置和后置运算符,C++规定: 对于前置单目运算符,重载函数没有参数 对于后置单目运算符,重载函数有一个整型参数,这个整型 参数没有其他用途,只是用于区分前置运算与后置运算。
返回
第8章 多态性
8.2 运算符重载为类的成员函数
8.2.1 双目运算符重载
双目运算符,如果重载为类的成员函数,其参数为一个,即 比运算对象少一个。
例8.2 复数的乘法运算,在上例的基础上添加乘法运算符重 载函数。复数类乘法运算的定义如下:
(a+bi)*(x+yi)= a*x-b*y + (a*y + b*x)i
};
CComplex CComplex::Add(CComplex c) {
CComplex temp; temp.real = real + c.real; temp.imag = imag + c.imag; return temp; } CComplex CComplex::Sub(CComplex c) { CComplex temp; temp.real = real - c.real; temp.imag = imag - c.imag; return temp; }
第8章 多态性
例8.4 类A只有一个数据成员str,是一个字符指针,在构造函 数中为str申请存储空间并赋值,在析构函数中释放内存。
#include "iostream.h" #include "string.h" class A { private:
char *str; public:
A(char *s="no data"); ~A(); void print(); }; A::A (char *s) { int len = strlen(s); str = new char[len+1]; strcpy(str,s); }
#include "iostream.h" class CComplex { private:
double real; double imag; public: CComplex(double r=0, double i=0); void Print(); CComplex operator +(CComplex c); CComplex operator -(CComplex c); }; CComplex::CComplex (double r, double i) { real = r; imag = i; }
复数加减法只能调用成员函数实现, 不能使用符号“+”和“-”,可以通 过重载“+”、“-”运算符,实现如 c=a+b这样的调用方式
}
运算符重载:运算符重载的实质就是对已有的运算符赋予多重含义,使同一个运算符 作用于不同类型的数据时,产生不同的行为。运算符重载的实质就是函数重载。
第8章 多态性
例8.1 用运算符实现复数的加减运算
第8章 多态性
8.1 运算符重载
8.1.1 问题的提出(续一)
void main(void)
{
CComplex a(1, 2), b(3.0, 4.0), c,d;
c = a.Add(b);
d = a.Sub(b); cout << "c = "; c.Print(); cout << "d = "; d.Print();
第8章 多态性
例8.4 (续)
A::~A ()
{
if(str)
delete []str;
}
void A::print()
AAAA
{ cout << str << endl;
p
str
str a1
}
void main(void)
{
A *p = new A("AAAA");
A a1; a1=*p; a1.print(); delete p;
相关文档
最新文档