How to use class in another class?

Hello, So my program is supposed to take two numbers and form an interval(class). Then another file creates an IntervalSet(class, vector of intervals). I can't figure out how to get the IntervalSet to take in the parameters to create an interval and then push_back into the vector while in the IntervalSet class.

This is the provided Interval file & header:
.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "interval.h"
#include <algorithm>   // for min, max
#include <iostream>

using namespace std;


// Construct a new interval with the indicated bounds
Interval::Interval (double l, double h)
  : low(l), high(h)
{}



// Test to see if this interval overlaps with another
bool Interval::overlaps (Interval x) const
{
  return !(low >= x.high || high <= x.low); 
}

// Get the portion of this interval that lies below x. For
// example:
//   Interval w (-10.0, 10.0);
//   Interval z = w.below(5.0);
// z should be (-10.0..5.0)
Interval Interval::below (double x) const
{
  return Interval (low, min(high, max(low, x)));
}

// Get the portion of this interval that lies above x. For
// example:
//   Interval w (-10.0, 10.0);
//   Interval z = w.above(5.0);
// z should be (5.0..10.0)
Interval Interval::above (double x) const
{
  return Interval (max(low,min(high, x)), high);
}


// Print an interval
void Interval::print (std::ostream& out) const
{
  out << "(" << low << " .. " << high << ")";
}


.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef INTERVAL_H
#define INTERVAL_H

#include <iostream>


class Interval {
private:
  double low;
  double high;

public:
  // Construct a new interval with the indicated bounds
  Interval (double l, double h);

  // Get the lower bound of this interval
  double getLower() const {return low;}

  // Get the upper bound of this interval
  double getUpper() const {return high;}


  // Test to see if this interval overlaps with another
  bool overlaps (Interval x) const;


  // Get the portion of this interval that lies below x. For
  // example:
  //   Interval w (-10.0, 10.0);
  //   Interval z = w.below(5.0);
  // z should be (-10.0..5.0)
  Interval below (double x) const;


  // Get the portion of this interval that lies above x. For
  // example:
  //   Interval w (-10.0, 10.0);
  //   Interval z = w.above(5.0);
  // z should be (5.0..10.0)
  Interval above (double x) const;

  // Test to see if this interval is empty
  bool empty () const
  {
    return high <= low;
  }

  // Print an interval
  void print (std::ostream& out) const;

};

#endif 


The portion I am having trouble with is below, I'm not sure how IntervalSet uses the parameters to create an Interval. I have tried mimicking the code into the IntervalSet code but even with the same declarations, it will not work.
.cpp
1
2
3
4
/*Construct a new interval set containing a single starting interval (which the constructor will received as a parameter.*/
IntervalSet::IntervalSet(double l, double h)
    : low(l), high(h), seq.push_back(Interval)
{}


.h
1
2
3
4
5
6
7
8
class IntervalSet {
private:
    std::vector<Interval> seq;  // You don't _have_ to use a vector, but
    double low;                 // given the way that a set can grow and shrink
    double high;                // it might be the easiest way to go.
public:
  /*Construct a new interval set containing a single starting interval (which the constructor will received as a parameter.*/
  IntervalSet(double l, double h);


And here is the main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iomanip>
#include <iostream>
#include "intervalSet.h"
#include "interval.h"

using namespace std;


void subtractBuildings(double borderLength)
{
  IntervalSet highway(0.0, borderLength);
  double x1, x2;
  cin >> x1 >> x2;
  while (x1 <= x2)
    {
      Interval building (x1, x2);
      highway.subtract(building);
      cin >> x1 >> x2;
    }
  double sum = highway.sum();
  cout << "The total planting length is "
       << setiosflags(ios::fixed) << setprecision(1) << sum << endl;
}





int main ()
{
  double L;
  cin >> L;
  subtractBuildings (L);
  return 0;
}


I tried to set up IntervalSet like Interval to read the doubles and generate an interval but it is just not working. I cannot change the interval files or the main. I can create an interval for highway, but I can't create it in the IntervalSet class. Any help would be greatly appreciated. Thanks,
Why is IntervalSet taking a high and low as arguments to its constructor? In fact, why does it have high and low members at all? It's just supposed to be a bunch of separate intervals, it seems.

If you want an initial Interval for the set, just take one:
1
2
3
IntervalSet::IntervalSet(Interval i) {
    seq.push_back(i);
}
I was trying to mimic the interval class. In the main program, the IntervalSet receives two arguments(2 doubles). I can't change that and It won't accept just an interval as an argument.
1
2
3
IntervalSet::IntervalSet(Interval i) {
    seq.push_back(i);
}

is what I originally started with but it gave me the error that it needs 2 arguments.
This is where IntervalSet is called in the main. It takes in two double variables. The interval class also takes in two double variables and makes an interval(1-10). The intervalSet class should take in the two double variables and produce an interval that is then pushed back into a vector. I cannot figure out how to use the intervalSet class to create the interval.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iomanip>
#include <iostream>
#include "intervalSet.h"
#include "interval.h"

using namespace std;


void subtractBuildings(double borderLength)
{
  IntervalSet highway(0.0, borderLength);
  double x1, x2;
  cin >> x1 >> x2;
  while (x1 <= x2)
    {
      Interval building (x1, x2);
      highway.subtract(building);
      cin >> x1 >> x2;
    }
  double sum = highway.sum();
  cout << "The total planting length is "
       << setiosflags(ios::fixed) << setprecision(1) << sum << endl;
}





int main ()
{
  double L;
  cin >> L;
  subtractBuildings (L);
  return 0;
}
This may not matter but shouldn't you be using floats as opposed to doubles? I was just thinking that you could have finicky numbers (like 9.5 and 3.4) and once those numbers run through you may get a number like 3.53987...So you using doubles, you're only going to be able to get the answer 3.5 you see?
Topic archived. No new replies allowed.