8.4: Case Study- C++ Class- StudentRecord

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    8.4: Case Study- C++ Class- StudentRecord

    Here we would like to discuss another example of C++ Class: StudentRecord.

    The class StudentRecord is for creating an object of student record. As a student record, the object may contain following information:

    Student last name
    Student first name
    Student identification number
    Student GPA record

    Of course, a student record may contain many other information, for simplicity, here we only consider a student record that contains above information.

    We will specify four data members in the private section of the definition of the class StudentRecord. We will use string variables for holding student's last name, first name, and the id number. We use double variable for holding the GPA record:

    class StudentRecord
    {
    private:
    string lastName;
    string firstName;
    string idNumber;
    double GPA;

    public:

    ------

    };

    In the public section, we will first need a constructor which will initialize the values of the data members:

    The default constructor:

    StudentRecord();

    The non-default constructor:

    StudentRecord(string last, string first, string iD, double gpa);

    Then we will need a pair of set and get. functions for each of the data members:

    void setLastName(string last);
    string getLastName();

    void setLastFirstName(string first);
    string getFirstName();

    void setID(string iD);
    string getID();

    void setGPA(string last);
    double getGPA();

    Finally, we will need a member function that will display the content of a student record:

    void display();

    The definition of the class StudentRecord is shown as following:

    SR01.png

    Now will need to implement those constructors and member functions. "To implement " means to put C++ statements in those functions to tell what they need to do when they were called in the source file of a program.

    First, let us look the default constructor. The default constructor will be called when we create an object of StudentRecord without providing any initial values for the data members. The function will simply assign the default values for each data member:

    StudentRecord::StudentRecord()
    {
    lastName="None";
    firstName="None";
    idNumber="----";
    GPA=0;
    }

    Then, we will look at non-default constructor. The non-default constructor will set the input values to each of the data members of the class:

    StudentRecord::StudentRecord(string last, string first, string iD, double gpa)
    {
    lastName=last;
    firstName=first;
    idNumber=iD;
    GPA=gpa;
    }

    Followings are the set and get functions for each data member:

    void StudentRecord::setLastName(string last)
    {
    lastName=last;
    }

    string StudentRecord::getLastName()
    {
    return lastName;
    }

    void StudentRecord::setFirstName(string first)
    {
    firstName=first;
    }

    string StudentRecord::getFirstName()
    {
    return firstName;
    }

    void StudentRecord::setID(string iD)
    {
    idNumber=iD;
    }

    string StudentRecord::getID()
    {
    return idNumber;
    }

    void StudentRecord::setGPA(double gpa)
    {
    GPA=gpa;
    }

    double StudentRecord::getGPA()
    {
    return GPA;
    }

    Finally, we look at the display function. This function simply displays each data member of the class on the screen:

    void StudentRecord::display()
    {
    cout<<"========================================="<<endl;
    cout<<setw(20)<<"Student Last Name:"<<setw(10)<<lastName<<endl;
    cout<<setw(20)<<"Student First Name:"<<setw(10)<<firstName<<endl;
    cout<<setw(20)<<"Student ID:"<<setw(10)<<idNumber<<endl;
    cout<<setw(20)<<"Student GPA:"<<setw(10)<<GPA<<endl;
    cout<<"========================================="<<endl;
    }

    We will place above implementations in the header file StudentRecord.h below the class interface:

    SR02.png

    SR03.png

    Since we use "cout" and set() functions in our implementations, will need to place following lines at the beginning of the header file:

    #include<string>
    #include<iostream>
    #include<iomanip>
    using namespace std;

    After we correctly create the header file with above class in the project, we can open a source file and use the class StudentRecord to create objects in the main() function:

    Image 8-4-1.png

    Line 7: We declare an instance of the class StudentRecord, recordA ,without providing initial values. The default constructor will be called here.

    Line 8: We declare an instance of the class StudentRecord, recordB, with the initial values provided

    Line 9 and Line 10: We call the member function display() of each object to display the recordA and recordB in the output window.

    Following is the screenshot of a sample run of the program:

    Image 8-4-2.png


    This page titled 8.4: Case Study- C++ Class- StudentRecord was last modified on Wed, 24 Dec 2025 20:37:23 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.