inheritance

i am making a program to store data of bank
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
#include<iostream.h>
#include<conio.h>
#include<string.h>
class bank_entry
{
protected:
char ac_holder,address;
long int ac_no,transaction_id,pincode,phone_no,balance;
static long int count1,count2;
public:
	bank_entry()
	{
	balance=0;
	}
	void getac_holder()
	{
	cout<<"enter your name ";
	cin>>ac_holder;
	}
	void getaddress()
	{
	cout<<"enter your address ";
	cin>>address;
	}
	void getpincode()
	{
	cout<<"enter pincode ";
	cin>>pincode;
	}
	void getac_no()
	{
	ac_no=count1;
	count1++;
	}
	void gettransaction_id()
	{
	transaction_id=count2;
	}
	void getphone_no()
	{
	cout<<"enter your phone no: ";
	cin>>phone_no;
	}
	void print()
	{
	cout<<ac_holder<<endl<<address<<" "<<pincode<<endl<<ac_no<<endl<<transaction_id;
	}
};
long int bank_entry::count1=10000;
long int bank_entry::count2=556562;

class bank_find:public bank_entry
{
protected:
long int ac_no1,ac_no2,transaction_id_find,amount;
public:
	void getac_no1()
	{
	cout<<"enter your a/c no: ";
	cin>>ac_no1;
	}
		void getac_no2()
	{
	cout<<"enter the acount to which money to be transfered ";
	cin>>ac_no2;
	}
		void gettransaction_id_find()
	{
	cout<<"enter your transacton id: ";
	cin>>transaction_id_find;
	}
	void newbalance(bank_entry &ob)
	{
		ob.balance+=amount;
	}
	int compare1(bank_entry &ob)
	{
	int a=0;
	if(ob.ac_no==ac_no1)
	{
		a=1;
		gettransaction_id_find();
		if(ob.transaction_id==transaction_id_find)
		{
			getac_no2();
			if(amount<=ob.balance)
			{
				newbalance(bank_entry &ob);
			}
		}
	}
	return a;
	} 
};
void main()
{
bank_entry detail[100];
bank_find find;
int a,b,i,c;

for(i=0;i<100;i++)
{
detail[i].getac_holder();
detail[i].getaddress();
detail[i].getpincode();
detail[i].getphone_no();
detail[i].getac_no();
detail[i].gettransaction_id();
detail[i].print();
again1:
cout<<"\npress 1 to continue\npress 2 to exit";
cin>>b;
switch(b)
{
	case 1:
         break;
    case 2:
	     goto end;
	default:cout<<"invalid entry";goto again1;

}
}
find.getac_no1();
for(i=0;i<100;i++)
{
b=find.compare1(detail[i]);
if(b==1)
{
        c=i;
}
}
find.newbalance(detail[c]);
detail[c].print();
end:
getch();
return 0;
}

and i want to use inheritance for this program than using friend function but
i am unable to USE the various entries of account!!
Last edited on
There is some bad code here. If you get a C++ compiler from the last decade a lot of this will refuse to compile. Some of it hasn't been C++ since 1998, and some of it has never been C++.

Once I tidy away the worst of the incorrect C++, I end up with this, which is not good but does compile and run.

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
#include<iostream>

using namespace std;

class bank_entry
{
protected:
char ac_holder,address;
long int ac_no,transaction_id,pincode,phone_no,balance;
static long int count1,count2;
public:
	bank_entry()
	{
	balance=0;
	}
	void getac_holder()
	{
	cout<<"enter your name ";
	cin>>ac_holder;
	}
	void getaddress()
	{
	cout<<"enter your address ";
	cin>>address;
	}
	void getpincode()
	{
	cout<<"enter pincode ";
	cin>>pincode;
	}
	void getac_no()
	{
	ac_no=count1;
	count1++;
	}
	void gettransaction_id()
	{
	transaction_id=count2;
	}
	void getphone_no()
	{
	cout<<"enter your phone no: ";
	cin>>phone_no;
	}
	void print()
	{
	cout<<ac_holder<<endl<<address<<" "<<pincode<<endl<<ac_no<<endl<<transaction_id;
	}
};
long int bank_entry::count1=10000;
long int bank_entry::count2=556562;

class bank_find:public bank_entry
{
protected:
long int ac_no1,ac_no2,transaction_id_find,amount;
public:
	void getac_no1()
	{
	cout<<"enter your a/c no: ";
	cin>>ac_no1;
	}
		void getac_no2()
	{
	cout<<"enter the acount to which money to be transfered ";
	cin>>ac_no2;
	}
		void gettransaction_id_find()
	{
	cout<<"enter your transacton id: ";
	cin>>transaction_id_find;
	}
	/*void newbalance(bank_entry &ob)
	{
		ob.balance+=amount;
	}
	int compare1()
	{
	int a=0;
	if(ac_no==ac_no1)
	{
		a=1;
		gettransaction_id_find();
		if(transaction_id==transaction_id_find)
		{
			getac_no2();
			if(amount<=balance)
			{
				newbalance(bank_entry &ob);
			}
		}
	}
	return a;
	} */
};
int main()
{
//bank_entry detail[100];
bank_find find;
int a,b,i;
//clrscr();
for(i=0;i<100;i++)
{
find.getac_holder();
find.getaddress();
find.getpincode();
find.getphone_no();
find.getac_no();
find.gettransaction_id();
find.print();
cout<<"press 1 to exit";
cin>>b;
switch(b)
{
	case 1:
	goto end;
	default:break;

}
}
end:
;
}


