Answer to Question #338190 in C++ for Rey

Question #338190

Using class name studgrade with public data members and functions, create a program that will allow user to enter students A's grades in her five(5) major subjects, compute and display for the average grade of the student.

1
Expert's answer
2022-05-06T18:34:38-0400
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

class studgrade {
public:
    //Parameterized constructor
   studgrade(string name) {
        sname = name;
    }
    
   void calculate(){
          total = grade1 + grade2 + grade3 + grade4 + grade5;
        CGPA = total/5;
          }
    void printData();
    void inputData();
  //   destructor.  
    ~studgrade() {};
private:
    string sname;
    float grade1;
    float grade2;
    float grade3;
    float grade4;
    float grade5;    
    float total;
    float CGPA;
};

void studgrade::inputData() {
   cout << "Student name:   " << sname << endl;
   cout << "==============================="<<endl;
   cout << "grade_1: ";
   cin>>grade1 ;
   cout << "grade_2: ";
   cin>>grade2;
   cout << "grade_3: ";
   cin>>grade3 ;
   cout << "grade_4: ";
   cin>>grade4 ;
   cout << "grade_5: ";
   cin>>grade5 ;  
    calculate();        
}

void studgrade::printData() {
   cout << "==============================="<<endl;  
   cout << "Student name:   " << sname << endl;
   cout << "grade_1:     " <<setw(6)<< fixed << setprecision(2) <<grade1 << endl;
   cout << "grade_2:     " <<setw(6)<< fixed << setprecision(2) <<grade2 << endl;
   cout << "grade_3:     " <<setw(6)<< fixed << setprecision(2) <<grade3 << endl;
   cout << "grade_4:     " <<setw(6)<< fixed << setprecision(2) <<grade4 << endl;
   cout << "grade_5:     " <<setw(6)<< fixed << setprecision(2) <<grade5 << endl;
   cout << "==============================="<<endl;      
   cout << "total:       " <<setw(6)<< fixed << setprecision(2) <<total << endl;
   cout << "CGPA:        " <<setw(6)<< fixed << setprecision(2) <<CGPA << endl;

}

int main() {
   studgrade st1("A");     //Parameterized constructor
   st1.inputData();
   
   st1.printData();
   return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS