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
|
#include <iostream>
#include <cstring>
using namespace std;
class Student {
char name[20] {};
int stno {};
int ssn {};
double gpa {};
public:
Course(const Course&) = delete; // NOT YET IMPLEMENTED
Course& operator=(const Course&) = delete; // NOT YET IMPLEMENTED
Student() {}
Student(const char* nam, int stn, int s, double g) : stno(stn), ssn(s), gpa(g) {
strcpy(name, nam);
}
void setName(const char* n) { strcpy(name, n); }
const char* getName() const { return name; }
void setStno(int i) { stno = i; }
int getStno() const { return stno; }
void setSsn(int s) { ssn = s; }
int getSsn() const { return ssn; }
void setGPA(double g) { gpa = g; }
double getGPA() const { return gpa; }
bool IsEqual(const Student& student1) {
return (student1.gpa == gpa) && strcmp(student1.name, name) == 0;
}
};
class Course {
char cname[20] {};
int cno {};
int CH {};
Student *s {};
int n {};
public:
~Course() { delete[] s; }
Course(const char* cnam, int cn, int CH1, int n1) : cno(cn), CH(CH1), n(n1) , s(new Student[n1]) {
strcpy(cname, cnam);
char sname[20] {};
int stno {};
int ssn {};
double gpa {};
for (int i = 0; i < n; ++i) {
cout << "Enter values for student #" << i + 1 << " Please enter name (no space/tab) stno ssn gpa: ";
cin >> sname >> stno >> ssn >> gpa;
s[i].setName(sname);
s[i].setStno(stno);
s[i].setSsn(ssn);
s[i].setGPA(gpa);
}
}
void setName(const char* na) { strcpy(cname, na); }
const char* getName() const {return cname; }
void setCno(int c) { cno = c; }
int getCno() const { return cno; }
void setCH(int d) { CH = d; }
int getCH() const { return CH; }
/*
// NO. s is a pointer to array, not single element
const Student* getStudent() const { return s; }
void setStudent(const char sname[], int sno, int ssn, double gpa) {
delete s;
s = new Student(sname, sno, ssn, gpa);
}
*/
// CALLER HAS TO DELETE RETURNED ALLOCATED MEMORY
Student* getStudentByName(const char* n1) {
int c {};
for (int i = 0; i < n; ++i)
if (strcmp(s[i].getName(), n1) == 0)
++c;
auto temp {new Student[c + 1]};
int j {};
for (int i = 0; i < n; ++i)
if (strcmp(s[i].getName(), n1) == 0)
temp[j++] = s[i];
temp[j] = *new Student("noname", 0, 0, -1);
return temp;
}
// CALLER HAS TO DELETE RETURNED ALLOCATED MEMORY
Student* getStudentByGPA(double g) {
int c {};
for (int i = 0; i < n; ++i)
if (s[i].getGPA() >= g)
c++;
auto temp {new Student[c + 1]};
int j {};
for (int i = 0; i < n; ++i)
if (s[i].getGPA() >= g)
temp[j++] = s[i];
temp[j] = *new Student("noname", 0, 0, -1);
return temp;
}
};
void display(const Student* st)
{
for (; strcmp(st->getName(), "noname") != 0; ++st)
cout << st->getName() << ' ' << st->getStno() << ' ' << st->getSsn() << ' ' << st->getGPA() << ' ' << '\n';
}
int main()
{
Course c1 {Course("CSE", 1, 3, 3)};
auto bynam {c1.getStudentByName("ahmad")};
auto bygpa {c1.getStudentByGPA(3.1)};
cout << "\nBy name\n";
display(bynam);
cout << "\nBy gpa\n";
display(bygpa);
delete[] bynam;
delete[] bygpa;
}
| |