In this lab, you will create a class called Student. The Student class will have
ID: 3834123 • Letter: I
Question
In this lab, you will create a class called Student. The Student class will have 4 private member variables.
Type
Variable name
Description
string
name
stores the name
int
id
stores the two digit id (in the range of 10-99)
int *
testptr
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
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.
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:
1. 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.
2. 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 *
testptr
Points to a dynamically allocated array with num elements
int
num
stores the number of test scores
Calling the set functions Invalid Can not set id to 200 for Tom Invalid Can not set test 5 to 95 for Joe Invalid Can not set test 4 to 105 for Joe Invalid Can not set test 5 to 105 for Joe Calling the display function The Nane Tom The ID 20 Test 0 had a score of 75 Test 1 had a score of 85 Test 2 had a score of 95 The Nane John The ID: 30 Test 0 had a score of 70 Test 1 had a score of 80 Test 2 had a score of 90 Test 3 had a score of 100 The Nane Joe The ID 40 Test 0 had a score of 90 Test 1 had a score of 91 Test 2 had a score of 92 Test 3 had a score of 93 Test 4 had a score of 94 Press any key to continueExplanation / Answer
//Program
#include <iostream>
using namespace std;
class Student
{
//The Student class will have 4 private member variables.
string name;//stores the name
int id; //stores the two digit id (in the range of 10-99)
int * testptr;//Points to a dynamically allocated array with num elements
int num; //stores the number of test scores
/*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*/
void makeArray()
{
testptr=new int[num] ;
for(int i=0;i<num;i++)
*(testptr)=0;
}
public:
/*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()
{
setName("None");
setID(10);
setNum(3);
makeArray();
}
/*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(int n)
{
setName("None");
setID(10);
if(n<=0)
n=3;
setNum(n);
makeArray();
}
/*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.*/
Student(string nm,int i, int n)
{
setName(nm);
setID(i);
if(n<=0)
n=3;
setNum(n);
makeArray();
}
//it should set num to i
void setNum(int i)
{
num=i;
}
//It should set name to nm.
void setName(string nm)
{
name=nm;
}
/*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 setID(int i)
{
if(i>=10 && i<=99)
{
id=i;
}
else
{
id=10;
cout<<endl<<"Invalid. Cannot set id to "<<i<<" for "<<getName();
}
}
/*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.*/
void setScore(int i, int s)
{
if(i>=0 && i<num)
{
if(s>=0 && s<=100)
{
*(testptr+i)=s;
}
}
else
{
cout<<endl<<"Invalid. Cannot set test "<<i<<" to "<<s<<" for "<<getName();
}
}
//It should return the name.
string getName() const
{
return name;
}
//It should return the id.
int getID() const
{
return id;
}
//It should loop through the dynamic array and cout the test number and the score.
void showScore()
{
for(int i=0;i<num;i++)
{
cout<<endl<<"Test "<<i<<" had a score of "<<*(testptr+i);
}
}
/*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.*/
void display()
{
cout<<endl<<"Calling the display function";
cout<<endl<<"The Name: "<<getName();
cout<<endl<<"The ID: "<<getID();
showScore();
}
//It should free the array that test is pointing to.
~Student()
{
delete []testptr;
}
} ;
int main()
{
/*Create 3 students.*/
//Student a should call the default constructor.
Student a;
//Student b should call the constructor with 1 parameter and pass in 4.
Student b(4);
//Student c should call the constructor with 3 parameters and pass in “Joe”, 40 and 5.
Student c("Joe",40,5);
//Call the necessary set functions
a.setName("Tom");
a.setID(200);
a.setID(20);
a.setScore(0,75);
a.setScore(1,85);
a.setScore(2,95);
b.setName("Jhon");
b.setID(30);
b.setScore(0,70);
b.setScore(1,80);
b.setScore(2,90);
b.setScore(3,100);
c.setScore(0,90);
c.setScore(1,91);
c.setScore(2,92);
c.setScore(3,93);
c.setScore(4,94);
c.setScore(5,95);
c.setScore(4,105);
c.setScore(-5,-105);
/*finally call the display function for each of the student objects.*/
a.display();
b.display();
c.display();
return 0;
}
//output refer question
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.