-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_student.cpp
106 lines (85 loc) · 2.7 KB
/
04_student.cpp
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
// // Define a class representing a student with attributes like name and roll number. Dynamically create an array of student objects based on user input for the number of students. Allow the user to input details for each student, display their information, and then use delete to free the memory.
// // Header files
#include <iostream>
#include <string.h>
#include <stdlib.h>
// // use namespace
using namespace std;
#define MAX_STUDENTS 10
// // define class Student
class Student
{
public:
// // static member variable
static const int MAX_CHARS_NAME = 31;
private:
// // instance member variables
char name[MAX_CHARS_NAME];
int roll;
public:
// // instance member function to set roll
void setRoll(int r)
{
roll = r;
}
// // instance member function to set name
void setName(char *nm)
{
strcpy(name, nm);
}
// // instance member function to get roll
int getRoll()
{
return roll;
}
// // instance member function to get name
char *getName(char *nm)
{
strcpy(nm, name);
return nm;
}
};
// // Main Function Start
int main()
{
int numOfStudents;
cout << "\nHow Many Students Data You Want to Enter => ";
cin >> numOfStudents;
// // invalid input
if (numOfStudents < 1 || numOfStudents > MAX_STUDENTS)
{
cout << "\n!!! Invalid Input..." << endl;
exit(0);
}
Student *students = new Student[numOfStudents]; // dynamically create array of objects of Student
char name[Student::MAX_CHARS_NAME];
int roll;
// // Get Students Data
cout << "\n>>>>>>>>>> Enter Data of " << numOfStudents << " Students <<<<<<<<<<<<<\n";
for (int i = 0; i < numOfStudents; i++)
{
cout << "\n>>>>>>>>> Enter Data of Student-" << i + 1 << " <<<<<<<<<<<<<\n";
cout << "\nEnter Student's Roll Number => ";
cin >> roll;
cout << "\nEnter Student's Name (MAX_CHARACTERS " << Student::MAX_CHARS_NAME - 1 << ") => ";
cin.ignore();
cin.getline(name, Student::MAX_CHARS_NAME);
// // set students's data
students[i].setRoll(roll);
students[i].setName(name);
}
// // get and display Students data
cout << "\n>>>>>>>>>> Data of " << numOfStudents << " Students <<<<<<<<<<<<<\n";
for (int i = 0; i < numOfStudents; i++)
{
cout << "\n>>>>>>>>> Data of Student-" << i + 1 << " <<<<<<<<<<<<<\n";
cout << "\nStudent's Roll Number => " << students[i].getRoll();
cout << "\nStudent's Name => " << students[i].getName(name) << endl;
}
// // deallocate memory
delete[] students;
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End