Question

In: Computer Science

Write the header and the implementation files (.h and .cpp separately) for a class called Course,...

  1. Write the header and the implementation files (.h and .cpp separately) for a class called Course, and a simple program to test it (C++), according to the following specifications:                   
  1. Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array to represent the GPAs of each student.
  1. Your class has the following member functions:

       

  1. a constructor with an int parameter representing the number of students; the constructor creates the dynamic variable and sets it to the int parameter, creates the two dynamic arrays and sets everything to zero.

  1. a function which reads in all the ids and the GPAs into the appropriate arrays.
  1. a function called print_info which, for each student, does all of the

                following:

                It prints the student’s id and GPA. If the student’s GPA is greater than or equal to 3.8, it prints “an honor student”.

  1. a destructor (hint: delete array)
  2. create 5 random instances of Course objects to represents 5 sessons of CSC211, each session includes 20 students, assign random score to each student, calculate the average GPA of all 100 students.

Solutions

Expert Solution

Ans: Since It is not specified in the question hence all the data members are declared as private and an extra utility function to get average of GPA per course has been created. Alternative to this is declare gpa array as public and calculating average in the main function.

Also, assignRandom function is created just to populate the values in each instance of the class and hence it is not an essential function.

Note: Read comments in the code to get it's description.

#include<bits/stdc++.h>
using namespace std;

// Course Class
class Course{
    private:
        int* number_of_students;
        int* sId;
        double* gpa;

    public:
        // Constructor dynamically assigns memory
        Course(int student_count){
            number_of_students = new int;
            *number_of_students = student_count;

            sId = (int*)malloc(sizeof(int)*student_count);
            gpa = (double*)malloc(sizeof(double)*student_count);

            for(int i = 0; i<student_count; i++){
                sId[i] = 0;
                gpa[i] = 0;
            }
        }

        // Function to add students at the end in a particular course
        void enroll(int Id, double gpa_score){
            int i = 0;

            while(i<*number_of_students && sId[i] != 0)
                i++;

            // If no more students can be added then return
            if(i == *number_of_students)
                return;
            
            sId[i] = Id;
            gpa[i] = gpa_score;
        }

        // Function prints the required info per course
        void print_info(){
            int n = *number_of_students;

            for(int i = 0; i<n; i++){
                cout<<sId[i]<<"\t"<<gpa[i]<<"\t";
                if(gpa[i] >= 3.8)
                    cout<<"An honor student";
                cout<<"\n";
            }
        }

        // Returns the average value of GPA in a particular course
        double getAverage(){
            double sum = 0.0;
            for(int i = 0; i<*number_of_students; i++)
                sum += gpa[i];

            return sum/(*number_of_students);
        }

        // Destructor function
        ~Course(){
            delete (number_of_students);
            delete [] sId;
            delete [] gpa;
        }
};


// Utility function to assign random values to the instance of the course class
Course * assignRandom(Course* obj, int size){
    int id;
    double gpa;

    for(int i = 0; i<size; i++){
        id = (rand()%100)+1;
        gpa = (rand()%5)+1;
        gpa = gpa+(id/100.0);

        obj->enroll(id, gpa);
    }
    return obj;
}

// Driver function
int main(){

    Course *c1 = new Course(20);
    Course *c2 = new Course(20);
    Course *c3 = new Course(20);
    Course *c4 = new Course(20);
    Course *c5 = new Course(20);

    c1 = assignRandom(c1, 20);
    c1->print_info();
    cout<<"\n";

    c2 = assignRandom(c2, 20);
    c2->print_info();
    cout<<"\n";

    c3 = assignRandom(c3, 20);
    c3->print_info();
    cout<<"\n";

    c4 = assignRandom(c4, 20);
    c4->print_info();
    cout<<"\n";

    c5 = assignRandom(c5, 20);
    c5->print_info();
    cout<<"\n";

    // Calculating and printing overall GPA
    double overAllGpa = (c1->getAverage()+ c2->getAverage() + c3->getAverage() + c4->getAverage() + c5->getAverage())/5;

    cout<<"OverAll average GPA: "<<overAllGpa<<"\n";

    return 0;
}

Related Solutions

