Advertisement
Jgug

l5_v4

Dec 22nd, 2012
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.07 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <conio.h>
  4.  
  5. using namespace std;
  6.  
  7. class X
  8. {
  9. protected:
  10.     int *x1, *x2;
  11. public:
  12.     X(int, int);
  13.     ~X();
  14.     virtual void set(int, int);
  15.     virtual void print();
  16. };
  17.  
  18. X::X(int i, int j)
  19. {
  20.     x1 = new int(i);
  21.     x2 = new int(j);
  22. }
  23.  
  24. X::~X()
  25. {
  26.     delete x1;
  27.     delete x2;
  28. }
  29.  
  30. void X::set(int i, int j)
  31. {
  32.     *x1 = i;
  33.     *x2 = j;
  34. };
  35.  
  36. void X::print()
  37. {
  38.     cout<<"X1= "<<*x1<<endl<<"X2= "<<*x2<<endl;
  39. };
  40.  
  41. class Y : public X
  42. {
  43. private:
  44.     int *y;
  45. public:
  46.     Y(int, int, int);
  47.     ~Y();
  48.     void set(int, int, int);
  49.     void print();
  50. };
  51.  
  52. Y::Y(int i, int j, int k):X(i, j)
  53. {
  54.     y = new int(k);
  55. }
  56.  
  57. Y::~Y()
  58. {
  59.     delete y;
  60. }
  61.  
  62. void Y::set(int i, int j, int k)
  63. {
  64.     *x1 = i;
  65.     *x2 = j;
  66.     *y = k;
  67. }
  68.  
  69. void Y::print()
  70. {
  71.     cout<<"X1= "<<*x1<<endl<<"X2= "<<*x2<<endl<<"Y= "<<*y<<endl<<"Result= "<<*x1+*x2-*y<<endl;
  72. }
  73.  
  74. void main()
  75. {
  76.     Y obj1(1, 3, 5);
  77.     Y obj2(2, 4, 6);
  78.     X *p;
  79.     p=&obj1;
  80.     p->print();
  81.     p=&obj2;
  82.     p->print();
  83.     obj2.set(7,8,9);
  84.     p->print();
  85.  
  86.     Y obj(10, 5, 11);
  87.     obj.print();
  88.     obj.set(3,3,3);
  89.     obj.print();
  90.  
  91.     _getch();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement