|
| 1 | +--- |
| 2 | +id: cpp-classes-introduction |
| 3 | +title: C++ Classes Introduction |
| 4 | +sidebar_label: 0101 C++ Classes Introduction |
| 5 | + |
| 6 | +tags: |
| 7 | +- CPP |
| 8 | +- OOP |
| 9 | +- Classes |
| 10 | + |
| 11 | +description: "This is a solution to the C++ Classes Introduction problem on GeeksForGeeks." |
| 12 | +--- |
| 13 | + |
| 14 | +## Problem Description |
| 15 | +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). |
| 16 | +Class CollegeCourse contains following functions: |
| 17 | +1. set_CourseId( string CID): sets courseId |
| 18 | +2. set_Grade(char g): sets grade equal to g |
| 19 | +3. set_Credit(int cr): sets credits equal to cr |
| 20 | +4.calculateGradePoints(char g): returns gradePoint(int) |
| 21 | +5. calculateHonorPoints(int gp,int cr): return honorPoint (float) |
| 22 | +6. display(): prints gradePoint and honorPoint |
| 23 | + |
| 24 | +### Examples |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | +``` |
| 28 | +Input: |
| 29 | +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. |
| 30 | +
|
| 31 | +Output: |
| 32 | +For each test case, the output is the gradePoints & the honorPoints of that course. |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +### Constraints |
| 37 | +- `1 ≤ T ≤ 100` |
| 38 | +- `0 ≤ CID.length() ≤ 100` |
| 39 | +- `'A' <= g <= 'F'` |
| 40 | +- `1 <= cr <= 4` |
| 41 | + |
| 42 | +Note: Grades are not case sensitive. |
| 43 | + |
| 44 | +### Example |
| 45 | + |
| 46 | +**Example 1:** |
| 47 | +``` |
| 48 | +Input: |
| 49 | +2 |
| 50 | +CSN-206 A 4 |
| 51 | +ECE-500 d 3 |
| 52 | +
|
| 53 | +Output: |
| 54 | +10 40 |
| 55 | +7 21 |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +## Solution for C++ Classes Introduction |
| 60 | + |
| 61 | +### Code in Different Languages |
| 62 | + |
| 63 | +<Tabs> |
| 64 | + |
| 65 | +<TabItem value="Python" label="Python"> |
| 66 | +<SolutionAuthor name="@vansh-codes" /> |
| 67 | + |
| 68 | + ```python |
| 69 | + class CollegeCourse: |
| 70 | + def __init__(self): |
| 71 | + self.courseID = "" |
| 72 | + self.gp = 0 |
| 73 | + self.grade = '' |
| 74 | + self.credits = 0 |
| 75 | + |
| 76 | + def set_CourseId(self, courseID): |
| 77 | + self.courseID = courseID |
| 78 | + |
| 79 | + def set_Grade(self, grade): |
| 80 | + self.grade = grade |
| 81 | + |
| 82 | + def set_Credit(self, credits): |
| 83 | + self.credits = credits |
| 84 | + |
| 85 | + def calculateGradePoints(self, grade): |
| 86 | + grade = grade.upper() |
| 87 | + if grade == 'A': |
| 88 | + self.gp = 10 |
| 89 | + elif grade == 'B': |
| 90 | + self.gp = 9 |
| 91 | + elif grade == 'C': |
| 92 | + self.gp = 8 |
| 93 | + elif grade == 'D': |
| 94 | + self.gp = 7 |
| 95 | + elif grade == 'E': |
| 96 | + self.gp = 6 |
| 97 | + elif grade == 'F': |
| 98 | + self.gp = 5 |
| 99 | + return self.gp |
| 100 | + |
| 101 | + def calculateHonorPoints(self, gp, credits): |
| 102 | + return gp * credits |
| 103 | + |
| 104 | + def display(self): |
| 105 | + print(self.calculateGradePoints(self.grade), self.calculateHonorPoints(self.gp, self.credits)) |
| 106 | + |
| 107 | +# Example usage: |
| 108 | +course = CollegeCourse() |
| 109 | +course.set_CourseId("CS101") |
| 110 | +course.set_Grade("A") |
| 111 | +course.set_Credit(4) |
| 112 | +course.display() # |
| 113 | + ``` |
| 114 | + |
| 115 | +</TabItem> |
| 116 | + |
| 117 | +<TabItem value="Java" label="Java"> |
| 118 | +<SolutionAuthor name="@vansh-codes" /> |
| 119 | + |
| 120 | + ``` |
| 121 | + public class CollegeCourse { |
| 122 | + private String courseID; |
| 123 | + private int gp; |
| 124 | + private char grade; |
| 125 | + private int credits; |
| 126 | + |
| 127 | + public void setCourseId(String courseID) { |
| 128 | + this.courseID = courseID; |
| 129 | + } |
| 130 | + |
| 131 | + public void setGrade(char grade) { |
| 132 | + this.grade = grade; |
| 133 | + } |
| 134 | + |
| 135 | + public void setCredit(int credits) { |
| 136 | + this.credits = credits; |
| 137 | + } |
| 138 | + |
| 139 | + public int calculateGradePoints(char grade) { |
| 140 | + grade = Character.toUpperCase(grade); |
| 141 | + switch (grade) { |
| 142 | + case 'A': |
| 143 | + gp = 10; |
| 144 | + break; |
| 145 | + case 'B': |
| 146 | + gp = 9; |
| 147 | + break; |
| 148 | + case 'C': |
| 149 | + gp = 8; |
| 150 | + break; |
| 151 | + case 'D': |
| 152 | + gp = 7; |
| 153 | + break; |
| 154 | + case 'E': |
| 155 | + gp = 6; |
| 156 | + break; |
| 157 | + case 'F': |
| 158 | + gp = 5; |
| 159 | + break; |
| 160 | + } |
| 161 | + return gp; |
| 162 | + } |
| 163 | + |
| 164 | + public int calculateHonorPoints(int gp, int credits) { |
| 165 | + return gp * credits; |
| 166 | + } |
| 167 | + |
| 168 | + public void display() { |
| 169 | + System.out.println(calculateGradePoints(grade) + " " + calculateHonorPoints(gp, credits)); |
| 170 | + } |
| 171 | + |
| 172 | + public static void main(String[] args) { |
| 173 | + CollegeCourse course = new CollegeCourse(); |
| 174 | + course.setCourseId("CS101"); |
| 175 | + course.setGrade('A'); |
| 176 | + course.setCredit(4); |
| 177 | + course.display(); // Output: 10 40 |
| 178 | + } |
| 179 | + } |
| 180 | + ``` |
| 181 | + |
| 182 | +</TabItem> |
| 183 | + |
| 184 | +<TabItem value="C++" label="C++"> |
| 185 | +<SolutionAuthor name="@vansh-codes" /> |
| 186 | + |
| 187 | + ```cpp |
| 188 | + class CollegeCourse |
| 189 | + { |
| 190 | + //your code here |
| 191 | + private: |
| 192 | + string courseID; |
| 193 | + int gp; |
| 194 | + char grade; |
| 195 | + int credits; |
| 196 | + |
| 197 | + public: |
| 198 | + void set_CourseId(string courseID){ |
| 199 | + this->courseID = courseID; |
| 200 | + } |
| 201 | + |
| 202 | + void set_Grade(char grade){ |
| 203 | + this->grade = grade; |
| 204 | + } |
| 205 | + |
| 206 | + void set_Credit(int credits){ |
| 207 | + this->credits = credits; |
| 208 | + } |
| 209 | + |
| 210 | + int calculateGradePoints(char grade){ |
| 211 | + grade = toupper(grade); |
| 212 | + switch(grade){ |
| 213 | + case 'A': gp = 10; break; |
| 214 | + case 'B': gp = 9; break; |
| 215 | + case 'C': gp = 8; break; |
| 216 | + case 'D': gp = 7; break; |
| 217 | + case 'E': gp = 6; break; |
| 218 | + case 'F': gp = 5; break; |
| 219 | + } |
| 220 | + return gp; |
| 221 | + } |
| 222 | + |
| 223 | + int calculateHonorPoints(int gp, int credits){ |
| 224 | + return gp * credits; |
| 225 | + } |
| 226 | + |
| 227 | + void display(){ |
| 228 | + cout<<calculateGradePoints(grade)<<" "<<calculateHonorPoints(gp, credits)<<endl; |
| 229 | + } |
| 230 | + |
| 231 | + }; |
| 232 | + ``` |
| 233 | + |
| 234 | +</TabItem> |
| 235 | +</Tabs> |
| 236 | + |
| 237 | +## References |
| 238 | + |
| 239 | +- **GeekForGeeks Problem**: [C++ Classes Introduction](https://www.geeksforgeeks.org/problems/c-classes-introduction/0) |
0 commit comments