《第12章(12.4纯虚函数)》由会员分享,可在线阅读,更多相关《第12章(12.4纯虚函数)(38页珍藏版)》请在金锄头文库上搜索。
1、12.3 静态关联,(补充) 类指针的关系,12.3 虚函数与动态联编,12.4 纯虚函数与抽象类,(补充) 虚函数和多态性的应用,小结,第12章 虚函数与多态性,12.4 纯虚函数和抽象类,纯虚函数是一个在基类中声明的虚函数,在基类中没有定义函数体, 要求任何派生类都定义自己的版本 声明纯虚函数的一般形式: virtual 类型 函数名(参数表)= 0 ; 注意:1.纯虚函数没有函数体 2.最后面的”=0”只是起形式上的作用,告诉编译器这是纯虚函数 3.这是一个声明语句,最后应有分号 具有纯虚函数的基类称为抽象类,12.4 纯虚函数和抽象类,class shape ; / 抽象类 doubl
2、e x, y ; public : void where ( ) coutxty ; void set( double a, double b) x = a; y =b; virtual void rotate ( int ) = 0 ; / 纯虚函数 virtual void draw ( ) = 0 ;/ 纯虚函数 ; .,shape x ;/ error,抽象类不能建立对象shape *p ;/ ok,可以声明抽象类的指针shape f ( ) ;/ error, 抽象类不能作为返回类型void g ( shape ) ;/ error, 抽象类不能作为参数类型shape& h ( sh
3、ape &) ;/ ok,可以声明抽象类的引用,例如:,12.4 纯虚函数和抽象类,class shape ; / 抽象类 double x, y ; public : void where ( ) coutxty ; void set( double a, double b) x = a; y =b; virtual void rotate ( int ) = 0 ; / 纯虚函数 virtual void draw ( ) = 0 ;/ 纯虚函数 ; .,class ab_circle : public shape int radius ; public : void rotate ( i
4、nt ) ;/在派生类中实现函数rotate ;,例如:,ab_circle 类仍为抽象类因为ab_circle : draw ( )还是纯虚函数,12.4 纯虚函数和抽象类,要使 ab_circle 成为非抽象类, 必须作以下定义: class ab_circle : public shape int radius ; public : void rotate ( int ) ; void draw ( ) ; ;,12.4 纯虚函数和抽象类,纯虚函数的作用:在基类中为其派生类保留一个函数的名字,以便派生类根据需要对它进行定义。 具有纯虚函数的基类称为抽象类。 抽象类的作用:不用来定义对象,
5、而只作为基类用来继承。,12.4 纯虚函数和抽象类,/figure.hclass figure protected : double x,y; public: void set_dim(double i, double j=0) x = i ; y = j ; virtual void show_area() = 0 ;class triangle : public figure public : void show_area() coutTriangle with high x and base y has an area of x*0.5*yn; ;class square : publi
6、c figure public: void show_area() coutSquare with dimension x*y has an area of x*yn; ;class circle : public figure public: void show_area() coutCircle with radius x; cout has an area of 3 4*x*xn; ;,virtual void show_area() = 0 ;/纯虚函数,例 简单图形类,void show_area() coutTriangle with high x and base y has a
7、n area of x*0.5*yn; ,void show_area() coutSquare with dimension x*y has an area of x*yn; ,void show_area() coutCircle with radius x; cout has an area of 3 4*x*xn; ,/figure.hclass figure protected : double x,y; public: void set_dim(double i, double j=0) x = i ; y = j ; virtual void show_area() = 0 ;c
8、lass triangle : public figure public : void show_area() coutTriangle with high x and base y has an area of x*0.5*yn; ;class square : public figure public: void show_area() coutSquare with dimension x*y has an area of x*yn; ;class circle : public figure public: void show_area() coutCircle with radius
9、 x; cout has an area of 3 4*x*xn; ;,virtual void show_area() = 0 ;/纯虚函数,例 简单图形类,void show_area() coutTriangle with high x and base y has an area of x*0.5*yn; ,void show_area() coutSquare with dimension x*y has an area of x*yn; ,void show_area() coutCircle with radius x; cout has an area of 3 4*x*xn;
10、 ,#include#includefigure.hvoid main() triangle t ;/派生类对象 square s ; circle c; t.set_dim(10.0,5.0) ; t.show_area(); s.set_dim(10.0,5.0) ; s.show_area() ; c.set_dim(9.0) ; c.show_area() ; ,/figure.hclass figure protected : double x,y; public: void set_dim(double i, double j=0) x = i ; y = j ; virtual
11、void show_area() = 0 ;class triangle : public figure public : void show_area() coutTriangle with high x and base y has an area of x*0.5*yn; ;class square : public figure public: void show_area() coutSquare with dimension x*y has an area of x*yn; ;class circle : public figure public: void show_area()
12、 coutCircle with radius x; cout has an area of 3 4*x*xn; ;,virtual void show_area() = 0 ;/纯虚函数,例 简单图形类,void show_area() coutTriangle with high x and base y has an area of x*0.5*yn; ,void show_area() coutSquare with dimension x*y has an area of x*yn; ,void show_area() coutCircle with radius x; cout has an area of 3 4*x*xn; ,