Repository navigation
Expand file tree
/
Copy patha2.cpp
More file actions
192 lines (155 loc) · 5.21 KB
/
Copy patha2.cpp
File metadata and controls
192 lines (155 loc) · 5.21 KB
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
/* Develop a program in C++ to create a database of student's information system containing the following
information: Name, Roll number, Class, Division, Date of Birth, Blood group, Contact address, Telephone number,
Driving license no. and other.
Construct the database with suitable member functions. Make use of constructor, default constructor,
copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic
memory allocation operators-new and delete as well as exception handling. */
#include <iostream>
using namespace std;
class Student {
string name, cls, dob, *bg, add;
int rollno;
char *div;
long *tel, *driv;
static int count;
public:
// Default constructor: initializes variables with default values
Student() {
name = " ";
rollno = 0;
cls = " ";
div = new char; // "new" allocates memory dynamically
dob = "dd/mm/yyyy";
bg = new string[3];
}
// Destructor: releases dynamically allocated memory
~Student() {
delete div;
delete bg;
}
// Copy constructor: creates copy of another Student object
Student(Student &o1) {
/* Value of member variable(eg: name) of current object('this') is assigned to same member
variable(name) of object o1. */
this->name = o1.name;
this->rollno = o1.rollno;
this->cls = o1.cls;
this->div = o1.div;
this->dob = o1.dob;
this->bg = o1.bg;
}
inline static void getcount() {
count++;
}
inline static int showcount() {
return count;
}
void getdata();
void display();
friend class Info;
};
int Student::count = 0;
inline void Student::getdata() {
cout << "\nEnter name: ";
cin >> name;
cout << "Enter class: ";
cin >> cls;
cout << "Enter division: ";
cin >> *div;
cout << "Enter roll number: ";
cin >> rollno;
cout << "Enter date of birth: ";
cin >> dob;
cout << "Enter blood group: ";
cin >> *bg;
}
inline void Student::display() {
cout << "\nName: " << name << endl;
cout << "Class: " << cls << endl;
cout << "Div: " << *div << endl;
cout << "Roll number: " << rollno << endl;
cout << "Date of birth: " << dob << endl;
cout << "Blood group: " << *bg << endl;
}
// class Info is a friend class of Student, and is allowed access to private members of class Student
class Info : public Student {
public:
// Default constructor
Info() {
add = " ";
tel = new long;
driv = new long;
}
// Destructor
~Info() {
delete tel;
delete driv;
}
// Copy constructor
Info(Info &o2) {
this->add = o2.add;
this->tel = o2.tel;
this->driv = o2.driv;
}
void getinfo();
void display();
};
/* The 'inline' keyword means that the function code can be substituted directly at the call site instead
of making a function call. This can potentially lead to more efficient code in terms of execution speed. */
inline void Info::getinfo() {
cout << "Enter address: ";
cin >> add;
cout << "Enter driving licence number: ";
cin >> *driv;
int n, total, flag = 0;
while (flag == 0) {
cout << "Enter telephone number: ";
cin >> *tel;
string telstring = to_string(*tel);
total = telstring.length();
n = 0;
for(int i = 1; i <= total; i++) {
n++;
}
cout << "Number of digits in the telephone number: " << n << endl;
try {
if (n == 10) {
cout << "Number entered is valid." << endl;
flag = 1;
}
else {
throw(*tel);
}
}
catch(...) {
cout << "Number entered is invalid." << endl;
*tel = 0;
}
}
}
inline void Info::display() {
cout << "Address: " << add << endl;
cout << "Telephone number: " << *tel << endl;
cout << "Driving licence number: " << *driv << endl;
}
int main() {
Student *o1[20]; // Array of pointers to student objects
Info *o2[20]; // Array of pointers to info objects
int m = 1;
string ans;
do {
cout << "\nStudent " << m << ": " << endl;
o1[m] = new Student; // To create a new student object and store its address in the array
o2[m] = new Info; // To create a new info object and store its address in the array
o1[m]->getdata(); // Calls the getdata() function for the student object
o2[m]->getinfo(); // Calls the getinfo() function for the info object
o1[m]->getcount(); // Increments the count of student objects
o1[m]->showcount();
o1[m]->display(); // Displays information for the student object
o2[m]->display(); // Displays information for the info object
m++; // To increment count of students
cout << "\nDo you want to add more data? (enter yes/no): ";
cin >> ans;
}
while (ans == "yes");
}