C++ Classes Introduction | Practice | GeeksforGeeks

Create class named CollegeCourse with fields courseID, grade, credits, gradePoints and honorPoints. Calculate honorpoints as the product of gradepoints and credits. GradePoints are calculated as (A-10),(B-9),(C-8),(D-7),(E-6) & (F-5).
Class CollegeCourse contains following functions:
1. set_CourseID( string CID): sets courseID
2. set_Grade(char g): sets grade equal to g
3. set_Credit(int cr): sets credits equal to cr 
4.calculateGradePoints(char g): returns gradePoint(int)
5. calculateHonorPoints(int gp,int cr): return honorPoint (float)
6. display(): prints gradePoint and honorPoint
Input:
The first line contains an integer T, the number of test cases. For each test case, there is a string CID, denoting Course ID, a character g, denoting the grade and an integer cr, denoting the credits of the course.
Output:
For each test case, the output is the gradePoints & the honorPoints of that course.
Constraints:
1<=T<=100
1<=CID.length()<=100
'A'<=g<='F'
1<=cr<=4
Note: Grades are not case sensitive.
Example:
Input
2
CSN-206 A 4
ECE-500 d 3
Output
10 40
7 21
0
atulharsh274Premium1 month ago
int calculateGradePoints(char g){           char arr[]={'A','B','C','D','E','F'};           char arr2[]={'a','b','c','d','e','f'};           for(int i=0;i<6;i++){               if(arr[i]==g || arr2[i]==g){gradePoints = 10-i;                break;           }}           return gradePoints;       }
0
atulharsh274Premium1 month ago
class CollegeCourse{   //your code here   private:    string courseID;      char grade;      int credits;      int gradePoints;      float  honorPoints;      public:      void  set_CourseId( string CID){           courseID = CID;      }     void set_Grade(char g){          grade = g;      }    void set_Credit(int cr){           credits = cr;       }             int calculateGradePoints(char g){           char arr[]={'A','B','C','D','E','F'};           char arr2[]={'a','b','c','d','e','f'};           for(int i=0;i<6;i++){               if(arr[i]==g || arr2[i]==g){gradePoints = 10-i;                break;           }}           return gradePoints;       }        float calculateHonorPoints(int gp,int cr){            honorPoints = gp*cr;              return honorPoints;         }                void display(){              cout<<gradePoints<<" "<<honorPoints<<endl;        }   }; 
 
0
harshithbhat
This comment was deleted.
+1
9chara92 months ago
    int calculateGradePoints(char g) 
   {
       gradePoints = 75 - toupper(g); 
       return gradePoints; 
   }
0
parasparihar613 months ago
// { Driver Code Starts//Initial Template for C++#include<bits/stdc++.h>using namespace std;
// } Driver Code Ends//User function Template for C++
// CollegeCourse Class class CollegeCourse{string courseID;char grade;int credits;int gradepoints;float honorPoints;public:void set_CourseId(string CID){   courseID=CID;}void set_Grade(char g){   grade=g;}void set_Credit(int cr){   credits=cr;}int calculateGradePoints(char g){  switch(g)  {      case 'A':gradepoints=10;      return 10;      break;      case 'B': gradepoints=9;      return 9;      break;      case 'C': gradepoints=8;      return 8;      break;      case 'D': gradepoints=7;      return 7;      break;      case 'E': gradepoints=6;      return 6;      break;      case 'F': gradepoints=5;      return 5;      break;      case 'a': gradepoints=10;      return 10;      break;      case 'b': gradepoints=9;      return 9;      break;      case 'c': gradepoints=8;      return 8;      break;      case 'd': gradepoints=7;      return 7;      break;      case 'e': gradepoints=6;      return 6;      break;      case 'f': gradepoints=5;      return 5;      break;  }}float calculateHonorPoints(int gp,int cr){      honorPoints= gp*cr;   return honorPoints;}void display(){   cout<<gradepoints<<" "<<honorPoints<<endl;}};
// { Driver Code Starts.
int main(){   int t;   cin>>t;   while(t--)   {   CollegeCourse cc;   string courseId;   int gp;   char grade;   int credits;   cin>>courseId>>grade>>credits;   cc.set_CourseId(courseId);   cc.set_Grade(grade);   cc.set_Credit(credits);   gp=cc.calculateGradePoints(grade);   cc.calculateHonorPoints(gp,credits);   cc.display();   }   return 0;} // } Driver Code Ends
0
parasparihar613 months ago
// { Driver Code Starts//Initial Template for C++#include<bits/stdc++.h>using namespace std;
// } Driver Code Ends//User function Template for C++
// CollegeCourse Class class CollegeCourse{string courseID;char grade;int credits;int gradepoints;float honorPoints;public:void set_CourseId(string CID){   courseID=CID;}void set_Grade(char g){   grade=g;}void set_Credit(int cr){   credits=cr;}int calculateGradePoints(char g){  switch(g)  {      case 'A':gradepoints=10;      return 10;      break;      case 'B': gradepoints=9;      return 9;      break;      case 'C': gradepoints=8;      return 8;      break;      case 'D': gradepoints=7;      return 7;      break;      case 'E': gradepoints=6;      return 6;      break;      case 'F': gradepoints=5;      return 5;      break;      case 'a': gradepoints=10;      return 10;      break;      case 'b': gradepoints=9;      return 9;      break;      case 'c': gradepoints=8;      return 8;      break;      case 'd': gradepoints=7;      return 7;      break;      case 'e': gradepoints=6;      return 6;      break;      case 'f': gradepoints=5;      return 5;      break;  }}float calculateHonorPoints(int gp,int cr){      honorPoints= gp*cr;   return honorPoints;}void display(){   cout<<gradepoints<<" "<<honorPoints<<endl;}};
// { Driver Code Starts.
int main(){   int t;   cin>>t;   while(t--)   {   CollegeCourse cc;   string courseId;   int gp;   char grade;   int credits;   cin>>courseId>>grade>>credits;   cc.set_CourseId(courseId);   cc.set_Grade(grade);   cc.set_Credit(credits);   gp=cc.calculateGradePoints(grade);   cc.calculateHonorPoints(gp,credits);   cc.display();   }   return 0;} // } Driver Code Ends
0
tushargarg98684 months ago
// { Driver Code Starts//Initial Template for C++#include<bits/stdc++.h>using namespace std;
// } Driver Code Ends//User function Template for C++
// CollegeCourse Class class CollegeCourse{   //your code here   string courseId;   char grade;   int credits;   int gradePoints;   float honarPoints;   public:   void set_CourseId(string CID){       courseId=CID;   }   void set_Grade(char g){       grade=g;   }   void set_Credit(int cr){       credits=cr;   }   int calculateGradePoints(char g){       if(g=='A' || g=='a'){           gradePoints=10;       }       else if(g=='b' || g=='B'){           gradePoints=9;       }       else if(g=='c' || g=='C'){           gradePoints=8;       }       else if(g=='d' || g=='D'){           gradePoints=7;       }       else if(g=='e' || g=='E'){           gradePoints=6;       }       else if(g=='f' || g=='F'){           gradePoints=5;       }       return gradePoints;   }   float calculateHonorPoints(int gp, int cr){       honarPoints=gradePoints*credits;       return honarPoints;   }   void display(){       cout<<gradePoints<<" "<<honarPoints<<endl;   }      };
// { Driver Code Starts.
int main(){   int t;   cin>>t;   while(t--)   {   CollegeCourse cc;   string courseId;   int gp;   char grade;   int credits;   cin>>courseId>>grade>>credits;   cc.set_CourseId(courseId);   cc.set_Grade(grade);   cc.set_Credit(credits);   gp=cc.calculateGradePoints(grade);   cc.calculateHonorPoints(gp,credits);   cc.display();   }   return 0;} // } Driver Code Ends
0
bhalgatshreya24 months ago
// { Driver Code Starts
//Initial Template for C++
#include<bits/stdc++.h>
using namespace std;


 // } Driver Code Ends
//User function Template for C++

// CollegeCourse Class 
class CollegeCourse
{
   public:
    string CID;
    char g;
   int cr;
   int gradePoints;
   float honorPoints;
   
 
         void set_CourseId(string courseId)
         {
            CID = courseId ;
         }
         void set_Grade(char grade)
         {
             g = grade;
         }
         void set_Credit(int credits)
         {
             cr = credits;
         }
 int calculateGradePoints(char g)
   {
       if(g == 'A' or g == 'a')
       {
           gradePoints = 10;
       }
       else if(g == 'B' or g == 'b')
       {
           gradePoints = 9;
       }
       else if(g == 'C' or g == 'c')
       {
           gradePoints = 8;
       }
       else if(g == 'D' or g == 'd')
       {
           gradePoints = 7;
       }
       else if(g == 'E' or g == 'e')
       {
           gradePoints = 6;
       }
       else if(g == 'F' or g == 'f')
       {
           gradePoints = 5;
       }
       
       return gradePoints;
   }

         int calculateHonorPoints(int gp,int cr)
         {
                honorPoints = gradePoints * cr;
                return honorPoints;
         }
         void display()
         {
             cout<<gradePoints<<" "<<honorPoints<<endl;
         }
    
};

// { Driver Code Starts.

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
    CollegeCourse cc;
    string courseId;
    int gp;
    char grade;
    int credits;
    cin>>courseId>>grade>>credits;
    cc.set_CourseId(courseId);
    cc.set_Grade(grade);
    cc.set_Credit(credits);
    gp=cc.calculateGradePoints(grade);
    cc.calculateHonorPoints(gp,credits);
    cc.display();
    }
    return 0;
}
  // } Driver Code Ends
0
priyam265 months ago
class CollegeCourse{   //why does this code is showing error?   // prog.cpp: In function int main():   // prog.cpp:70:8: error: class CollegeCourse has no //member named set_CourseId   //  cc.set_CourseId(courseId);   //     ^   string courseID;   char grade;   int credits, gradePoints;   float honorPoints;   public:       void set_CourseID(string CID) {           courseID = CID;       }       void set_Grade(char g) {           grade = g;       }       void set_Credit(int cr) {           credits = cr;       }       int calculateGradePoints(char g) {           if(g == 'A' or g == 'a') {               gradePoints = 10;           }           else if(g == 'B' or g == 'b') {               gradePoints = 9;           }           else if(g == 'C' or g == 'c') {               gradePoints = 8;           }           else if(g == 'D' or g == 'd') {               gradePoints = 7;           }           else if(g == 'E' or g == 'e') {               gradePoints = 6;           }           else if(g == 'F' or g == 'f') {               gradePoints = 5;           }           return gradePoints;       }       float calculateHonorPoints(int gp,int cr) {           honorPoints = gradePoints * credits;           return honorPoints;       }       void display() {           cout << gradePoints << " " << honorPoints << endl;       }   }; 
0
priyam26
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.