Chinaunix首页 | 论坛 | 博客
  • 博客访问: 360430
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-19 11:40:12

  1. #include <iostream>
  2. using namespace std;

  3. class CPolygon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values(int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11. };

  12. class CRectangle: public CPolygon {
  13.         public:
  14.                 int area() {
  15.                         return (width * height);
  16.                 }
  17. };

  18. class CTriangle: public CPolygon {
  19.         public:
  20.                 int area() {
  21.                         return (width * height) / 2;
  22.                 }
  23. };

  24. int
  25. main(void)
  26. {
  27.         CRectangle rect;
  28.         CTriangle trgl;
  29.         CPolygon *ppoly1 = &rect;
  30.         CPolygon *ppoly2 = &trgl;
  31.         ppoly1->set_values (4,5);
  32.         ppoly2->set_values (4,5);
  33.         cout << rect.area() << endl;
  34.         cout << trgl.area() << endl;

  35.         return (0);
  36. }
One of the key features of derived classes is that a pointer to a derived class is type-compatible with a pointer to its base class. Polymorphism is the art of taking advantage of this simple but powerful and versatile feature, that brings Object Oriented Methodologies to its full potential.

The only limitation in using *ppoly1 and *ppoly2 instead of rect and trgl is that both *ppoly1 and *ppoly2 are of type CPolygon* and therefore we can only use these pointers to refer to the members that CRectangle and CTriangle inherit from CPolygon. For that reason when we call the area() members at the end of the program we have had to use directly the objects rect and trgl instead of the pointers *ppoly1 and *ppoly2.

Virtual member:
A member of a class that can be redefined in its derived classes is known as a virtual member. In order to declare a member of a class as virtual, we must precede its declaration with the keyword virtual:
A class that declares or inherits a virtual function is called a polymorphic class.
  1. #include <iostream>
  2. using namespace std;

  3. class CPolygon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values (int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11.                 virtual int area () {
  12.                         return (0);
  13.                 }
  14. };

  15. class CRectangle: public CPolygon {
  16.         public:
  17.                 int area() {
  18.                         return (width * height);
  19.                 }
  20. };

  21. class CTriangle: public CPolygon {
  22.         public:
  23.                 int area() {
  24.                         return (width * height / 2);
  25.                 }
  26. };

  27. int
  28. main(void)
  29. {
  30.         CRectangle rect;
  31.         CTriangle trgl;
  32.         CPolygon poly;
  33.         CPolygon *ppoly1 = &rect;
  34.         CPolygon *ppoly2 = &trgl;
  35.         CPolygon *ppoly3 = &poly;
  36.         ppoly1->set_values (4,5);
  37.         ppoly2->set_values (4,5);
  38.         ppoly3->set_values (4,5);

  39.         cout << ppoly1->area() << endl;
  40.         cout << ppoly2->area() << endl;
  41.         cout << ppoly3->area() << endl;

  42.         return (0);
  43. }

Abstract base class:
in an abstract base classes we could leave that area() member function without implementation at all. This is done by appending =0 (equal to zero) to the function declaration.
This type of function is called a pure virtual function, and all classes that contain at least one pure virtual function are abstract base classes.
The main difference between an abstract base class and a regular polymorphic class is that because in abstract base classes at least one of its members lacks implementation we cannot create instances (objects) of it.

  1. #include <iostream>
  2. using namespace std;

  3. class CPolygon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values (int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11.                 virtual int area (void) = 0;
  12. };

  13. class CRectangle: public CPolygon {
  14.         public:
  15.                 int area() {
  16.                         return (width * height);
  17.                 }
  18. };

  19. class CTriangle: public CPolygon {
  20.         public:
  21.                 int area() {
  22.                         return (width * height / 2);
  23.                 }
  24. };

  25. int
  26. main(void)
  27. {
  28.         CRectangle rect;
  29.         CTriangle trgl;
  30.         CPolygon *ppoly1 = &rect;
  31.         CPolygon *ppoly2 = &trgl;
  32.         ppoly1->set_values (4,5);
  33.         ppoly2->set_values (4,5);

  34.         cout << ppoly1->area() << endl;
  35.         cout << ppoly2->area() << endl;

  36.         return (0);
  37. }

  1. #include <iostream>
  2. using namespace std;

  3. class CPolygon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values (int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11.                 virtual int area(void) = 0;
  12.                 void printarea(void) {
  13.                         cout << this->area() <<endl;
  14.                 }
  15. };

  16. class CRectangle: public CPolygon {
  17.         public:
  18.                 int area(void) {
  19.                         return (width * height);
  20.                 }
  21. };

  22. class CTriangle: public CPolygon {
  23.         public:
  24.                 int area(void) {
  25.                         return (width * height / 2);
  26.                 }
  27. };

  28. int
  29. main(void)
  30. {
  31.         CRectangle rect;
  32.         CTriangle trgl;
  33.         CPolygon *ppoly1 = &rect;
  34.         CPolygon *ppoly2 = &trgl;
  35.         ppoly1->set_values(4,5);
  36.         ppoly2->set_values(4,5);
  37.         ppoly1->printarea();
  38.         ppoly2->printarea();

  39.         return (0);
  40. }

  1. #include <iostream>
  2. using namespace std;

  3. class CPolygon {
  4.         protected:
  5.                 int width, height;
  6.         public:
  7.                 void set_values (int a, int b) {
  8.                         width = a;
  9.                         height = b;
  10.                 }
  11.                 virtual int area(void) = 0;
  12.                 void printarea(void) {
  13.                         cout << this->area() <<endl;
  14.                 }
  15. };

  16. class CRectangle: public CPolygon {
  17.         public:
  18.                 int area(void) {
  19.                         return (width * height);
  20.                 }
  21. };

  22. class CTriangle: public CPolygon {
  23.         public:
  24.                 int area(void) {
  25.                         return (width * height / 2);
  26.                 }
  27. };

  28. int
  29. main(void)
  30. {
  31.         CPolygon *ppoly1 = new CRectangle;
  32.         CPolygon *ppoly2 = new CTriangle;
  33.         ppoly1->set_values(4,5);
  34.         ppoly2->set_values(4,5);
  35.         ppoly1->printarea();
  36.         ppoly2->printarea();
  37.         delete ppoly1;
  38.         delete ppoly2;

  39.         return (0);
  40. }

阅读(815) | 评论(0) | 转发(0) |
0

上一篇:友元 继承

下一篇:模板

给主人留下些什么吧!~~