and here is a sample session:
enter your name A
enter your address s
enter pincode 345
enter your phone no: 678
A
s 345
10000
556562press 1 to exit1


i am unable to USE the various entries of account!!

In what way are you unable to USE them? What is supposed to happen that does not happen?
Last edited on
and where have you actually used friend function?
first how is it THE WORST CODE EVER???

if i store say 10 entries
and then want to access 5th entry using inheritance.
how do i tell the compiler to go to 5th entry as i am using "find" to call objects.
Last edited on
first how is it THE WORST CODE EVER???

WHERE DOES IT SAY THAT??? Or do you already know that, and you want us to tell you why it's so bad?

and then want to access 5th entry using inheritance.

That makes no sense at all.

You have created ONE object of type bank_find. Every time you store data in it, you are overwriting the previous data. The 5th entry has been overwritten by the 6th, which has been overwritten by the 7th, which has been overwritten by 8th and so on.

You are not ready for inheritance, or classes. You need to go back and understand variables first, so that you can understand that one variable can hold one value, and that if you put a different value into it, the first value is overwritten.
hey Moschops

here is the actual code that i wanted to run.(edited the original)
now the error says data members are protected but bank_find should inherit those??

your help is apreciated.


(and i am a beginner :))
here is the actual code that i wanted to run.(edited the original)


Please do not do that. It effectively alters the past and renders untrustworthy all following posts.

Firstly, you appear to be using a compiler from the dark ages of C++. Stop. Do NOT learn old, incorrect C++. There is very little point in that when you could be learning modern, correct C++. Are you using Dev-C++ (before version 5)? Please don't.

newbalance(bank_entry &ob);
Is this a function call? It is wrong. Presumably it should be
newbalance(ob);

void main()
Bad. Should be
int main() main returns an int, always always always. I see that at the end of your function, main does in fact return an int, so this is a real mess. Your compiler was probably a fine compiler when it was made 15 or more years ago. It is not so good now. Please get a recent one for free.

The following compiles without error on a recent compiler.


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
#include<iostream>

using namespace std;

#include<string.h>
class bank_entry
{
protected:
char ac_holder,address;
long int ac_no,transaction_id,pincode,phone_no,balance;
static long int count1,count2;
public:
	bank_entry()
	{
	balance=0;
	}
	void getac_holder()
	{
	cout<<"enter your name ";
	cin>>ac_holder;
	}
	void getaddress()
	{
	cout<<"enter your address ";
	cin>>address;
	}
	void getpincode()
	{
	cout<<"enter pincode ";
	cin>>pincode;
	}
	void getac_no()
	{
	ac_no=count1;
	count1++;
	}
	void gettransaction_id()
	{
	transaction_id=count2;
	}
	void getphone_no()
	{
	cout<<"enter your phone no: ";
	cin>>phone_no;
	}
	void print()
	{
	cout<<ac_holder<<endl<<address<<" "<<pincode<<endl<<ac_no<<endl<<transaction_id;
	}
};
long int bank_entry::count1=10000;
long int bank_entry::count2=556562;

class bank_find:public bank_entry
{
protected:
long int ac_no1,ac_no2,transaction_id_find,amount;
public:
	void getac_no1()
	{
	cout<<"enter your a/c no: ";
	cin>>ac_no1;
	}
		void getac_no2()
	{
	cout<<"enter the acount to which money to be transfered ";
	cin>>ac_no2;
	}
		void gettransaction_id_find()
	{
	cout<<"enter your transacton id: ";
	cin>>transaction_id_find;
	}
	void newbalance(bank_entry &ob)
	{
		ob.balance+=amount;
	}
	int compare1(bank_entry &ob)
	{
	int a=0;
	if(ob.ac_no==ac_no1)
	{
		a=1;
		gettransaction_id_find();
		if(ob.transaction_id==transaction_id_find)
		{
			getac_no2();
			if(amount<=ob.balance)
			{
				newbalance(ob);
			}
		}
	}
	return a;
	} 
};
int main()
{
bank_entry detail[100];
bank_find find;
int a,b,i,c;

for(i=0;i<100;i++)
{
detail[i].getac_holder();
detail[i].getaddress();
detail[i].getpincode();
detail[i].getphone_no();
detail[i].getac_no();
detail[i].gettransaction_id();
detail[i].print();
again1:
cout<<"\npress 1 to continue\npress 2 to exit";
cin>>b;
switch(b)
{
	case 1:
         break;
    case 2:
	     goto end;
	default:cout<<"invalid entry";goto again1;

}
}
find.getac_no1();
for(i=0;i<100;i++)
{
b=find.compare1(detail[i]);
if(b==1)
{
        c=i;
}
}
find.newbalance(detail[c]);
detail[c].print();
end:

return 0;
}

Last edited on
1. i am using dev c++ 5.2.0.3(is this ok ,would you plz sugest any other otherwise)

2. the code is still not working
it shows error that [Error] 'long int bank_entry::balance' is protected .......
devc++ is outdated and is no longer being worked on, you can use one of its forks like orwell's dev c++ or what I would recommend, Code::Blocks.
Last edited on
Try VC++
Topic archived. No new replies allowed.