polymorphic arrays run time error

For some reason, when my program runs, it doesn't execute the code at the bottom of my for loop in my main routine. I'm pretty sure that the classes I wrote are good. Here is my main.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
#include <iostream>
#include <string>
#include <vector>
#include "worker.h"
using namespace std;

int main () {
	double wage=1, salary=1;
	char enterNewName = 'y';
	string name, type;
	vector<Worker*> workers;
	
	for (int i=0; (enterNewName=='y' || enterNewName=='Y'); i++) {
		cout << "Enter name of worker: ";
		cin >> name;
		cout << "Enter type of worker (hourly / salaried): ";
		cin >> type;
		
		if (type == "hourly" || type == "Hourly") {
			cout << "Enter wage: $";
			cin >> wage;
			workers[i] = new HourlyWorker(name,wage);
		}
		else if (type == "salaried" || type == "Salaried") {
			cout << "Enter overtime wage: $";
			cin >> wage;
			workers[i] = new SalariedWorker(name,salary,wage);
		}
		else
			workers[i] = new Worker(name);
			
		cout << "Enter another name? (y/n): ";
		cin >> enterNewName;
	}
	
	cout << workers[0] -> get_name();
	
    return 0;
}

------------------------------
Here are the class declarations and definitions:
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
#include <string>

using namespace std;

class Worker {
	public:
		Worker();
		Worker(string my_name);
		virtual double compute_pay(int hours) const;
		string get_name() const;
	private:
		string name;
};

class HourlyWorker: public Worker {
	public:
		HourlyWorker();
		HourlyWorker(string my_name, double my_wage);
		virtual double compute_pay(int hours) const;
	private:
		double wage;
};

class SalariedWorker: public Worker {
	public:
		SalariedWorker();
		SalariedWorker(string my_name, double my_salary, double my_overtimeWage);
		virtual double compute_pay(int hours) const;
	private:
		double salary;
		double overtimeWage;
};


And the definitions:
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

#include "worker.h"

Worker::Worker() {

}

Worker::Worker(string my_name) {
	name = my_name;
}

double Worker::compute_pay(int hours) const {
	return 0;
}

string Worker::get_name() const {
	return name;
}

HourlyWorker::HourlyWorker(): Worker() {

}

HourlyWorker::HourlyWorker(string my_name, double my_wage): Worker(my_name) {
	wage = my_wage;
}

double HourlyWorker::compute_pay(int hours) const {
	if (hours <= 40)
		return wage*hours;
	else
		return (wage*40 + (hours-40)*1.5*wage);
}


SalariedWorker::SalariedWorker(): Worker() {

}

SalariedWorker::SalariedWorker(string my_name, double my_salary, double my_overtimeWage): Worker(my_name) {
	salary = my_salary;
	overtimeWage = my_overtimeWage;
}

double SalariedWorker::compute_pay(int hours) const {
	if (hours <= 40)
		return salary;
	else
		return salary + (hours-40)*overtimeWage;
}

-------------------

Here is an example of what it does when it runs in the console window:

[Session started at 2008-12-19 01:10:33 -0800.]
Enter name of worker: jeff
Enter type of worker (hourly / salaried): hourly
Enter wage: $34

[Session started at 2008-12-19 01:10:40 -0800.]
Loading program into debugger…
GNU gdb 6.3.50-20050815 (Apple version gdb-768) (Tue Oct 2 04:07:49 UTC 2007)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-apple-darwin".Program loaded.
sharedlibrary apply-load-rules all
Attaching to program: `/Users/joesilverstein/worker/build/Release/worker', process 85480.
(gdb)

------------------------

Any help would be greatly appreciated. Thanks!
Last edited on
if you change the main workers[i] = ... into

 
workers.push_back( new SalariedWorker(name,salary,wage));

change the all three places.
you can have a working program. But you loss the fun of debugging.

if you don't have the debugging knowledge then read gdb manual or
debugging tutorial on the internet and get it. or you can use
cout << " -- debug : comes to line 72 -- " << endl ;
like this , use it inside the trace macro.

anyway the problem is with that vector. Just read the stl vector page.
www.cplusplus.com have the documentation for that class. search and find it.

and the problem there is you cannot access with [] operator before there
is an element exists there.

bye.
Last edited on
Topic archived. No new replies allowed.