Advertisement
prprice16

Untitled

Nov 18th, 2021
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. #pragma once
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. //DSet will store an unknown number of double values
  6. //all values must be unique
  7. class DSet
  8. {
  9. private:
  10. double* list; //a pointer for the array
  11. int size; //number of values stored
  12. public:
  13. DSet(); //default constructor
  14. int getSize(); //return the number of elements
  15.  
  16. void add(double); //adds a value to list
  17. ~DSet();
  18. friend ostream& operator<<(ostream&, DSet);
  19. void print();
  20. bool contains(double);
  21. };
  22.  
  23. //FUNCTION DEFINITIONS
  24.  
  25. //Code default constructor
  26. DSet::DSet()
  27. {
  28. //what code goes here?
  29. //list is initially empty
  30. size = 0;
  31. list = nullptr;
  32. }
  33.  
  34. int DSet::getSize()
  35. {
  36. //return the number of elements in the set
  37. return size;
  38. }
  39.  
  40.  
  41. void DSet::add(double val)
  42. {
  43. //add val as the next element of list
  44. //need to allocate memory 1 bigger than current size
  45. double* newlist = new double[size + 1];
  46.  
  47. //copy values from list to newlist
  48. for (int i = 0; i < size; i++)
  49. {
  50. //copy from list into newlist
  51. newlist[i] = list[i];
  52. }
  53.  
  54. //add val as last value
  55. newlist[size] = val;
  56.  
  57. //update size
  58. size++;
  59.  
  60.  
  61. //delete old list
  62. delete[] list;
  63.  
  64. //make newlist the list
  65. list = newlist;
  66. cout << "in add" << endl;
  67. for (int i = 0; i < size; i++)
  68. {
  69. cout << list[i] << " ";
  70. }
  71.  
  72. }
  73.  
  74. //destructor
  75. DSet::~DSet()
  76. {
  77. cout << "size is " << size << endl;
  78. //release memory associated with list
  79. delete [] list;
  80. //cout << "destructor called" << endl;
  81.  
  82. }
  83.  
  84. ostream& operator<<(ostream& out, DSet obj)
  85. {
  86. cout << "printing size is " << obj.size << endl;
  87. //print data in obj
  88. //print each value separated by a space
  89. //data is in array called list
  90. for (int i = 0; i < obj.size; i++)
  91. {
  92. out << obj.list[i] << " ";
  93. }
  94. return out;
  95.  
  96. }
  97.  
  98. void DSet::print()
  99. {
  100. for (int i = 0; i < size; i++)
  101. {
  102. cout << list[i] << " ";
  103. }
  104. }
  105.  
  106.  
  107. bool DSet::contains(double val)
  108. {
  109. //return true if val is in the list
  110. //otherwise return false
  111.  
  112.  
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement