Classes
#include <iostream>
#include <string>
using namespace std;
class student {
string name;
const string cnic;
const char gender;
float cgpa;
public:
student() : name(" "), cnic(" "), gender(' '), cgpa(0.0) {}
student(string cnic, char g) : cnic(cnic), gender(g) {}
string getname() const { return name; }
string getcnic() const { return cnic; }
char getgender() const { return gender; }
float getcgpa() const { return cgpa; }
void setname(const string& newname) { name = newname; }
void setcgpa(float newcgpa) { cgpa = newcgpa; }
};
class section {
student s[2];
string sectionname;
string teacher;
public:
section(): s() {}
void editsection();
void addstudent();
void updatestudent();
void printlist();
void printliststudents();
};
Main function
int main() {
section* ptr;
int size;
cout << "enter number of sections: ";
cin >> size;
cout << endl;
ptr = new section[size];
int choice = 0;
cout << "1. edit section attributes\n2. add student in a section\n3. update student of a section\n4. print list of students of a section\n5. print list of sections\n6. exit the program\n";
while (choice != 6) {
cout << "\nenter your choice: ";
cin >> choice;
if (choice == 1) {
for (int i = 0; i < size; i++) {
cout << "section: " << i + 1 << endl;
ptr[i].editsection();
cout << endl;
}
}
else if (choice == 2) {
char section_choice;
cout << "in which section do you want to add a student: ";
cin >> section_choice;
for (int i = 0; i < size; i++) {
if (section_choice) {
cout << "section: " << i + 1 << endl;
ptr[i].addstudent();
cout << endl;
}
}
}
else if (choice == 3) {
for (int i = 0; i < size; i++) {
cout << "section: " << i + 1 << endl;
ptr[i].updatestudent();
cout << endl;
}
}
else if (choice == 4) {
for (int i = 0; i < size; i++) {
cout << "section: " << i + 1 << endl;
ptr[i].printlist();
cout << endl;
}
}
else if (choice == 5) {
for (int i = 0; i < size; i++) {
cout << "section: " << i + 1 << endl;
ptr[i].printliststudents();
cout << endl;
}
}
else if (choice == 6) {
cout << "exiting the program!\n";
}
else {
cout << "incorrect choice!\nenter again: ";
cin >> choice;
}
}
delete[] ptr;
ptr = nullptr;
return 0;
}
void section::editsection() {
cout << "enter new teacher name: ";
cin.ignore();
getline(cin, teacher);
cout << "enter new section name: ";
getline(cin, sectionname);
cout << endl;
};
void section::addstudent() {
string name, cnic;
char gender;
float cgpa;
int emptyslotindex = -1;
for (int i = 0; i < 2; ++i) {
if (s[i].getname() == " ") {
emptyslotindex = i;
break;
}
}
if (emptyslotindex != -1) {
cout << "enter student's name: ";
cin >> name;
cout << "enter student's cnic: ";
cin >> cnic;
cout << "enter student's gender: ";
cin >> gender;
cout << "enter student's cgpa: ";
cin >> cgpa;
s[emptyslotindex] = student(cnic, gender); //error
s[emptyslotindex].setname(name);
s[emptyslotindex].setcgpa(cgpa);
}
else {
cout << "section is full. cannot add more students." << endl;
}
}
void section::updatestudent() {
string S;
cout << "enter which student do you want to update: ";
cin.ignore();
getline(cin, S);
for (int i = 0; i < 2; i++) {
if (s[i].getname() == S) {
string studentname, cnic;
float gpa;
char gender;
cout << "enter student updated name: ";
getline(cin, studentname);
s[i].setname(studentname);
cout << "enter updated cgpa: ";
cin >> gpa;
s[i].setcgpa(gpa);
cout << "enter student's cnic: ";
cin.ignore();
getline(cin, cnic);
cout << "enter student's gender: ";
cin >> gender;
cin.ignore();
/*s[i] = student(cnic, gender);*/ //error
break;
}
else {
cout << "student not found!\n";
}
}
};
void section::printlist() {
cout << "teacher name: " << teacher << endl;
cout << "section name: " << sectionname << endl;
};
void section::printliststudents() {
for (int i = 0; i < 2; i++) {
cout << "student name: " << s[i].getname() << endl;
cout << "student cnic: " << s[i].getcnic() << endl;
cout << "student gender: " << s[i].getgender() << endl;
cout << "student cgpa: " << s[i].getcgpa() << endl;
cout << endl;
}
};
Constant members initilizing
-i am trying to assign the cnic and gender enterd by user to the secion class array.
-i can do it only by initilizer list as they are both constant members
-how can i solve this issue
Problem Description:
-In my class student, I have two constant member variables: CNIC (National Identity Card number) and gender. These variables are intended to remain constant throughout the lifetime of a student object. However, I need to initialize them with values provided by the user during runtime. Since they are constant, I cannot directly assign values to them within the constructor's body.
-My task is to find a solution to initialize these constant members with user-provided values. I'm seeking advice on how to handle this situation effectively while adhering to good design principles. My goal is to ensure that CNIC and gender remain constant after object creation, while still allowing them to be initialized with user input.
-I'm looking for alternative approaches or design modifications that would enable me to achieve this initialization without violating the constancy of these member variables. My question aims to gather insights on best practices or alternative designs to solve this issue.