Write the header and the implementation files (.h and .cpp separatelu) for a class called Course,...
Write the header and the implementation files (.h and .cpp separatelu) for a class called Course, and a simple program to test it, according to the following specifications:                    Your class has 3 member data: an integer pointer that will be used to create a dynamic variable to represent the number of students, an integer pointer that will be used to create a dynamic array representing students’ ids, and a double pointer that will be used to create a dynamic array...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are...
WRITE ONLY THE TO DO'S   in FINAL program IN C++ (necessary cpp and header files are witten below) "KINGSOM.h " program below: #ifndef WESTEROS_kINGDOM_H_INCLUDED #define WESTEROS_KINGDOM_H_INCLUDED #include <iostream> namespace westeros { class Kingdom{         public:                 char m_name[32];                 int m_population; };         void display(Kingdom&);                      } #endif } Kingdom.cpp Program below: #include <iostream> #include "kingdom.h" using namespace std; namespace westeros {         void display(Kingdom& pKingdom) {                 cout << pKingdom.m_name << ", population " << pKingdom.m_population << endl;                                                               FINAL:...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only useiostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Create : locomotive.h, locomotive.cpp, main.cpp Locomotive Class Hierarchy Presented here will be a class diagram depicting the nature of the class hierarchy formed between a parent locomotive class and its children, the two kinds of specic trains operated. The relationship is a...
Complete the following task in C++. Separate your class into header and cpp files. You can...
Complete the following task in C++. Separate your class into header and cpp files. You can only use iostream, string and sstream. Create a main.cpp file to test your code thoroughly. Given : commodity.h and commodity.cpp here -> https://www.chegg.com/homework-help/questions-and-answers/31-commodity-request-presented-two-diagrams-depicting-commodity-request-classes-form-basic-q39578118?trackid=uF_YZqoK Given : locomotive.h, locomotive.cpp, main.cpp -> https://www.chegg.com/homework-help/questions-and-answers/complete-following-task-c--separate-class-header-cpp-files-useiostream-string-sstream-crea-q39733428 Create : DieselElectric.cpp DieselElectric.h DieselElectric dieselElectric -fuelSupply:int --------------------------- +getSupply():int +setSupply(s:int):void +dieselElectric(f:int) +~dieselElectric() +generateID():string +calculateRange():double The class variables are as follows: fuelSuppply: The fuel supply of the train in terms of a number of kilolitres...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main...
In C++ You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an arrayy of objects and practice passing objects to and return objects from functions. String functions practice has also been included. You have been given a template in Zylabs to help...
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file....
BankAccount: You will create 3 files: The .h (specification file), .cpp (implementation file) and main file. You will practice writing class constants, using data files. You will add methods public and private to your BankAccount class, as well as helper methods in your main class. You will create an array of objects and practice passing objects to and return objects from functions. String functions practice has also been included. Extend the BankAccount class. The member fields of the class are:...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class...
Separate code into .cpp and .h files: // C++ program to create and implement Poly class representing Polynomials #include <iostream> #include <cmath> using namespace std; class Poly { private: int degree; int *coefficients; public: Poly(); Poly(int coeff, int degree); Poly(int coeff); Poly(const Poly& p); ~Poly(); void resize(int new_degree); void setCoefficient(int exp, int coeff); int getCoefficient(int exp); void display(); }; // default constructor to create a polynomial with constant 0 Poly::Poly() { // set degree to 0 degree = 0; //...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full...
c++ Write the implementation (.cpp file) of the Counter class of the previous exercise. The full specification of the class is: A data member counter of type int. An data member named counterID of type int. A static int data member named nCounters which is initialized to 0. A constructor that takes an int argument and assigns its value to counter. It also adds one to the static variable nCounters and assigns the (new) value of nCounters to counterID. A...
You are to write a class named Rectangle. You must use separate files for the header...
You are to write a class named Rectangle. You must use separate files for the header (Rectangle.h) and implementation (Rectangle.cpp) just like you did for the Distance class lab and Deck/Card program. You have been provided the declaration for a class named Point. Assume this class has been implemented. You are just using this class, NOT implementing it. We have also provided a main function that will use the Point and Rectangle classes along with the output of this main...
i need an example of a program that uses muliple .h and .cpp files in c++...
i need an example of a program that uses muliple .h and .cpp files in c++ if possible.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT