Comapre Two diff data members

class member
{
public:
int mcode;
void min()
{
cout<<"\nMcode:\t";
cin>>mcode;
}
};

class product:public member
{
public:
int pcode;
void pin()
{
cout<<"\nPcode:\t";
cin>>pcode;
}
void pout()
{
int mas;
mas=m.mcode;
cout<<mas;
cout<<endl;

if(pcode==mas)
cout<<"True";
else
cout<<"False";
}
};

void main()
{
clrscr();

member m;
product p;

m.min();
p.pin();
p.pout();

getch();
}




Guys i hav created two different classes with data member Pcode,mcode.
I want to compare these data member. i am able to do so in void main but unable to perform the task using a function. So pls help me
Hi!

The purpose of a class is that you can hide the data member and attach the function (which can operate with hidden data members.) So it is rewarding make private data members.

1
2
3
4
5
6
7
8
9
class example
{
 int m; // default access level is private

public:
 int getMember(){return m;} // You can access the member with getMember() and setMember
 void setMember(int a){m = a;}

};



If you make any variable, object in a scope, then you can't access them from other scope. Therefore you get error at this line:

mas=m.mcode;


You should make a similar code:
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	member m;
	product p;
	
	if (m.getMember() == p.getMember())
	{
		cout << "They are equal" << endl;
	}
	else cout << "They aren't equal" << endl;
	
	return 0;
}



