In: Computer Science
What do I need to implement this code. I need an ADT //--------------------------------------------- // This would be the Student.h file //--------------------------------------------- #include <iostream> #include <cassert> using namespace std; // each student have a name, an ID (100000000~999999999), and three grades class Student { private: public: Student(); Student(); setName(); setId(); setGrade (); getName(); getId(); getGrade() ; printAll() ; }; //--------------------------------------------- // This would be the Student.cpp file //--------------------------------------------- //====================== YOUR CODE STARTS HERE ====================== Student::Student() //default constructor { } Student::Student(string aName, int anId, int Grade1, int Grade2, int Grade3) //other constructor { } Student::setName() { } Student::setId() { } Student::setGrade () { } Student::getName() { } Student::getId() { } // print all information of a student Student::printAll() { } Student::getGrade() { } //====================== YOUR CODE ENDS HERE ====================== //--------------------------------------------- // This would be the main.cpp file //--------------------------------------------- int main () { Student stu; //calls default constructor string inName; int inId; int i; cout << "********************************"<<endl; cout << "View stu after default constructor" << endl; stu.printAll(); cout << endl; //Use setters to reset the values cout << "Your name, please: "; getline(cin, inName, '\n'); // input a string stu.setName (inName); cout << "Your id, please: "; cin >> inId; //====================== YOUR CODE HERE ====================== // set stu's id and set three grades of students // // //====================== YOUR CODE ENDS HERE ====================== cout << endl; cout << "********************************"<<endl; cout << "View stu after setters" << endl; stu.printAll(); cout << endl; cout << "********************************"<<endl; cout << "Testing other constructor" << endl; Student tom ("Tom", 99, 55, 32, 68); //calls second constructor //notice using getters to print cout << "Hello, " << tom.getName() << endl; cout << "Your Student ID is " << tom.getId() << endl; cout << "Your Grades are: " << endl; for (int i=0; i < 3; i++) { cout << "Test " << i+1 << ": " << tom.getGrade(i) << " " << endl; } }
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
student.h
#include <iostream>
#include <cassert>
using namespace std;
// each student have a name, an ID (100000000~999999999), and
three grades
class Student
{
private:
string name;
int id;
int grade[3];
public:
Student();
Student(string , int , int , int , int );
void setName(string);
void setId(int);
void setGrade (int, int ,int);
string getName();
int getId();
int getGrade(int) ;
void printAll() ;
};
student.cpp
#include "student.h"
Student::Student() //default constructor
{
//initializing with null or empty values
name="";
id=0;
for(int i=0;i<3;i++)
grade[i]=0;
}
Student::Student(string aName, int anId, int Grade1, int Grade2,
int Grade3)
//other constructor
{
//set all the parameters
name = aName;
id = anId;
grade[0] = Grade1;
grade[1] = Grade2;
grade[2] = Grade3;
}
void Student::setName(string aName)
{
name = aName;
}
void Student::setId(int anId)
{
id = anId;
}
void Student::setGrade (int Grade1, int Grade2, int
Grade3)
{
grade[0] = Grade1;
grade[1] = Grade2;
grade[2] = Grade3;
}
string Student::getName()
{
return name;
}
int Student::getId()
{
return id;
}
// print all information of a student
void Student::printAll()
{
cout<<"Name: "<<name<<endl;
cout<<"Id: "<<id<<endl;
cout<<"Grades: ";
for(int i=0;i<3;i++)
cout<<grade[i]<<" ";
cout<<endl;
}
int Student::getGrade(int i)
{
return grade[i];
}
main.cpp
#include "student.cpp"
int main ()
{
Student stu; //calls default constructor
string inName;
int inId;
int i;
cout << "********************************"<<endl;
cout << "View stu after default constructor" <<
endl;
stu.printAll();
cout << endl;
//Use setters to reset the values
cout << "Your name, please: ";
getline(cin, inName, '\n'); // input a string
stu.setName (inName);
cout << "Your id, please: ";
cin >> inId;
stu.setId(inId);
int Grade1, Grade2, Grade3;
cout << "Your grades, please: ";
cin >> Grade1 >> Grade2 >> Grade3;
stu.setGrade(Grade1, Grade2, Grade3);
cout << endl;
cout << "********************************"<<endl;
cout << "View stu after setters" << endl;
stu.printAll();
cout << endl;
cout << "********************************"<<endl;
cout << "Testing other constructor" << endl;
Student tom ("Tom", 99, 55, 32, 68); //calls second
constructor
//notice using getters to print
cout << "Hello, " << tom.getName() << endl;
cout << "Your Student ID is " << tom.getId() <<
endl;
cout << "Your Grades are: " << endl;
for (int i=0; i < 3; i++)
{
cout << "Test " << i+1 << ": " <<
tom.getGrade(i) << " " << endl;
}
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.