Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. //
  2. // main.cpp
  3. // Bab
  4. //
  5. // Created by Кирилл Киселев on 15/02/2019.
  6. // Copyright © 2019 Кирилл. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12.  
  13.  
  14.  
  15. class vect{
  16. //public:
  17. int N;
  18. // static const int N = 3;
  19. int * body;
  20. //если убрать конструктор, нельзя будет создать 12 одинаковых
  21. public:
  22. vect(){
  23. for(int i = 0; i < N; i++){
  24. body[i] = 0;
  25. }
  26. printf("vect() = "); print(); printf("\n");
  27. }
  28. vect(const int *init, int size){
  29. N = size;
  30. body = new int[N];
  31. // size = size > N ? N : size;
  32. for(int i = 0; i < size; i++){
  33. body[i] = init[i];
  34. }
  35. for(int i = size; i < N; i++){
  36. body[i] = 0;
  37. }
  38. printf("vect(int*) = "); print(); printf("\n");
  39. }
  40. vect(vect const &v){
  41. body = new int[N = v.N];
  42. for(int i = 0; i < N; i++){
  43. body[i] = v.body[i];
  44. }
  45. printf("vect(vect) ="); print(); printf("\n");
  46. }
  47. vect & operator = (vect const & v){
  48. N = v.N;
  49. delete [] body;
  50. body = new int[N];
  51. for(int i = 0; i < N; i++){
  52. body[i] = v.body[i];
  53. }
  54. printf("vectr :: operator = "); print(); printf("\n");
  55. return * this;
  56. }
  57. void print() const {
  58. printf("(");
  59. for (int i = 0; i < N; i++){
  60. printf("%d", body[i]);
  61. }
  62. printf(")");
  63. }
  64.  
  65. ~vect(){
  66. printf("~vect: ");
  67. printf("\n");
  68. delete [] body;
  69. }
  70. //написано для того, чтобы можно было писать v[i]
  71. int operator[](int x) const {
  72. return body[x];
  73. }
  74. int & operator[](int x){
  75. return body[x];
  76. }
  77. int size() const {
  78. return N;
  79. }
  80. };
  81. //vect :: N область видимости
  82.  
  83. //можно добавить амперсент and const
  84. int sum(vect const &v){
  85. v.print();
  86. int res = 0;
  87. for(int i = 0; i < v.size(); i++){
  88. res += v[i];
  89. //res += v.body[i];
  90.  
  91. }
  92. return res;
  93. }
  94.  
  95. ////int & b = a;
  96. //int main(){
  97. // srand(time(NULL));
  98. // int v[2] = {5, 7};
  99. // vect a, b(v, 2);
  100. // printf("a = "); a.print(); printf("\n");
  101. // printf("b = "); b.print(); printf("\n");
  102. // printf("sum of b = %d", sum(b));
  103. //// vect c;
  104. // vect *c;
  105. // if (rand() < rand()){
  106. // c = new vect(v, 2);
  107. // } else{
  108. // c = new vect();
  109. // }
  110. // printf("sum of c = %d", sum(*c));
  111. // delete c;
  112. //
  113. //}
  114. //
  115. //
  116. //int main(){
  117. // vect points[10];
  118. // vect *v = new vect[10];
  119. //// vect *v = new vect[1]; //эквивалентно когда единичный элемент
  120. //
  121. // delete [] v;
  122. //}
  123. int main(){
  124. int t[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  125. vect v(t, 10);
  126. // v[5] = 0;
  127. printf("sum of v = %d\n", sum(v));
  128. }
  129.  
  130.  
  131. //vect & operator = (vect const & v){
  132. //N = v.N
  133. //delete [] body
  134. //body =
  135. //
  136. //return *this
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement