fork download
  1. #include <iostream>
  2. using namespace std;
  3. class Student{
  4. public:
  5. double attn;
  6. int n;
  7. double *grades;
  8. double CGPA;
  9.  
  10. Student(){
  11. attn=0;
  12. n=0;
  13. grades=nullptr;
  14. }
  15. Student(double attn, int n, double* grades){
  16. this->attn=attn;
  17. this->n=n;
  18. this->grades=new double[n];
  19. for(int i=0;i<n;i++){
  20. this->grades[i]=grades[i];
  21. }
  22. }
  23. ~Student(){
  24. delete[] grades;
  25. }
  26. double calculateGradeSumOfCourses(){
  27. double sum=0;
  28. for(int i=0;i<n;i++){
  29. sum+=grades[i];
  30. }
  31. return sum;
  32. }
  33. void calculateCGPA(){
  34. if(n==0) {
  35. CGPA=0;
  36. return;
  37. }
  38. double sum=calculateGradeSumOfCourses();
  39. CGPA=sum/n;
  40. }
  41. };
  42.  
  43.  
  44. class L4Student : public Student{
  45. public:
  46. double thesis;
  47.  
  48. L4Student(){
  49. thesis=0;
  50. }
  51.  
  52. L4Student(double attn, int n, double* grades, double thesis): Student(attn, n, grades){
  53. this->thesis=thesis;
  54. }
  55. void calculateCGPA(){
  56. if(n==0) {
  57. CGPA=0;
  58. return;
  59. }
  60. double sum=calculateGradeSumOfCourses();
  61. sum+=thesis;
  62. CGPA=sum/(n+1);
  63. }
  64. };
  65.  
  66.  
  67. int main(){
  68. double gradesOfS[]={4.00, 3.75, 3.75, 3.5, 4.00};
  69. Student s(1, 5, gradesOfS);
  70.  
  71. s.calculateCGPA();
  72. cout<<s.CGPA<<endl;
  73.  
  74. double gradesOfL4S[]={4.00, 3.75, 3.5, 3.5, 2.50};
  75. L4Student l4s(0.75, 5, gradesOfL4S, 4.00);
  76.  
  77. l4s.calculateCGPA();
  78. cout<<l4s.CGPA<<endl;
  79. return 0;
  80. }
  81.  
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
3.8
3.54167