Presentation is loading. Please wait.

Presentation is loading. Please wait.

第七章 操作符重载 胡昊 南京大学计算机系软件所.

Similar presentations


Presentation on theme: "第七章 操作符重载 胡昊 南京大学计算机系软件所."— Presentation transcript:

1 第七章 操作符重载 胡昊 南京大学计算机系软件所

2 重要内容

3 操作符重载-Complex(1) class Complex { double real, imag; public:
Complex() {real=0; imag=0;} Complex(double r, double i) {real=r; imag=i;} Complex add(const Complex &x) const Complex temp; temp.real=real+x.real; temp.imag=imag+x.imag; return temp; } };

4 操作符重载-Complex(2) class Complex { double real, imag; public:
Complex() {real=0; imag=0;} Complex(double r, double i) {real=r; imag=i;} Complex operator+(const Complex &x) const Complex temp; temp.real=real+x.real; temp.imag=imag+x.imag; return temp; } };

5 操作符重载-Complex(3) class Complex { double real, imag; public:
Complex() {real=0; imag=0;} Complex(double r, double i) {real=r; imag=i;} friend Complex operator+(const Complex &c1, const Complex &c2); }; Complex operator+(const Complex &c1, const Complex &c2) Complex temp; temp.real=c1.real+c2.real; temp.imag=c1.imag+c2.imag; return temp; }

6 单目操作符重载++前置后置 class Counter { int value; public: Counter() {value=0;}
Counter& operator ++() //前置++ value++; return *this; } const Counter operator++(int) //后置++ Counter temp=*this; ++(*this); //调用前置的++重载函数 return temp; };

7 必须用全局函数来实现 class Complex { double real, imag; public: Complex() {real=0; imag=0;} Complex(double r, double i) {real=r; imag=i;} friend Complex operator+(double d, const Complex &c); }; Complex operator+(double d, const Complex &c) return Complex(d+c.real, c.imag); } 不能用成员函数来实现,因为第一个参数的类型是double,而成员函数的第一个参数规定为Complex *const this

8 赋值操作符= String& String::operator=(const String& st) {
if(&st == this) return *this; //防止自身赋值 delete []str; str=new char[strlen(st.str)+1]; strcpy(str, st.str); return *this; } class String { char *str; public: String() {str=NULL;} String(const char *p) str=new char[strlen(p)+1]; strcpy(str,p); } ~String() delete [] str; str=NULL; String& operator=(const String& st); };

9 下标操作符[] ...... class String { char *str; public:
char& operator [](int i) //操作符[]的重载函数 if(i>=strlen(str) || i<0) cerr<<“下标越界错误\n”; exit(-1); } return str[i]; };

10 函数绑定 class A { int x, y; public: virtual void f(); };
class B: public A int z; void f(); void g(); func1(A &a) …… a.f(); //调用A::f还是B::f } func2(A *pa) { …… pa->f(); //调用A::f还是B::f } A a; func1(a); func2(&a); B b; func1(b); func2(&b);

11 虚函数动态绑定 class A { public: A() {f();} ~A() {……} virtual void f();
void g(); void h() {f(); g();} }; class B: public A ~B() {……}; void f(); A a; a.f(); a.g(); a.h(); B b(); b.f(); b.g(); b.h(); A *p; p=&a; p->f(); p->g(); p->h(); p=new B; delete p;

12 例1. 从键盘输入一组图形数据,然后画出相应的图形。其中的图形可以是:线段、矩形和圆。
Figure Rectangle Circle Line

13 virtual void draw() const=0; virtual void input_data()=0; };
class Figure{ public: virtual void draw() const=0; virtual void input_data()=0; }; class Rectangle: public Figure { double left,top,right,bottom; public: void draw() const { /*画矩形*/ } void input_data() { cout << "请输入矩形的坐标:"; cin >> left >> top >> right >> bottom; } double area() const{ … } };

14 class Circle: public Figure {
double x, y, r; public: void draw() const{ /*画圆*/ } void input_data() { cout << "请输入圆的圆心坐标和半径 :"; cin >> x >> y >> r; } double area() const{ … }; };

15 class FigureGroup { //图形对象管理类
Figure *figures_buf[MAX_NUM]; int num_of_figures; public: FigureGroup() { num_of_figures = 0; } void display_figures() { for (int i=0; i<num_of_figures; i++) figures_buf[i]->draw(); } void input_figures_data() ; };

16 void FigureGroup::input_figures_data() {
for (num_of_figures=0; num_of_figures<MAX_NUM; num_of_figures++ ) { int shape; do{ cout << “请输入图形的种类(0:线段,1: 矩形,2:圆,-1:结束):"; cin >> shape; } while (shape < -1 || shape > 2); if (shape == -1) break; switch (shape) { case 0: // 线 figures_buf[num_of_figures] = new Line; break; …… } figures_buf[num_of_figures]->input_data(); } }

17 int main() { FigureGroup figures; //输入图形数据 figures.input_figures_data(); //输出所有图形 figures.display_figures(); return 0; }


Download ppt "第七章 操作符重载 胡昊 南京大学计算机系软件所."

Similar presentations


Ads by Google