These questions should be written in C++ and should compile. A) Write the class
ID: 3572627 • Letter: T
Question
These questions should be written in C++ and should compile.
A) Write the class definition for the ADT Student. The class will contain three private member variables, a cstring statically sized 32 called name, an integer pointer for tests, and an integer for testCount. The class definition will also contain prototypes for a default constructor, a copy constructor, an overloaded assignment operator, a destructor, a void function called inputStudent, and a void function called displayStudent.
B) Write the definitions for the Student class. The default constructor will assign NULL to the first character of name, dynamically allocate one integer for tests then assign it 0, and assign 1 to testCount. The copy constructor will copy the passed Student object, and the overloaded assignment operator will assign the passed Student object into this Student object. The destructor will deallocate any and all memory the dynamic member variable has allocated. The function inputStudent will prompt the user for a name, assign the entered value into name, prompt for the amount of test scores to enter, assign the entered value into testCount, allocate, and assign a value for each tests. Remember to handle the dynamic member variable accordingly. The function displayStudent will output the student's name, each tests score, and the average of the test scores.
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
class Student {
private :
char name[32];
int* tests = NULL;
int testCount;
public :
Student() {
name[0] = '';
tests = new int;
*tests = 0;
testCount = 1;
}
//copy constructor
Student(Student& s) {
strcpy(name,s.name);
if (tests == NULL)
tests = new int[s.testCount];
for(int i = 0; i < testCount; i++)
*(tests+i) = *(s.tests+i);
testCount = s.testCount;
}
//overloaded = operator
void operator = (Student& s) {
strcpy(name,s.name);
if (tests == NULL) //memory should be previously allocated by default constructor
tests = new int[s.testCount];//then remove the previous allocation and allocate new size
else {
delete[] tests;
tests = new int[s.testCount];
}
for(int i = 0; i < testCount; i++)
*(tests+i) = *(s.tests+i);
testCount = s.testCount;
}
~Student() {
if(tests != NULL)
delete[] tests;
}
void inputStudent() {
cout << " Enter name : "; cin >> name;
cout << " Number of test scores : "; cin >> testCount;
if(tests!=NULL)
delete[] tests;
tests = new int[testCount];
for(int i = 0; i < testCount; i++) {
cout << " Enter score for " << (i+1) << " test : ";
cin >> *(tests+i);
}
}
void displayStudent() {
int sum = 0;
cout << " Student's name : " << name;
cout << " Number of test scores : " << testCount;
for(int i = 0; i < testCount; i++) {
cout << " Score for " << (i+1) << "test is : " << *(tests+i);
sum = sum + *(tests+i);
}
out << " Average score is : " << (sum/testCount);
}
};
int main() {
Student s1, s2, s3;
s1.inputStudent();
s2.inputStudent();
s3 = s1; //assignment operator
Student s4(s2); //copy constructor
s1.displayStudent();
s2.displayStudent();
s3.displayStudent();
s4.displayStudent();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.