Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this lab, you will create a class called Student . The Student class will hav

ID: 3535747 • Letter: I

Question

In this lab, you will create a class called Student. The Student class will have 4 private member variables. Note; there are 65 points possible in this lab. That means a 15 point bonus possible

Type

Variable name

Description

string

name

stores the name

int

id

stores the two digit id (in the range of 10-99)

int *

test

Points to a dynamically allocated array with num elements

int

num

stores the number of test scores

It will have the following member functions, all of them will be public except makeArray() which will be private.

Function Header (5 points for each function = 60 points)

Description

void makeArray()

It will dynamically allocate an int array with num elements, assigns the address of the array to test, and assigns 0 to all the elements

Student()

It should call setName and pass in “Noneâ€. It should call setID and pass in 10. It should set num to 3. It should call makeArray.

Student(int n)

It should call setName and pass in “Noneâ€. It should call setID and pass in 10. It should set num to n if n is > 0, else set it to 3. It should call makeArray.

Student(string nm,int i, int n)

It should call setName and pass in nm. It should call setID and pass in i. It should set num to n if n is > 0, else set it to 3. It should call makeArray.

void setName(string nm)

It should set name to nm.

Function Header (5 points for each function = 60 points)

Description

void setID(int i)

It should set id to i if i is in the range of 10 to 99. If it is not, it should set id to 10 and print an error message saying it cannot set the id to i. The error message should include the student’s name by displaying the return value of getName.

void setScore(int i, int s)

It should only set the score if the index i is a valid index within the bounds of the dynamic array holding the test scores, and if s is a valid score in the range of 0 -100. If it does not meet these conditions, an error message should display saying the test i cannot be set to s. The error message should include the student’s name by displaying the return value of getName.

string getName() const

It should return the name.

int getID() const

It should return the id.

void showScore()

It should loop through the dynamic array and cout the test number and the score.

void display()

It should call the getName, getID, and showScore functions to get and display the student’s information. See the output for the format of the cout statements.

~Student()

It should free the array that test is pointing to.

In the main (5 points), create 3 students. Student a should call the default constructor. Student b should call the constructor with 1 parameter and pass in 4. Student c should call the constructor with 3 parameters and pass in “Joeâ€, 40 and 5. Call the necessary set functions and finally call the display function for each of the student objects. Look at the output to figure out which set functions you need to call and what values you need to pass in.

Type

Variable name

Description

string

name

stores the name

int

id

stores the two digit id (in the range of 10-99)

int *

test

Points to a dynamically allocated array with num elements

int

num

stores the number of test scores

Explanation / Answer

class student

{

int rollno;

char name[50];

int p_marks, c_marks, m_marks, e_marks, cs_marks;

float per;

char grade;

void calculate(); //function to calculate grade

public:

void getdata(); //function to accept data from user

void showdata(); //function to show data on screen

void show_tabular();

int retrollno();

}; //class ends here



void student::calculate()

{

per=(p_marks+c_marks+m_marks+e_marks+cs_marks)/5.0;

if(per>=60)

grade='A';

else if(per>=50)

grade='B';

else if(per>=33)

grade='C';

else

grade='F';

}


void student::getdata()

{

cout<<" Enter The roll number of student ";

cin>>rollno;

cout<<" Enter The Name of student ";

gets(name);

cout<<" Enter The marks in physics out of 100 : ";

cin>>p_marks;

cout<<" Enter The marks in chemistry out of 100 : ";

cin>>c_marks;

cout<<" Enter The marks in maths out of 100 : ";

cin>>m_marks;

cout<<" Enter The marks in english out of 100 : ";

cin>>e_marks;

cout<<" Enter The marks in computer science out of 100 : ";

cin>>cs_marks;

calculate();

}


void student::showdata()

{

cout<<" Roll number of student : "<<rollno;

cout<<" Name of student : "<<name;

cout<<" Marks in Physics : "<<p_marks;

cout<<" Marks in Chemistry : "<<c_marks;

cout<<" Marks in Maths : "<<m_marks;

cout<<" Marks in English : "<<e_marks;

cout<<" Marks in Computer Science :"<<cs_marks;

cout<<" Percentage of student is :"<<per;

cout<<" Grade of student is :"<<grade;

}


void student::show_tabular()

{

cout<<rollno<<setw(6)<<" "<<name<<setw(10)<<p_marks<<setw(4)<<c_marks<<setw(4)<<m_marks<<setw(4)

<<e_marks<<setw(4)<<cs_marks<<setw(6)<<per<<setw(6)<<" "<<grade<<endl;

}


int student::retrollno()

{

return rollno;

}



//***************************************************************

// function declaration

//****************************************************************



void write_student(); //write the record in binary file

void display_all(); //read all records from binary file

void display_sp(int); //accept rollno and read record from binary file

void modify_student(int); //accept rollno and update record of binary file

void delete_student(int); //accept rollno and delete selected records from binary file

void class_result(); //display all records in tabular format from binary file

void result(); //display result menu

void intro(); //display welcome screen

void entry_menu(); //display entry menu on screen



//***************************************************************

// THE MAIN FUNCTION OF PROGRAM

//****************************************************************




int main()

