Class Inheritance Issues? - Sorting Algorithms

Hi there,

Just some background information, I've currently made sorting algorithms for heap sort, quick sort and bubble sort. They work perfectly fine when used with the class below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class SortableContainer
{	
  // storage for the new vector class 
  vector<double> v;
  public:
    // constructor
    explicit SortableContainer(){}
    explicit SortableContainer(int n):v(n){srand(static_cast<unsigned>(time(NULL)));}
    // access data in vector 
    double& operator[](int index){ return v[index]; }
    // size of vector
    int size() const {return v.size();}
	void initialise_random(double xmin, double xmax) {
		const unsigned n = this->size();
		for(unsigned i=0;i<n;i++) {
			v[i] = xmin + rand()*(xmax - xmin) / static_cast<double>(RAND_MAX);
		}
	}
	void swap(int i,int j) { double c; c=v[i]; v[i] = v[j]; v[j] = c;  }
	bool cmp(int i, int j) { 
		if (v[i] < v[j]) { return true; } 
		else { return false; } 
	}
}; // end class SortableContainer 


However, I'm trying to make a new class, which inherits from this class and has the structure:

1
2
3
4
5
struct IntegerCoordinate
{
	unsigned X;
	unsigned Y;
};


(Basically a 2D structure)

I've done this so far, but my sorting algorithms won't work properly without the commented out section being included (shown below), however apparently I should be able to use the functions from the initial class apart from overloading [] apparently.

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
class CoordinateArray: public SortableContainer
{	
  // storage for the new vector class 
  vector<IntegerCoordinate> v;
  public:

	// constructor
	explicit CoordinateArray(){}
	explicit CoordinateArray(int n):v(n){srand(static_cast<unsigned>(time(NULL)));}
	
	/*IntegerCoordinate& operator[](int index){ return v[index]; }
	IntegerCoordinate operator[](int index) const { return v[index]; }
	// functions
	int size() const {return v.size();}*/

	void set_x(int i, unsigned x) { v[i].X = x; }
	void set_y(int i, unsigned y) { v[i].Y = y; }

	double get_x(int i) { return v[i].X; }
	double get_y(int i) { return v[i].Y; }

	/*void swap(int i,int j) { 
		int a,b;
		a = get_x(i); b = get_y(i);
		set_x(i,get_x(j)); set_y(i,get_y(j));
		set_x(j,a); set_y(j,b);
	}
	  
	void initialise_random(double xmin, double xmax) {
		const unsigned n = this->size();
		for(unsigned i=0;i<n;i++) {
			v[i].X = xmin + rand()*(xmax - xmin) / static_cast<unsigned>(RAND_MAX);
			v[i].Y = xmin + rand()*(xmax - xmin) / static_cast<unsigned>(RAND_MAX);
		}
	}

	bool cmp (int i, int j) {
		if (get_x(i) < get_x(j)) { return true; }
		else if (get_x(i) == get_x(j) && get_y(i) < get_y(j)) { return true; }
		else { return false; }
	}*/

}; // end class CoordinateArray 


Any guidance would be greatly appreciated. Thank you for your time.
ocprodigy wrote:
I'm trying to make a new class, which inherits from this class and has the structure:

1
2
3
4
5
struct IntegerCoordinate
{
	unsigned X;
	unsigned Y;
};


(Basically a 2D structure)
Are you sure that you don't actually want to change SortableContainer from a container of double to a container of IntegerCoordinate? It doesn't make any sense for IntegerCoordinate to inherit from SortableContainer of double.
That does make more sense but it's an exercise and apparently it should work by just overloading [] to work with the previous functions? I'm honestly not sure, if its possible...
Sorry, does anyone have any ideas? Thank you so much
That does make more sense but it's an exercise and apparently it should work by just overloading [] to work with the previous functions? I'm honestly not sure, if its possible...


It is possible if the idea is to interpret or convert the individual elements of the existing vector of doubles to IntegerCoordinates. It is not possible if you just want to add in another data member via inheritance and reuse the existing code.

It would make more sense to convert the class to a template class where the type contained is specified by a template parameter, so it could work with doubles or IntegerCoordinates. Then, of course, you'd have to get creative with initialise_random (probably by moving it out of the class since that functionality doesn't really belong in a container.)
Topic archived. No new replies allowed.