(of course you need put #include <iostream> and using namespace std; at the beginning of your code)


The other solution is that you use operator overloading technology so you can use == operator between objects.

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=502

http://www.google.com/search?hl=hu&q=c%2B%2B+operator+overloading&btnG=Keres%C3%A9s&lr=
Thnks buddy for the reply. But my problem is yet unsolved.

Bro i am using TURBO C++ 3.0.

Problem:
Actually there are two classes named product and sales. i want to compare the data variables. I knw hw to do it by using formal argunments but i want to do without using it

void sales::sin()
{
clrscr();

cout<<"Customers Name:\t";
puts(billname);

cout<<"\n\nProduct Code:\t";
cin>>scode;

//Actually I want to do the folowing:
if(sales::scode == product::code)
{
strcpy(sales::sname,product::name);
cout<<sales::sname;

sales::srate=product::rate;
cout<<sales::srate;

start:
cout<<"Quantity:\t";
cin>>sales::qty;
if(sales::qty > product::stock)
{
cout<<"\nOut of Stock";
cout<<"\nEnter Again";
goto start;
cout<<"\n";
}

cout<<"Total:\t";
total=srate*qty;
}

cout<<"Wrong Product code entered";
}


The full program code is here:

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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include<iostream.h>
#include<conio.h>
#include<dos.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>

class product
{
public:
	void pin();
	void pout();
	void pedit();
//private:
	double rate;
	double stock;
	double code;
	char name[50];
};

class member
{
public:
	void min();
	void mout();
	void medit();
	void mcard();
//private:
	char memname[50];
	int no;
	char add[75];
	char tel[25];
	double card;
	long double amt;
};

class sales: public product
{
public:
	void sin();
	void sout();
//private:
	double scode;
	double qty;
	char sname[50];
	double srate;
	double total;

};

char billname[50];

void member::min()
{

	clrscr();

	int q;

	fstream m;

	m.open("member.dat", ios::in | ios::binary);
	m.seekg(0,ios::end);
	q=m.tellg()/sizeof(member);
	q+=1000;
	m.close();

	m.open("member.dat", ios::in | ios::app | ios::binary);

	char x;

	do
	{
		clrscr();
		cout<<"\nEnter Members Detail:\n\n";
		cout<<"Name:\t";
		gets(memname);
		cout<<"Card No:\t";
		cin>>card;
		cout<<"Address:\t";
		gets(add);
		cout<<"Tel:\t";
		cin>>tel;
		cout<<"Amount Deposit:\tRs";
		cin>>amt;
		cout<<"Member No:\t";
		no=q++;
		cout<<no;
		no++;

		m.write((char*)this,sizeof(member));

		cout<<"\n\nEnter More(Y/N)?";
		x=getch();
	}while(x=='y' || x=='Y');

	cout<<"No";
	m.close();
}


void member::mout()
{
	clrscr();

	fstream m;
	m.open("member.dat", ios::in | ios::binary);

	m.read((char*)this,sizeof(member));

	cout<<"\nMembers Detail:\n";

	while(!m.eof())
	{
		cout<<"\n\nName:\t";
		puts(memname);
		cout<<"\nCard Number\t";
		cout<<card;
		cout<<"\nAddress:\t";
		puts(add);
		cout<<"Tel\t";
		cout<<tel;
		cout<<"\nAmount Deposit:\t";
		cout<<amt;
		cout<<"\nMember No:\t";
		cout<<no;

		m.read((char*)this,sizeof(member));
	}

	m.close();
}

void member::medit()
{
	clrscr();
	fstream m;
	m.open("member.dat" ,ios::in | ios::out | ios::binary);

	int p=0, loc, buff=0;

	int x,xa;
	cout<<"\nMember No:\t";
	cin>>x;

	m.read((char*)this,sizeof(member));

	while(!m.eof())
	{
		if(x==no)
		{
			buff=100;
			break;
		}
		p++;
		m.read((char*)this,sizeof(member));
	}

	loc=p*sizeof(member);
	m.seekp(loc);

	if(buff!=0)
	{
		cout<<"\n\nCurrent Detail:\n\n";
		cout<<"Name:\t";
		puts(memname);
		cout<<"\nCard Number\t";
		cout<<card;
		cout<<"\nAddress:\t";
		puts(add);
		cout<<"Tel\t";
		cout<<tel;
		cout<<"\nAmount Deposit:\t";
		cout<<amt;
		cout<<"\nMember No:\t";
		cout<<no;

		cout<<"\n\n\nEnter Members Detail:\n\n";

		cout<<"Name:\t";
		gets(memname);
		cout<<"Card No:\t";
		cin>>card;
		cout<<"Address:\t";
		gets(add);
		cout<<"Tel:\t";
		cin>>tel;
		cout<<"Amount Deposit:\tRs";
		cin>>amt;
		cout<<"Member No:\t";
		cout<<no;

		m.write((char*)this,sizeof(member));
	}

	else

	cout<<"Data MisMatached";

	cout<<"\n\nMore Modification(Y/N)?";
	xa=getch();
	if(xa=='y' || xa=='Y')
	{
		cout<<"Yes";
		medit();
	}
	else
	cout<<"No";

	m.close();
}

void product::pin()
{

	clrscr();

	int q;

	fstream f;

	f.open("product.dat", ios::in | ios::binary);
	f.seekg(0,ios::end);
	q=f.tellg()/sizeof(product);
	q+=1000;
	f.close();

	f.open("product.dat", ios::in | ios::app | ios::binary);

	char x;

	do
	{
		clrscr();
		cout<<"\nEnter Products Detail:\n\n";
		cout<<"Name:\t";
		gets(name);
		cout<<"Rate:\tRs ";
		cin>>rate;
		cout<<"Stock:\t";
		cin>>stock;
		cout<<"Code:\t";
		code=q++;
		cout<<code;
		code++;

		f.write((char*)this,sizeof(product));

		cout<<"\n\nEnter More(Y/N)?";
		x=getch();
	}while(x=='y' || x=='Y');

	cout<<"No";
	f.close();
}


void product::pout()
{
	clrscr();

	fstream f;
	f.open("product.dat", ios::in | ios::binary);

	f.read((char*)this,sizeof(product));

	cout<<"\nProducts Detail:\n";

	while(!f.eof())
	{

		cout<<"\n\nName:\t";
		puts(name);
		cout<<"Rate:\tRs ";
		cout<<rate;
		cout<<"\nStock:\t";
		cout<<stock;
		cout<<"\nCode:\t";
		cout<<code;

		f.read((char*)this,sizeof(product));
	}

	f.close();
}

void product::pedit()
{
	clrscr();
	fstream f;
	f.open("product.dat" ,ios::in | ios::out | ios::binary);

	int p=0, loc, buff=0;

	int x,xa;
	cout<<"\nProducts Code:\t";
	cin>>x;

	f.read((char*)this,sizeof(product));

	while(!f.eof())
	{
		if(x==code)
		{
			buff=100;
			break;
		}
		p++;
		f.read((char*)this,sizeof(product));
	}

	loc=p*sizeof(product);
	f.seekp(loc);

	if(buff!=0)
	{
		cout<<"\n\nCurrent Detail:\n\n";
		cout<<"Name:\t";
		puts(name);
		cout<<"Rate:\tRs ";
		cout<<rate;
		cout<<"\nStock:\t";
		cout<<stock;
		cout<<"\nCode:\t";
		cout<<code;

		cout<<"\n\n\nEnter Products Detail:\n\n";

		cout<<"Name:\t";
		gets(name);
		cout<<"Rate:\tRs ";
		cin>>rate;
		cout<<"Stock:\t";
		cin>>stock;
		cout<<"Code:\t";
		cout<<code;

		f.write((char*)this,sizeof(product));
	}

	else

	cout<<"Data MisMatached";

	cout<<"\n\nMore Modification(Y/N)?";
	xa=getch();

	if(xa=='y' || xa=='Y')
	{
		cout<<"Yes";
		pedit();
	}

	else
	cout<<"No";

	f.close();
}

void member::mcard()
{
	clrscr();

	char x;
	int cardnum,memnum,check=1;
	cout<<"Are you Member(Y/N)";
	x=getch();

	if(x=='y' || x=='Y')
	{
		cout<<"Yes";
		cout<<"\n\n\tMember no:\t";
		cin>>memnum;
		cout<<"\tCard no:\t";
		cin>>cardnum;

		fstream m;
		m.open("member.dat", ios::in | ios::binary);

		m.read((char*)this,sizeof(member));

		while(!m.eof())
		{
			if(memnum==no && cardnum==card)
			{
				check=0;
				strcpy(billname,memname);
				break;
			}
			m.read((char*)this,sizeof(member));
		}

		if(check!=0)
		{
			cout<<"\nWrong Info Enterd";
			cout<<"\n\nTry Again:\t";
			x=getch();
			if(x=='y' || x=='Y')
			{
				cout<<"Yes";
				mcard();
			}
			else
			cout<<"No";
			goto end;
		}

	}

	else
	{
		cout<<"No";
		cout<<"\n\n\tName:\t";
		gets(billname);
	}

	sales s;
	s.sin();

	end:
}

void sales::sin()
{
	clrscr();

	cout<<"Customers Name:\t";
	puts(billname);

	cout<<"\n\nProduct Code:\t";
	cin>>scode;

       //Actually I want to do the folowing:
		if(sales::scode == product::code)
		{
			strcpy(sales::sname,product::name);
			cout<<sales::sname;

			sales::srate=product::rate;
			cout<<sales::srate;

			start:
			cout<<"Quantity:\t";
			cin>>sales::qty;
			if(sales::qty >  product::stock)
			{
				cout<<"\nOut of Stock";
				cout<<"\nEnter Again";
				goto start;
				cout<<"\n";
			}

			cout<<"Total:\t";
			total=srate*qty;
		}

		cout<<"Wrong Product code entered";
}

void mainmenu()
{

	clrscr();
	int x;
	char xx;

	product p;
	member m;
	sales s;

	cout<<"Main Menu\n\n";

	cout<<"Products\n";
	cout<<"\t\t1.Add New Products\n";
	cout<<"\t\t2.View Existing Products\n";
	cout<<"\t\t3.Modify Existing Prodcuts\n";

	cout<<"\nMembers\n";
	cout<<"\t\t4.Add New Members\n";
	cout<<"\t\t5.View Existing Members\n";
	cout<<"\t\t6.Modify Existing Members\n";

	cout<<"\nBilling\n";
	cout<<"\t\t7.Billing\n";

	cout<<"\n\tEnter Choice:\t";

	cin>>x;

	switch(x)
	{

		case 1:
		p.pin();
		break;

		case 2:
		p.pout();
		break;

		case 3:
		p.pedit();
		break;

		case 4:
		m.min();
		break;

		case 5:
		m.mout();
		break;

		case 6:
		m.medit();
		break;

		case 7:
		m.mcard();
		break;
	 }

	cout<<"\n\nBack to Main Menu(Y/N)?\t";
	xx=getch();

	if(xx=='y' || xx=='Y')
	{
		cout<<"Yes";
		mainmenu();
	}
	else
	{
		cout<<"No";
		cout<<"\nBye! Will Meet Soon!";
	}

}

void main()
{
	clrscr();
	mainmenu();
	getch();
}

Last edited on
Here is a problem:

if(sales::scode == product::code)
{
strcpy(sales::sname,product::name);
cout<<sales::sname;
...

You have to refer for the members by . or -> symbols
For example:

if (sales.scode == product.code)
{
strcpy(sales.sname,product.name);


(You can refer to members with using :: but the data and function members are releated to class and not object. So here you should use . symbol)
Topic archived. No new replies allowed.