Chapter 9 defined the struct studentType to implement the basic properties of a student. Define the class studentType with the same components as the struct studentType, and add member functions to manipulate the data members. (Note that the data members of the class studentType must be private.)
Write a program to illustrate how to use the class studentType.
struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
};
An example of the program is shown below:
Name: Sara Spilner
Grade: A
Test score: 89
Programming score: 92
GPA: 3.57
***************
#include <string>
#include <iostream>
using namespace std;
class studentType
{
public:
studentType() {}
studentType(string firstName_, string lastName_, char couurseGrade_, int testScore_, int programmingScore_, double GPA_);
void SetFirstName(string firstName) { this->firstName = firstName; }
string GetFristName() { return firstName; }
void SetLastName(string lastName) { this->lastName = lastName; }
string GetLastName() { return lastName; }
void SetCourseGrade(char courseGrade) { this->courseGrade = courseGrade; }
char GetCourseGrade() { return courseGrade; }
void SetTestScore(int testScore) { this->testScore = testScore; }
int GetTestScore() { return testScore; }
void SetProgrammingScore(int programmingScore) { this->programmingScore = programmingScore; }
int GetProgrammingScore() { return programmingScore; }
void SetGPA(double GPA) { this->GPA = GPA; }
double GetGPA() { return GPA; }
void Display();
private:
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
};
studentType::studentType(string firstName_, string lastName_, char couurseGrade_, int testScore_, int programmingScore_, double GPA_) :
firstName(firstName_), lastName(lastName_), courseGrade(couurseGrade_), testScore(testScore_), programmingScore(programmingScore_), GPA(GPA_)
{}
void studentType::Display()
{
cout << "Name: " << GetFristName() << " " << GetLastName() << endl;
cout << "Grade: " << GetCourseGrade() << endl;
cout << "Test score: " << GetTestScore() << endl;
cout << "Programming score: " << GetProgrammingScore() << endl;
cout << "GPA: " << GetGPA() << endl;
cout << "***************" << endl;
}
int main()
{
studentType Mike;
Mike.SetFirstName("Mike");
Mike.SetLastName("Wazowski");
Mike.SetCourseGrade('A');
Mike.SetTestScore(89);
Mike.SetProgrammingScore(92);
Mike.SetGPA(3.57);
Mike.Display();
system("pause");
return 0;
}
Comments
Leave a comment