Polymorphism

I have this question for university...

Use your platform to create a project that input 10 instrument quotes from your keyboard, save the quotes to an array, and then save them to a text file.

Here, instrument includes only stock and option. The stock quote includes stock name(12 characters), bid price, bid size, ask price, ask size, and issue exchange. The option quote includes option name (26 characters), underlying name(12 characters), bid price, bid size, ask price, ask size, and expire date.

The instrument quote, the stock quote and the option quote must be handled by classes. The methods, input, save to an array, and save to text file must be implemented by polymorphism concept.


I am having trouble with the polymorphism concept...I have read about virtual functions and such but I'm not sure how to implement them in this situation.

My text file is also just outputting the last myoption[I] and mystock[I] in the respective tables how ever many times I. So in the end there is the same option information 10 times and the same stock information 10 times. Not sure how to fix that.

Thanks for any help and suggestions in advance.

TFish

I have 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


#ifndef INSTRUMENT_H_
#define INSTRUMENT_H_

#include <iostream>
#include <string>
using namespace std;

class Instrument
{
public:

	float bid_price;
	int bid_size;
	float ask_price;
	int ask_size;
};

class Option : public Instrument
{
public:
	char option[26];
	char under[12];
	int t_month;
	int t_day;
	int t_year;

};

class Stock : public Instrument
{
public:
	char stock[12];
	string Issue;
};


#endif /* INSTRUMENT_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
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


#include <iostream>
#include <fstream>
#include <string>
#include "Instrument.h"

using namespace std;


int I,m;


int main(){
	cout <<"How many instruments will you input?"<<endl;
	cin >> I;
	Option myOption[10];//Initializes option array
	Stock myStock[10];//Initializes stock array
	for( int f=0; f<I; f++)//runs a loop with a choice for the option or stock
	{
	cout <<"Is this an Option or Stock?"<<endl;
	cout <<"Type '1' for Option and '2' for Stock..."<<endl;
	cin >> m;
	switch(m)//Has user decide what the instrument is
	{
	case 1:

		cout <<"What is the option name?"<<endl;
		cin >>myOption[I].option;
		cout <<"what is the underlying of the option?"<<endl;
		cin >>myOption[I].under;
		cout <<"What is the bid price?"<<endl;
		cin >>myOption[I].bid_price;
		cout <<"What is the bid size?"<<endl;
		cin >>myOption[I].bid_size;
		cout <<"What is the ask price?"<<endl;
		cin >>myOption[I].ask_price;
		cout <<"What is the ask size?"<<endl;
		cin>>myOption[I].ask_size;
		cout<<"What is the Expire date?"<<endl;
		cout<<"Month(Numeric-MM): ";
		cin>>myOption[I].t_month;
		cout<<"Date(DD): ";
		cin>>myOption[I].t_day;
		cout<<"Year(YYYY): ";
		cin>>myOption[I].t_year;
		break;

	case 2:

		cout<<"What is the stock name?"<<endl;
		cin>>myStock[I].stock;
		cout<<"What is the bid price?"<<endl;
		cin>>myStock[I].bid_price;
		cout<<"What is the bid size?"<<endl;
		cin>>myStock[I].bid_size;
		cout<<"What is the ask price?"<<endl;
		cin>>myStock[I].ask_price;
		cout<<"What is the ask size?"<<endl;
		cin>>myStock[I].ask_size;
		cout<<"What is the Issue Exchange?"<<endl;
		cin>>myStock[I].Issue;
		break;

	default :
		cout<<"Not the correct input.  Please enter '1' for Option and '2' for Stock."<<endl;
		I=I+1;
		break;


};
	};

	// Creates the text file 'Instrument'
		ofstream myfile;

		myfile.open("Instrument.txt");

		//Creates the table for the Option Array
		myfile << "Option Name | ";
		myfile.width(12);  //width function used so that there is consistency in the spacing
		myfile<< "Underlying |" ;
		myfile.width(10);
		myfile<< "Bid Price |" ;
		myfile.width(10);
		myfile << "Bid Size |";
		myfile.width(10);
		myfile << "Ask Price |";
		myfile.width(10);
		myfile << "Ask Size |";
		myfile.width(10);
		myfile << "Expiration";
		myfile.width(10);
		myfile<< "\n";

		//Loop to fill in the option table
	for (int z=0;z<I;z++)
	{
		myfile << myOption[I].option << " | ";
		myfile.width(12);  //width function used so that there is consistency in the spacing
		myfile<< myOption[I].under << " | " ;
		myfile.width(10);
		myfile<< myOption[I].bid_price <<  " | " ;
		myfile.width(10);
		myfile << myOption[I].bid_size;
		myfile.width(10);
		myfile << myOption[I].ask_price << " | ";
		myfile.width(10);
		myfile << myOption[I].ask_size << " | ";
		myfile.width(10);
		myfile << myOption[I].t_month << "/" << myOption[I].t_day << "/" << myOption[I].t_year;
		myfile.width(10);
		myfile << "\n";


	}

	//Creates the table for the Stock Array
	myfile<<"\n";
	myfile<<"\n";
	myfile<<"\n";
	myfile << "Stock Name | ";
	myfile.width(12);
	myfile<< "Bid Price |" ;
	myfile.width(10);
	myfile << "Bid Size |";
	myfile.width(10);
	myfile << "Ask Price |";
	myfile.width(10);
	myfile << "Ask Size |";
	myfile.width(10);
	myfile << "Issue Exchange";
	myfile.width(10);
	myfile<< "\n";

	//Loop to fill in the stock table
	for (int z=0;z<I;z++)
	{
		myfile << myStock[I].stock;
		myfile.width(12);
		myfile << myStock[I].bid_price;
		myfile.width(10);
		myfile << myStock[I].bid_size;
		myfile.width(10);
		myfile << myStock[I].ask_price;
		myfile.width(10);
		myfile << myStock[I].ask_size;
		myfile.width(10);
		myfile << myStock[I].Issue;
		myfile.width(10);
		myfile << "\n";
	}

	myfile.close();
	return 0;
};


Last edited on
I think in your loop below, you need to change I
for (int z=0;z<5;z++)
EricDu,

I don't believe that is the reason for that...I have changed it and it still does the same thing.

Thanks anyways.

Tom
@tfish1714

I think EricDu was referring to myfile << myStock[I].stock; and the rest in the loop. It should be myfile << myStock[z].stock; since that is the variable being increased up to the variable I.
@EricDu & whitenite1,

Thanks...I guess I was looking at it for so long I didn't even realize that mistake.

Topic archived. No new replies allowed.