{

char ch;

cout.setf(ios::fixed|ios::showpoint);

cout<<setprecision(2); // program outputs decimal number to two decimal places

clrscr();

intro();

do

{

clrscr();

cout<<" MAIN MENU";

cout<<" 01. RESULT MENU";

cout<<" 02. ENTRY/EDIT MENU";

cout<<" 03. EXIT";

cout<<" Please Select Your Option (1-3) ";

cin>>ch;

clrscr();

switch(ch)

{

case '1': result();

break;

case '2': entry_menu();

break;

case '3':

break;

default :cout<<"";

}

}while(ch!='3');

return 0;

}


//***************************************************************

// function to write in file

//****************************************************************


void write_student()

{

student st;

ofstream outFile;

outFile.open("student.dat",ios::binary|ios::app);

st.getdata();

outFile.write((char *) &st, sizeof(student));

outFile.close();

cout<<" Student record Has Been Created ";

cin.ignore();

getch();

}


//***************************************************************

// function to read all records from file

//****************************************************************


void display_all()

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

getch();

return;

}

cout<<" DISPLAY ALL RECORD !!! ";

while(inFile.read((char *) &st, sizeof(student)))

{

st.showdata();

cout<<" ==================================== ";

}

inFile.close();

getch();

}


//***************************************************************

// function to read specific record from file

//****************************************************************


void display_sp(int n)

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

getch();

return;

}


int flag=0;

while(inFile.read((char *) &st, sizeof(student)))

{

if(st.retrollno()==n)

{

st.showdata();

flag=1;

}

}

inFile.close();

if(flag==0)

cout<<" record not exist";

getch();

}


//***************************************************************

// function to modify record of file

//****************************************************************


void modify_student(int n)

{

int found=0;

student st;

fstream File;

File.open("student.dat",ios::binary|ios::in|ios::out);

if(!File)

{

cout<<"File could not be open !! Press any Key...";

getch();

return;

}

while(File.read((char *) &st, sizeof(student)) && found==0)

{

if(st.retrollno()==n)

{

st.showdata();

cout<<" Please Enter The New Details of student"<<endl;

st.getdata();

int pos=(-1)*sizeof(st);

File.seekp(pos,ios::cur);

File.write((char *) &st, sizeof(student));

cout<<" Record Updated";

found=1;

}

}

File.close();

if(found==0)

cout<<" Record Not Found ";

getch();

}


//***************************************************************

// function to delete record of file

//****************************************************************


void delete_student(int n)

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

getch();

return;

}

ofstream outFile;

outFile.open("Temp.dat",ios::out);

inFile.seekg(0,ios::beg);

while(inFile.read((char *) &st, sizeof(student)))

{

if(st.retrollno()!=n)

{

outFile.write((char *) &st, sizeof(student));

}

}

outFile.close();

inFile.close();

remove("student.dat");

rename("Temp.dat","student.dat");

cout<<" Record Deleted ..";

getch();

}


//***************************************************************

// function to display all students grade report

//****************************************************************


void class_result()

{

student st;

ifstream inFile;

inFile.open("student.dat",ios::binary);

if(!inFile)

{

cout<<"File could not be open !! Press any Key...";

getch();

return;

}

cout<<" ALL STUDENTS RESULT ";

cout<<"========================================================== ";

cout<<"R.No Name P C M E CS %age Grade"<<endl;

cout<<"========================================================== ";

while(inFile.read((char *) &st, sizeof(student)))

{

st.show_tabular();

}

getch();

inFile.close();

}


//***************************************************************

// function to display result menu

//****************************************************************


void result()

{

char ch;

int rno;

cout<<" RESULT MENU";

cout<<" 1. Class Result";

cout<<" 2. Student Report Card";

cout<<" 3. Back to Main Menu";

cout<<" Enter Choice (1/2/3)? ";

cin>>ch;

clrscr();

switch(ch)

{

case '1' :class_result(); break;

case '2' :cout<<" Enter Roll Number Of Student : ";

cin>>rno;

display_sp(rno); break;

case '3' :break;

default :cout<<"";

}

}


//***************************************************************

// INTRODUCTION FUNCTION

//****************************************************************


void intro()

{

cout<<" STUDENT";

cout<<" REPORT CARD";

cout<<" PROJECT";

cout<<" MADE BY : SULABH AGRAWAL";

cout<<" SCHOOL : CAMBRIDGE SCHOOL";

getch();

}


//***************************************************************

// ENTRY / EDIT MENU FUNCTION

//****************************************************************


void entry_menu()

{

char ch;

int num;

clrscr();

cout<<" ENTRY MENU";

cout<<" 1.CREATE STUDENT RECORD";

cout<<" 2.DISPLAY ALL STUDENTS RECORDS";

cout<<" 3.SEARCH STUDENT RECORD ";

cout<<" 4.MODIFY STUDENT RECORD";

cout<<" 5.DELETE STUDENT RECORD";

cout<<" 6.BACK TO MAIN MENU";

cout<<" Please Enter Your Choice (1-6) ";

cin>>ch;

clrscr();

switch(ch)

{

case '1': write_student(); break;

case '2': display_all(); break;

case '3': cout<<" Please Enter The roll number "; cin>>num;

display_sp(num); break;

case '4': cout<<" Please Enter The roll number "; cin>>num;

modify_student(num);break;

case '5': cout<<" Please Enter The roll number "; cin>>num;

delete_student(num);break;

case '6': break;

default: cout<<""; entry_menu();

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote