C++ Classes Introduction
Problem Description​
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:
- set_CourseId( string CID): sets courseId
- set_Grade(char g): sets grade equal to g
- set_Credit(int cr): sets credits equal to cr 4.calculateGradePoints(char g): returns gradePoint(int)
- calculateHonorPoints(int gp,int cr): return honorPoint (float)
- display(): prints gradePoint and honorPoint
Examples​
Example 1:
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
0 ≤ CID.length() ≤ 100
'A' <= g <= 'F'
1 <= cr <= 4
Note: Grades are not case sensitive.
Example​
Example 1:
Input:
2
CSN-206 A 4
ECE-500 d 3
Output:
10 40
7 21
Solution for C++ Classes Introduction​
Code in Different Languages​
- Python
- Java
- C++
class CollegeCourse:
def __init__(self):
self.courseID = ""
self.gp = 0
self.grade = ''
self.credits = 0
def set_CourseId(self, courseID):
self.courseID = courseID
def set_Grade(self, grade):
self.grade = grade
def set_Credit(self, credits):
self.credits = credits
def calculateGradePoints(self, grade):
grade = grade.upper()
if grade == 'A':
self.gp = 10
elif grade == 'B':
self.gp = 9
elif grade == 'C':
self.gp = 8
elif grade == 'D':
self.gp = 7
elif grade == 'E':
self.gp = 6
elif grade == 'F':
self.gp = 5
return self.gp
def calculateHonorPoints(self, gp, credits):
return gp * credits
def display(self):
print(self.calculateGradePoints(self.grade), self.calculateHonorPoints(self.gp, self.credits))
# Example usage:
course = CollegeCourse()
course.set_CourseId("CS101")
course.set_Grade("A")
course.set_Credit(4)
course.display() #
public class CollegeCourse {
private String courseID;
private int gp;
private char grade;
private int credits;
public void setCourseId(String courseID) {
this.courseID = courseID;
}
public void setGrade(char grade) {
this.grade = grade;
}
public void setCredit(int credits) {
this.credits = credits;
}
public int calculateGradePoints(char grade) {
grade = Character.toUpperCase(grade);
switch (grade) {
case 'A':
gp = 10;
break;
case 'B':
gp = 9;
break;
case 'C':
gp = 8;
break;
case 'D':
gp = 7;
break;
case 'E':
gp = 6;
break;
case 'F':
gp = 5;
break;
}
return gp;
}
public int calculateHonorPoints(int gp, int credits) {
return gp * credits;
}
public void display() {
System.out.println(calculateGradePoints(grade) + " " + calculateHonorPoints(gp, credits));
}
public static void main(String[] args) {
CollegeCourse course = new CollegeCourse();
course.setCourseId("CS101");
course.setGrade('A');
course.setCredit(4);
course.display(); // Output: 10 40
}
}
class CollegeCourse
{
//your code here
private:
string courseID;
int gp;
char grade;
int credits;
public:
void set_CourseId(string courseID){
this->courseID = courseID;
}
void set_Grade(char grade){
this->grade = grade;
}
void set_Credit(int credits){
this->credits = credits;
}
int calculateGradePoints(char grade){
grade = toupper(grade);
switch(grade){
case 'A': gp = 10; break;
case 'B': gp = 9; break;
case 'C': gp = 8; break;
case 'D': gp = 7; break;
case 'E': gp = 6; break;
case 'F': gp = 5; break;
}
return gp;
}
int calculateHonorPoints(int gp, int credits){
return gp * credits;
}
void display(){
cout<<calculateGradePoints(grade)<<" "<<calculateHonorPoints(gp, credits)<<endl;
}
};
References​
- GeekForGeeks Problem: C++ Classes Introduction