Question

In: Computer Science

What do I need to implement this code. I need an ADT //--------------------------------------------- // This would...

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;
   }
}

Solutions

Expert Solution

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.


Related Solutions

Where would I need to go and what would I need to do in order to...
Where would I need to go and what would I need to do in order to obtain the trademark? (please list all of the steps for this process from beginning to end)
In C++, Design and implement an ADT that represents a triangle. The data for the ADT...
In C++, Design and implement an ADT that represents a triangle. The data for the ADT should include the three sides of the triangle but could also include the triangle’s three angles. This data should be in the private section of the class that implements the ADT. Include at least two initialization operations: one that provides default values for the ADT’s data, and another that sets this data to client-supplied values. These operations are the class’s constructors. The ADT also...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using...
1. Implement the graph ADT using the adjacency list structure. 2. Implement the graph ADT using the adjacency matrix structure. LANGUAGE IS IN JAVA Comment for any questions Data structures and algorithms
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work...
Hi there, I need mpx2100ap arduino code do I need an amplifier to make this work ?
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For...
I haves code on bottom. what do i need to edit? Create a subdirectory called proj1.  For this project you need to create at least two files: proj1.cpp, and makefile. Both files should be placed in the proj1 directory. The file proj1.cpp should contain the main function, int main(). In the main() function, the program should read the input until it reaches the end, counting the number of times each word, number, and character is used. A word is defined as a sequence of letters ('a'..'z' or 'A'..'Z')....
What is Apple Inc. NAICS code and why would they need the code.
What is Apple Inc. NAICS code and why would they need the code.
Q5) i) What would a central bank need to do to reverse the effects of a...
Q5) i) What would a central bank need to do to reverse the effects of a favorable supply shock on inflation? What would its reaction do to the unemployment rate in the short run? ii) 5. Suppose speculators lost confidence in foreign economies and bought more U.S. bonds. How would this affect net exports in the U.S., and which way would this cause the aggregate demand curve to shift? Q6) Describe the proc
In C++, Implement the queue ADT with a singly linked list
In C++, Implement the queue ADT with a singly linked list
C++ good documentation as well as explanations would be great. I need the code to be...
C++ good documentation as well as explanations would be great. I need the code to be written without (#include if possible. Please don't just copy and paste someone else's answer. In this HW assignment we will simulate a car wash and calculate the arrival time, departure time and wait time for each car that came in for a car wash. We will use the following assumptions about our car wash: Each car takes 3 minutes from start of the car...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS:...
Implement a Bag ADT using Dynamic Array structure as underlying data storage for Bag ADT. RESTRICTIONS: Not allowed to use ANY built-in Python data structures and their methods. You must solve by importing the DynamicArray class and using class methods to write solution. Also not allowed to directly access any variables of the DynamicArray class (like self.size, self.capacity and self.data in part 1). All work must be done by only using class methods. Below is the Bag ADT starter code...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT