Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. // 1.cpp : Defines the entry point for the console application.
  2. // Odnaleźć błędy i poprawić
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. #define MAX_CHAR 512
  10.  
  11. class A
  12. {
  13. protected:
  14. char str[MAX_CHAR];
  15. public:
  16. A() { str[0] = '\0'; }
  17. A(char sstr[]) {
  18. str[0] = '\0';
  19. try {
  20. strcpy_s(str, sizeof(str), sstr);
  21. }
  22. catch (bad_alloc) {
  23. }
  24. }
  25. void disp() { cout << str << endl; }
  26. };
  27.  
  28. class B
  29. {
  30. protected:
  31. int *b;
  32. public:
  33. B() { b = NULL; }
  34. B(int dim) {
  35. try {
  36. b = new int[dim];
  37. memset(b, 0, dim*sizeof(int));
  38. }
  39. catch (bad_alloc) {
  40. }
  41. }
  42. ~B() { if (b) delete[] b; }
  43. void disp() {
  44. if (b)
  45. for (size_t it = 0; it<_msize(b) / sizeof(b[0]); ++it)
  46. cout << b[it] << endl;
  47. }
  48. };
  49.  
  50. class C : public A, public B
  51. {
  52. public:
  53. C(int i, char sstr[]) : B(i), A(sstr) {};
  54. C(const C &ob) { *this = ob; };
  55. int &operator [] (const size_t ind) { return b[ind]; }
  56. friend ostream & operator <<(ostream &wyjscie, C const &ob);
  57. };
  58.  
  59. ostream & operator << (ostream & wyjscie, C const &ob)
  60. {
  61. if (ob.str)
  62. {
  63. int inc=0, size = _msize(ob.b)/sizeof(int);
  64. wyjscie << "int[] = ";
  65. while (inc != size)
  66. wyjscie << ob.b[inc++] << " ";
  67. wyjscie << "str = " << ob.str << endl;
  68. }
  69. else
  70. {
  71. exit(5);
  72. }
  73. return wyjscie;
  74. }
  75.  
  76.  
  77. int _tmain(int argc, _TCHAR* argv[])
  78. {
  79. C ob1(10, "abcde"), ob2(20, "efkjyklmn");
  80. for (size_t it = 0; it<10; ++it)
  81. ob2[it] = it*it + 1;
  82. cout << ob1 << endl << ob2;
  83. ob1 = ob2;
  84. cout << endl << endl << ob1;
  85. C ob3 = ob1;
  86. cout << endl << endl << ob3;
  87. return 0;
  88. }
  89.  
  90.  
  91. #undef MAX_CHAR
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement