How can i incorporate polymorphism into this program without changing it too much?

Does my program employ any operator or function overloading? If not, is there any way i can incorporate it without changing it too much?

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
#include <iostream>
using namespace std;
#include <fstream>
#include <string>

class user {
	public:
		char choice1;		
		string newpass;
		fstream usernames;
		fstream pswd;
		fstream npswd;
		fstream rnusernames;
		fstream rnpass;
		fstream rmaide;
};

class login : public user {
	private:
	string temp;
	string temp1;
	string temp2;
	string username;
	string userpass;
	string newuserpass;
	string loginyn;
	string mynewpass;

	public:
		int checkusername(){
		usernames.open("names.txt");
		cout << "\nplease enter your username   ";
		cin >> username; 
		while ((usernames >> temp)) { 
			if (temp.compare(username) == 0) {
				//cout << "found";
				usernames.close();
				return 1;
			}
		}
		usernames.close();
		return 0;
		}
		int checkpassword(){
		cout<<"please enter your password   ";
		cin>>userpass;
		pswd.open("pass.txt");
			while (pswd >> temp1) { 
				if (temp1.compare(userpass) == 0){
					cout<<"\n[ "<<username<<" ] having password [ "<<userpass<<" ] has been found in the database.\n\n";
					pswd.close();
					return 1;
				}
			}
		pswd.close();
		return 0;
		}
		int checknewpassword(){
		cout<<"please enter your password   ";
		cin>>newuserpass;
		npswd.open("newpass.txt");
			while (npswd >> temp2) { 
				if (temp2.compare(newuserpass) == 0){
					cout<<"Congratulations!!! You have successfully logged in with your new password.";
					npswd.close();
					return 1;
				}
				else{
					cout<<"ACCESS DENIED";
				}
			}
		npswd.close();
		return 0;
		}
		int creatnewpass(){
		cout<<"Password: ";
		cin>>mynewpass;
		npswd.open("newpass.txt");
		npswd<<mynewpass;
		npswd.close();
			cout<<"\nLogin now? (y/n) ";
		cin>>loginyn;
			if(loginyn=="y"){
				cout<<"\n        [LOGIN PROCESS STARTING...]\n";
				checkusername();
				checknewpassword();
			}
		return 0;
		}
		private:
};

class registration :  public user {
	private:
	string nusername;
	string rpass;
	string maiden;

	public:
		void askusername(){
			cout<<"\nUsername: ";
			cin>>nusername;
			rnusernames.open("newnames.txt");
			rnusernames<<nusername;
			rnusernames.close();
		}

		void askuserpass(){
			cout<<"Password: ";
			cin>>rpass;
			rnpass.open("newpass.txt");
			rnpass<<rpass;
			rnpass.close();
		}

		void askMMN(){
			cout<<"Mother's maiden name: ";
			cin>>maiden;
			rmaide.open("maidennames.txt");
			rmaide<<maiden;
			rmaide.close();
		}
};

int main() {
registration r;
user u;   
login verify; 
int attempts, success;
cout<<"\nAre you a new user? (y/n) ";
cin>>u.choice1;
	if (u.choice1 == 'n'){
		for (attempts = 1; attempts < 4; attempts++){
			success = verify.checkusername();
				if (success == 1) { 
					break; 
				}
			}
		if (success == 0){
			cout<<"\nACCESS DENIED\n";
			cin.ignore();
			abort();
		}
		verify.checkpassword();
		cout<<"would you like to create a new password? (y/n) ";
		cin>>u.newpass;
			if (u.newpass=="y"){
				verify.creatnewpass();
			}
			else if (u.newpass=="n"){
				cin.ignore();
				abort();
			}
	}
	else if (u.choice1 == 'y'){
		r.askusername();
		r.askuserpass();
		r.askMMN();
		cin.ignore();
		abort();
	}
	else {
		cout<<"Error";
	}
return 0;
}
Last edited on
The first thing I notice is your concept of inheritance itself is wrong.

The way you have it set up is that login is-a user, and registration is-a login, and thus registration is a user (?) That doesn't make any sense. There is no real need for inheritance or polymorphism in this hierarchy at all, and most of the class methods are extremely repetitive. You could have just made a single user class with the methods register() and login(). Everything else could be abstracted.

There is no need to make things complicated.
i know but that's how the assignment is laid out. I do see your point and will change them to old user and new user. i know doing all this stuff isn't necessary but it's the objective of the assignment.
Did the assignment tell you to make a class called "user" that is the base class of "registration" and 'login"?
Topic archived. No new replies allowed.