Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- using namespace std;
- //DSet will store an unknown number of double values
- //all values must be unique
- class DSet
- {
- private:
- double* list; //a pointer for the array
- int size; //number of values stored
- public:
- DSet(); //default constructor
- int getSize(); //return the number of elements
- void add(double); //adds a value to list
- ~DSet();
- friend ostream& operator<<(ostream&, DSet);
- void print();
- bool contains(double);
- };
- //FUNCTION DEFINITIONS
- //Code default constructor
- DSet::DSet()
- {
- //what code goes here?
- //list is initially empty
- size = 0;
- list = nullptr;
- }
- int DSet::getSize()
- {
- //return the number of elements in the set
- return size;
- }
- void DSet::add(double val)
- {
- //add val as the next element of list
- //need to allocate memory 1 bigger than current size
- double* newlist = new double[size + 1];
- //copy values from list to newlist
- for (int i = 0; i < size; i++)
- {
- //copy from list into newlist
- newlist[i] = list[i];
- }
- //add val as last value
- newlist[size] = val;
- //update size
- size++;
- //delete old list
- delete[] list;
- //make newlist the list
- list = newlist;
- cout << "in add" << endl;
- for (int i = 0; i < size; i++)
- {
- cout << list[i] << " ";
- }
- }
- //destructor
- DSet::~DSet()
- {
- cout << "size is " << size << endl;
- //release memory associated with list
- delete [] list;
- //cout << "destructor called" << endl;
- }
- ostream& operator<<(ostream& out, DSet obj)
- {
- cout << "printing size is " << obj.size << endl;
- //print data in obj
- //print each value separated by a space
- //data is in array called list
- for (int i = 0; i < obj.size; i++)
- {
- out << obj.list[i] << " ";
- }
- return out;
- }
- void DSet::print()
- {
- for (int i = 0; i < size; i++)
- {
- cout << list[i] << " ";
- }
- }
- bool DSet::contains(double val)
- {
- //return true if val is in the list
- //otherwise return false
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement