Write and run a program that has a class called Student with a private string da
ID: 3686540 • Letter: W
Question
Write and run a program that has a class called Student with a private string data member called ID that provides a four-digit student ID number and three private double data members called gradel, grade2 and grade3. The class Mi must have a constructor that provides default values of "0000" for ID and 0.0 for gradel, grade2 and grade3. The constructor must allow initialization of all of the private data members. In addition to the constructor, a public member B function called grade avg() must provide the average of the three grades of the object. A member function called display student() must be provided that "cout's the ID, gradel, grade2, grade3 and grade avg() for the objectExplanation / Answer
#include <iostream>
#include<cstdlib>
#include<string>
using namespace std;
class Student
{
private:
string ID;
double grade1,grade2,grade3;
public:
Student(void)
{
ID="0000";
grade1=0.0;
grade2=0.0;
grade3=0.0;
}
Student(string s1,double g1,double g2,double g3)
{
ID = s1;
grade1=g1;
grade2=g2;
grade3=g3;
}
double grade_avg()
{
return (grade1+grade2+grade3)/3;
}
void display_student()
{
cout<<"The ID is "<<ID<<endl;
cout<<"The Grade in first subject is "<<grade1<<endl;
cout<<"The Grade in second subject is "<<grade2<<endl;
cout<<"The Grade in third subject is "<<grade3<<endl;
cout<<"The average grade is "<<grade_avg()<<endl;
}
};
int main() {
Student s1("1234",75,85,95.5);
s1.display_student();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.