Advertisement
andruhovski

sp0303

Feb 13th, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. // Lec0303.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. class Figure
  10. {
  11. public:
  12.     Figure(void){};
  13.     ~Figure(void){};
  14.     virtual double size(void){return 0;};
  15.     virtual void show(void){return;};
  16. };
  17.  
  18. class Rectangle : public Figure
  19. {
  20. protected:
  21.     double a,b;
  22. public:
  23.     Rectangle(void) : a(0),b(0) {};
  24.     ~Rectangle(void) {};
  25.     virtual double size(void) {return  a*b ;};
  26.     virtual void show(void){cout << "Rectangle" << endl; return;};
  27. };
  28.  
  29. class Circle: public Figure
  30. {
  31. protected:
  32.     double r;
  33. public:
  34.     Circle(void) : r(0) {};
  35.     ~Circle(void) {};
  36.     virtual double size(void) {return  3.14*r*r ;};
  37.     virtual void show(void){cout << "Circle" << endl; return;};
  38. };
  39.  
  40. class RoundRect: public Rectangle, public Circle
  41. {
  42. private:
  43.     int n;
  44. public:
  45.     RoundRect(void):n(4){};
  46.     virtual double size(void) {return  a*b+n*3.14*r*r ;};
  47. };
  48.  
  49. int a=5;
  50. const int *p=&a;
  51.  
  52. void demo1(int *x);
  53. void demo2a(Figure *f);
  54. void demo2b(Figure *f);
  55. void demo2c(Circle *c);
  56. void demo3(void);
  57. void demo4(void);
  58. void demo5(Figure *f);
  59. void demo6(Figure *f);
  60. int _tmain(int argc, _TCHAR* argv[])
  61. {
  62.     //Приклад 1
  63.     //demo1(p); //demo1(const_cast<int *>(p));
  64.     //==========================================
  65.    
  66.     //Приклад 2 ================================
  67.     //Circle *c = new Circle();
  68.     //demo2a(c);
  69.  
  70.     //Figure *b=dynamic_cast<Figure *> (c); //Підвищуюче перетворення
  71.     //demo2a(b);
  72.  
  73.     //Circle *c1=new Circle();
  74.     //demo2b(c1);
  75.     //Rectangle *r1=new Rectangle();
  76.     //demo2b(r1);
  77.     //
  78.     //RoundRect *r2=new RoundRect();
  79.     //demo2c(r2);
  80.    
  81.     //Приклад 3 ================================
  82.     //demo3();
  83.     //==========================================
  84.  
  85.     //Приклад 4 ================================
  86.     //demo4();
  87.     //==========================================
  88.    
  89.     //Приклад 5 ================================
  90.     //Circle *c2=new Circle();
  91.     //demo5(c2);
  92.     //==========================================
  93.    
  94.     //Приклад 6
  95.     Circle *c3=new Circle();
  96.     demo6(c3);
  97.     return 0;
  98. }
  99.  
  100. void demo1(int *x)
  101. {
  102.     cout << "demo 1" << endl;
  103.     cout << *x << endl;
  104. }
  105.  
  106. void demo2a(Figure *f)
  107. {
  108.     f->show ();
  109. }
  110.  
  111. void demo2b(Figure *f)
  112. {
  113.     Circle *c = dynamic_cast<Circle *>(f);
  114.     if (c)
  115.         c->show ();
  116.     else
  117.         cout << "Error" << endl;
  118.  
  119.     return;
  120. }
  121.  
  122. void demo2c(Circle *c)
  123. {
  124.     Rectangle *r = dynamic_cast<Rectangle*>(c);
  125.     if (r)
  126.         r->show ();
  127.     else
  128.         cout << "Error" << endl;
  129.     return;
  130. }
  131.  
  132.  
  133. void demo3()
  134. {
  135.     float b=2.9f;
  136.     int c;
  137.     c= static_cast<int> (b);
  138.     cout << c << endl;
  139. }
  140.  
  141. void demo4()
  142. {
  143.     char *p=reinterpret_cast<char *>(malloc(100));
  144.     long l = reinterpret_cast<long> (p);
  145.     cout << l << endl;
  146. }
  147.  
  148. void demo5(Figure *f)
  149. {
  150.  
  151.     if (typeid(*f)==typeid(Circle))
  152.     {
  153.         Circle *c = dynamic_cast<Circle *>(f);
  154.         c->show ();
  155.     }
  156.     else
  157.         cout << "Error" << endl;
  158.     return;
  159. }
  160.  
  161. void demo6(Figure *f)
  162. {
  163.     cout << typeid(*f).name() << endl;
  164.     return;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement