Pointers

is this how you dynamically allocate have 10 pointer to a single object of class

1
2
3
*ptr[10]=new class_name;
ptr[0]->call();
delete[0]ptr;

and is this how you pass it to a function of arguments
void function (*a, int i)
now function call
1
2
int b=1;
function(*ptr,b);
No; nearly all of your syntax is incorrect. I'd suggest re-reading whatever section of material you have on dynamic memory. This might help:
http://www.cplusplus.com/doc/tutorial/dynamic/
Base class and derived class with reference
So is this correct
1
2
3
4
5
6
int c=0;
BaseClass *ptr[10];
DerivedClass C;
ptr[c]=&C;
ptr[c]->add();
++c;
Last edited on
Don't simply give the answer to the TC; the goal here is to help them learn, not give them the answer. Moreover your answer has its own syntax error on line 2...
YEs but its edited now no syntax error if this was dynamically allocated pointer will it be
ptr[c]=new DerivedClass
than why does my project retain previous stored values
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include<iostream>/////////////////////
#include<string>///////////////////
#include <conio.h>
#include<string.h>
#include<Windows.h>
using namespace std;
class Account
{
protected:
	 string acn;
	string act;
	float balance;
public:
	
 bool friend getdata(Account a[],int i);
	Account():acn("none"),act("none"),balance(0){}
	Account(string num, string title ,float bal):acn(num),act(title),balance(bal){}
	
	void deposit(int nm)
	{
		balance =balance+nm;
	}
	string getn()
	{
		return acn;
	}
	
		void withdraw()
	{
		int temp;
		cout<<"Enter amount \n";
		cin>>temp;
		if(temp>balance)
		{
		cout<<"Withdraw can't be greater or equal than balance\n your balance is"<<balance<<endl;
		cin>>temp;
		}
		
		else balance=balance-temp;
	}
	void virtual add()
{
	 cout<<"Account number :";
	cin>>acn;
    cout<<"Account title :";
	cin>>act;
	
}


	void virtual display()
{
	cout<<"Account number :"<<acn<<endl;
	cout<<"Account title :"<<act<<endl;
	
}
};

class SavingAccount:public Account
{
protected:
	float intrest;
	double min_bal;
	int years;
public:
	 
	SavingAccount():Account()
	{
	  intrest=.025;
	  min_bal=1000;
	  years=0;
	}
	
	void display()
	{
		balance=balance-(years*intrest);
		cout<<"Balance : "<<balance<<endl;
		cout<<"Intrest : "<<intrest<<endl;
		cout<<"Minimum balance : "<<min_bal<<endl; 
	}
	void add()
	{
		int temp=0;
     Account::add();
	 cout<<"Enter amount of years\n";
	 cin>>years;
	 cout<<"Balance : ";
	cin>>temp;
	if(temp<1000)
	{
		cout<<"minimum balance required is 1000\n";
	cout<<"Enter Balance : ";
	cin>>temp;
	}
	else 
		balance=temp;
	}


};
class CurrentAccount:public Account
{
protected:
	float fee;
	float bonus;//intrest
public:
	
	CurrentAccount():Account()
	{
		fee=250;
		bonus=7.5;
	}
	void display()
	{
		balance=((balance*(bonus))-fee);
		cout<<"Balance : "<<balance<<endl;
		cout<<"Fee : "<<fee<<endl;
		cout<<"intrest :"<<bonus<<endl;
	}
	void add()
	{
		Account::add();
		cout<<"Enter Balance\n";
		cin>>balance;
	}
	
};
bool getdata(Account a[],int i)
 {
	 cout<<"get data called\n";
	 string account_num;
	 int flag=0;
	 Account temp;
	 cout<<"Enter Account number\n";
	 cin>>account_num;
	 for(int j=0;j<=i;++j)
	 {
		 if(a[j].acn==account_num)
		 {
			 
			 return true;
			 break;
		 }
	 }
	 return false;
 }
void inline call()
{
cout<<"Enter 'C' for current account or 'S' for saving account \n";
}
int main()
{
	cout<<"*****************************************************\n";
	cout<<"***********BANK MANAGEMENT SYSTEM********************\n";
	cout<<"*****************************************************\n";
	int c=0;
	int s=0;
	int option;
	char choice;
	Account *ptrc[10];
	Account *ptrs[10];
	CurrentAccount C;
	//for(int i=0;i<10;++i)
	//{
	//ptrc[i]= new CurrentAccount;
	//}
	//for(int i=0;i<10;++i)
	//{
	//ptrs[i]= new SavingAccount;
	//}
	
	while (1)
	{cout<<"Enter option \n";
	cout<<"1 Create Account \n";
	cout<<"2 Withdraw \n";
	cout<<"3 Deposit \n";
	cout<<"4 Display details\n";
	cout<<"5 Exit\n";
	cin>>option;
	if(option==1)
	{call();
		cin>>choice;
		if(choice=='c'||choice=='C')
		{
			cout<<"select option\n";
			ptrc[c]=&C;
			ptrc[c]->add();
			++c;
		}
		if(choice=='s'||choice=='S')
		{
			cout<<"select option\n";
			/*ptrs[s]=&S;*/
			ptrs[s]->add();
			++s;
		}
	}
	if(option==2)
	{call();
		cin>>choice;
		if(choice=='c'||choice=='C')
		{
			
			if(getdata(*ptrc,c)==true)
			{
			ptrc[c]->withdraw();
			}
			else
			{
				cout<<"Account not found\n";
			}
	
			
		}
		if(choice=='s'||choice=='S')
		{
			
			if(getdata(*ptrs,s)==true)
			{
			ptrs[s]->withdraw();
			}
			else
			{
				cout<<"Account not found\n";
			}
	
			
	}
	}if(option==3)
	{call();
		cin>>choice;
		if(choice=='c'||choice=='C')
		{
			int nm=0;
			
			if(getdata(*ptrc,c)==true)
			{
				cout<<"Enter amount to depist\n";
				cin>>nm;
			ptrc[c]->deposit(nm);
			}
			else
			{
				cout<<"Account not found\n";
			}
	
			
		}
		if(choice=='s'||choice=='S')
		{
			int nm=0;
			
			if(getdata(*ptrs,s)==true)
			{
			ptrs[s]->deposit(nm);
			}
			else
			{
				cout<<"Account not found\n";
			}
	
			
	}
	}if(option==4)
	{call();
		cin>>choice;
		if(choice=='c'||choice=='C')
		{
			
			if(getdata(*ptrc,c)==true)
			{
			ptrc[c]->display();
			}
			else
			{
				cout<<"Account not found\n";
			}
	
			
		}
		if(choice=='s'||choice=='S')
		{
			
			if(getdata(*ptrc,c)==true)
			{
			ptrc[c]->display();
			}
			else
			{
				cout<<"Account not found\n";
			}
	
			
		}

	
	}
	

getch();

}
}



Topic archived. No new replies allowed.