Subclasses

Here is my code

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
#include <string>
#include <vector>
using namespace std;

//exception for incomplete object
struct inc_obj
{
	string str;
	inc_obj(string iStr) { str = iStr; }			//constructor function

};

//exception for missing value for int type
struct miss_val
{
	string type;
	miss_val(string mVal) { type = mVal; }			//constructor function
};

//exception for negative integer values
struct neg_int
{
	int num;
	neg_int(int negVal) { num = negVal; }			//constructor fucntion 
};

//exception for unrecognized type
struct wrong_type
{
	string type;
	wrong_type(string wType) { type = wType; }		//constructor function
};

//exception for invalid age
struct invalid_age 
{
	int age;
	invalid_age(int iAge) { age = iAge; }
};


//class type named MyObjects
class MyObjects
{
protected:
	int age; float dmt; string sub_name;			//delcare members private to this class
	class MyObjects* next;						//declare private pointer to the class
public:
	MyObjects(int _age, float _dmt, string _sub_name) : age(_age), dmt(_dmt), sub_name(_sub_name), next(0) {}	//constructor function and members
	//MyObjects(const MyObjects& obj) : age(obj.age), dmt(obj.dmt), sub_name(obj.sub_name), next (0) {}
	virtual ~MyObjects() { if (next != 0) delete next; }	//virtual destructor
	virtual float get_speed() const {};						//
	virtual string get_name() const {};
	string get_sub_name() const { return sub_name; }			//function returns name of object
	int get_age() const { return age; }						//function returns age of object	
	float get_diameter() const { return dmt; }				//function returns diameter of object
	int year_passed(int years) { return age += years; }		//function return age incremented by years
};

//Derived Human class of MyObjects
class Human : public MyObjects {
	class Human* next;
public:
	Human(int age, float dmt, string sub_name) : MyObjects(age,dmt,sub_name) {}
	virtual ~Human() { if (next != 0) delete next; }
	virtual float get_speed() const {
		if (age == 0) return 0; 
		if (age > 1 && age <= 30) return age*0.5;		
		if (age > 30 && age <= 65) return 15;
		if (age > 65 && age <= 95) return age / 0.5;
		if (age > 95 && age <= 100) return 0;
		if (age < 0 || age > 100) throw invalid_age(age);
	}
	virtual string get_name() const { return "Human"; }
};

//Derived Car class of Myobjects
class Car : public MyObjects {
	class Car* next;
public:
	Car(int age,float dmt, string sub_name) : MyObjects(age,dmt,sub_name) {}
	virtual ~Car() { if (next != 0) delete next; }
	virtual float get_speed() const {return 50;}
	virtual  string get_name() const { return "Car"; }
};

//derived tiger class of MyObjects
class Tiger : public MyObjects {
	class Tiger* next;
public:
	Tiger(int age,float dmt,string sub_name) : MyObjects(age,dmt,sub_name) {}
	virtual ~Tiger() { if (next != 0) delete next; }
	virtual float get_speed() const {
		if (age == 0) return 5;
		if (age > 0 && age <= 26) return 5 + age;
		if (age< 0 || age > 26) throw invalid_age(age);		
	}
	virtual string get_name() const { return "Tiger"; }
};

//Overlaod output operator
ostream& operator << (ostream& out, const MyObjects& obj)		
{
	out << "(" << obj.get_name() << "," << obj.get_sub_name() << "," << obj.get_diameter() << "," << obj.get_age() << "," << obj.get_speed() << ")";
	return out;
}

vector <MyObjects*> objs;		//declare vector

MyObjects* new_object(string sub_name_val, int ageVal, float dmtVal) {
	
	
	MyObjects* sub_obj;

	if (sub_name_val == "H" || sub_name_val == "h") {
		if (ageVal > 0 && ageVal < 100)
		sub_obj= new Human(ageVal,dmtVal,sub_name_val);
		else throw invalid_age(ageVal);
	}

	if (sub_name_val == "T" || sub_name_val == "t") {
		if (ageVal > 0 && ageVal < 26 )
		sub_obj = new Tiger(ageVal,dmtVal,sub_name_val);	
		else throw invalid_age(ageVal);
	}

	if (sub_name_val == "C" || sub_name_val == "c") {
		if (ageVal > 0 && ageVal < 50)
		sub_obj = new Car(ageVal,dmtVal,sub_name_val);
		else throw invalid_age(ageVal);
	}

	return sub_obj;
		
}

//function to read inputs check for errors and organizes data into collection of objects
void readObjects() {

	int temp_age;
	int iCount = 0;					//declare and initialize int variable to hold number of integer items
	int fCount = 0;					//declare and initialize int variable to hold number of float items
	string type;					//declare string variable to hold string type
	int age_val;					//declare int variable to hold age of obejct
	float dmt_val;					//declare float variable to hold diameter of object
	string sub_name_val;				//declare string variable to hold sub name of object
	bool age_ready = false, dmt_ready = false, name_ready = false;

	cin >> type;							//get first input

	while (cin) {							//while loop keeps reading input

		try {
			if (type == "int") {							
				cin >> age_val;									//get value of age					
				
				if (age_val < 0) throw neg_int(age_val);	//if negative value is entered throw an exception
				if (!cin) throw miss_val(type);			//if no integer is entered throw an exception				

				age_ready = true;							//mark that int value has been read
				temp_age = age_val;
				iCount++;									//increment iCount
			}
			else if (type == "float") {
				cin >> dmt_val;									//get value of float
				dmt_ready = true;								//mark that float value has been read
				fCount++;										//increment fCount
			}
			else if (type == "string") {
				cin >> sub_name_val;								//get value of name
				if (iCount < 1 || fCount < 1)				//if no int or float has been read throw exception
					throw inc_obj(sub_name_val);
				else {					
					name_ready = true;								//mark that string value has been read
					if (dmt_ready && age_ready)						//if int and float has been read push back values into class
						new_object(sub_name_val,temp_age, dmt_val);
				}
				
			}
			else throw wrong_type(type);				//if uncrecognized type is entered throw exception	
			cin >> type;							//get input again
		}

		catch (inc_obj) {					//catch incomplete object error 
			cerr << "Error: incomplete object encountered:" << sub_name_val << "\n";
			cin.clear();
			cin >> type;
		}
		catch (wrong_type) {		  			//catch unrecognized type error
			cerr << "Error: unrecognized type:" << type << "\n";
			cin.clear();
			cin >> type;
		}
		catch (neg_int) {					//catch negative integer error
			cerr << "Error: no integer should be negative:" << age_val << "\n";
		
			cin.clear();
			cin >> type;
		}
		catch (miss_val) {					//catch missing value error
			cin.clear();
			cin >> type;
			cerr << "Error: missing value for int type:" << type << "\n";
		}
	}

	
}



//main function calls readObjects function and outputs the result
int main() {	
		
	//call readObjects function
	readObjects();		
	//output summary
	for (int i = 0; i < objs.size(); ++i) {
		cout << *(objs[i]);
	}					


	cout << "\n";
	return 0;
}


My input: int 5 float 3.2 string t1 int 49 string car...
I literally get nothing on my output, just blank space. I do however catch and output the errors.(There is none in this input)
I should get something like :
(Tiger,5,3.2,2.5) (Car,49,3,2,50)
Last edited on
Please don't double post :+)

It's not cool because we could have multiple peopl replying, and saying the same thing as someone else. It is especially annoying when someone responds with a long reply, only to find someone else has said the same things in the other post.

Cheers
Topic archived. No new replies allowed.