Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lec0303.cpp: определяет точку входа для консольного приложения.
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class Figure
- {
- public:
- Figure(void){};
- ~Figure(void){};
- virtual double size(void){return 0;};
- virtual void show(void){return;};
- };
- class Rectangle : public Figure
- {
- protected:
- double a,b;
- public:
- Rectangle(void) : a(0),b(0) {};
- ~Rectangle(void) {};
- virtual double size(void) {return a*b ;};
- virtual void show(void){cout << "Rectangle" << endl; return;};
- };
- class Circle: public Figure
- {
- protected:
- double r;
- public:
- Circle(void) : r(0) {};
- ~Circle(void) {};
- virtual double size(void) {return 3.14*r*r ;};
- virtual void show(void){cout << "Circle" << endl; return;};
- };
- class RoundRect: public Rectangle, public Circle
- {
- private:
- int n;
- public:
- RoundRect(void):n(4){};
- virtual double size(void) {return a*b+n*3.14*r*r ;};
- };
- int a=5;
- const int *p=&a;
- void demo1(int *x);
- void demo2a(Figure *f);
- void demo2b(Figure *f);
- void demo2c(Circle *c);
- void demo3(void);
- void demo4(void);
- void demo5(Figure *f);
- void demo6(Figure *f);
- int _tmain(int argc, _TCHAR* argv[])
- {
- //Приклад 1
- //demo1(p); //demo1(const_cast<int *>(p));
- //==========================================
- //Приклад 2 ================================
- //Circle *c = new Circle();
- //demo2a(c);
- //Figure *b=dynamic_cast<Figure *> (c); //Підвищуюче перетворення
- //demo2a(b);
- //Circle *c1=new Circle();
- //demo2b(c1);
- //Rectangle *r1=new Rectangle();
- //demo2b(r1);
- //
- //RoundRect *r2=new RoundRect();
- //demo2c(r2);
- //Приклад 3 ================================
- //demo3();
- //==========================================
- //Приклад 4 ================================
- //demo4();
- //==========================================
- //Приклад 5 ================================
- //Circle *c2=new Circle();
- //demo5(c2);
- //==========================================
- //Приклад 6
- Circle *c3=new Circle();
- demo6(c3);
- return 0;
- }
- void demo1(int *x)
- {
- cout << "demo 1" << endl;
- cout << *x << endl;
- }
- void demo2a(Figure *f)
- {
- f->show ();
- }
- void demo2b(Figure *f)
- {
- Circle *c = dynamic_cast<Circle *>(f);
- if (c)
- c->show ();
- else
- cout << "Error" << endl;
- return;
- }
- void demo2c(Circle *c)
- {
- Rectangle *r = dynamic_cast<Rectangle*>(c);
- if (r)
- r->show ();
- else
- cout << "Error" << endl;
- return;
- }
- void demo3()
- {
- float b=2.9f;
- int c;
- c= static_cast<int> (b);
- cout << c << endl;
- }
- void demo4()
- {
- char *p=reinterpret_cast<char *>(malloc(100));
- long l = reinterpret_cast<long> (p);
- cout << l << endl;
- }
- void demo5(Figure *f)
- {
- if (typeid(*f)==typeid(Circle))
- {
- Circle *c = dynamic_cast<Circle *>(f);
- c->show ();
- }
- else
- cout << "Error" << endl;
- return;
- }
- void demo6(Figure *f)
- {
- cout << typeid(*f).name() << endl;
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement