Question: HW21: CGrade Class Pointers Redo HW13/HW16/HW17 (or do it for the first time : ), but this time youll create a pointer to a class
HW21: CGrade Class Pointers
Redo HW13/HW16/HW17 (or do it for the first time : ), but this time youll create a pointer to a class object. The idea is very similar to the examples we talked about in class. When youre creating a pointer youll want to call two functions to allocate dynamic memory for CGrade and to display the grade to the output screen, MakeCGrade and ShowGrade respectively. However, youll want to implement these in the same file as main (Note: Youll only need to change main.cpp).
Main should create and allocate space for a pointer for an object CGrade. It will then prompt the user to enter the students name, quiz1, quiz2, midterm, and final exam. Then it will call the function MakeCGrade passing the grades of the students and it will return the address of the CGrade object created to be assigned to the pointer. You should check if the pointer is not NULL to make sure an address has been successfully assigned, if not, youll need to display an error message, delete the pointer, and exit. Then main will call ShowGrade by passing the pointer as an argument to display the grade object. Main will also display the grades of the students using the pointer and overloaded insertion operator. Dont forget to release the pointer!
MakeCGrade receives name, quiz1, quiz2, midterm, and final exam as arguments from the caller and uses them to create a CGrade object from the heap. If all five of the double parameters are successfully assigned to the CGrade object, a pointer to the object is returned to the caller, otherwise, a NULL is returned.
ShowGrade receives a pointer to a CGrade object and uses it to write the student info to the standard output stream using the objects DispGrade member function.
Note: What is a NULL pointer. It is essentially assigning a NULL to a pointer to signify that it has no address assigned to it instead of just a garbage value. For example:
CBox *myBox; // A pointer with a garbage value. CBox *myBox2 = NULL; // A pointer that is initialized to a NULL // We can test out if myBox has an address assigned. // We check because when we call the new operator // maybe there was no memory available from the HEAP // (maxing out the RAM). myBox2 = new CBox; // find memory in the HEAP for CBox if (myBox2 != NULL) { cout << "There was memory in the HEAP!!! "; } The program has 3 files total. A header file called cgradeClassPointers.h, where your class should be declared. An implementation file called cgradeClassPointers.cpp, where your functions implementations are stored. Lastly, youll need a file to store main called main.cpp. You can use your existing code and change it where you need to. You can save your files in your HW21 directory.
A sample run is below:
$ ./a.out Enter student's full name: Elon Musk Enter Quiz 1 grade (out of 10): 6 Enter Quiz 2 grade (out of 10): 7 Enter midterm grade (out of 100): 73 Enter final exam grade (out of 100): 88 Type Constructor! (Using the ShowGrade function...) Elon Musk: 78.5% C The grades of the student(s) are: Elon Musk: 78.5% C
This is HW17
MAIN
#include
// ==== main ================================================================== // // ============================================================================
int main() { CGrade S1; // first student CGrade S2("Edgar Allen Poe"); // second student CGrade S3(S2); // copy of the second student
// Display the default constructor info cout << " The grades of S1 (default constructor): ";
cout << S1;
// prompt the user for name/grades cout << " Enter student's full name, Quiz 1 grade (out of " << QUIZ1 << "), " << "Quiz 2 grade (out of " << QUIZ2 << "), midterm grade (out of " << MIDTERM << "), " << "and final exam grade (out of " << FINAL << "). (HIT ENTER AFTER EACH ONE!!!) ";
cin >> S1;
// call the function to calculate percent/grade S1.CalcGrade();
// check if S1 == S2 and S2 == S3 if (S1 == S2) { cout << " S1 == S2 "; } else { cout << " S1 != S2 ";
}
if (S2 == S3) { cout << " S2 == S3 "; } else { cout << " S2 != S3 "; }
// display their grades cout << " The grades of the students are: "; cout << S1; cout << S2; cout << S3;
return 0; } // end of "main"
cgradeOverload.h
// ============================================================================ // File: cgradeOverload.h // ============================================================================ // Programmer: Danish Idrees // Date: 11/3/2018 // // Description: // This is the header file for the CGrade class. // ============================================================================
#ifndef CGRADE_HEADER #define CGRADE_HEADER #include
// constant grade values const double QUIZ1 = 10; const double QUIZ2 = 10; const double MIDTERM = 100; const double FINAL = 100;
class CGrade { public: // constructors CGrade(); // default constructor CGrade(char sName[], double quiz1 = 0, double quiz2 = 0, double midterm = 0, double finalExam = 0, double percent = 0, char grade = 'F'); // type CGrade(CGrade &object); // copy constructor
// member functions void GetInfo(???); void CalcGrade(void); void CalcPercent(void); void DispGrade(???) ???;
// overloaded operators (==) ???
private: // data members char m_name[256]; double m_quiz1; double m_quiz2; double m_midterm; double m_finalExam; double m_percent; char m_grade; };
// overloaded streams ??? ???
#endif // CGRADE_HEADER
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
