Skip to main content

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:

  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)
  4. calculateHonorPoints(int gp,int cr): return honorPoint (float)
  5. 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​

Written by @vansh-codes
 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() #

References​