45.7k views
5 votes
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

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:

struct studentType
{
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
};
An example of the program is shown below:

Name: Paul Johnson
Grade: B
Test score: 79
Programming score: 84
GPA: 3.24

User Rerx
by
8.2k points

2 Answers

2 votes

Final answer:

To define the class studentType with the same components as the struct studentType, make data members private and provide public member functions for manipulation. Example code.

Step-by-step explanation:

To define the class studentType with the same components as the struct studentType and add member functions to manipulate the data members, you will need to make the data members private in the class. Additionally, you can provide public member functions to access and modify the private data members. Here is an example of how you could define and use the class studentType:

class studentType
{
private:
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
public:
void setFirstName(string fName)
{
firstName = fName;
}
string getFirstName()
{
return firstName;
}
// Add similar member functions for other data members
};
int main()
{
studentType student;
student.setFirstName("Paul");
// Use similar member functions to set and get other data members
return 0;
}
User Agne
by
8.0k points
6 votes

Final answer:

A class 'StudentType' is created with the same elements as the given struct, but with private data members. Public member functions are added to manipulate the data, and a program is provided to demonstrate the class's usage.

Step-by-step explanation:

The requirement is to define a class studentType based on the previously defined struct studentType and to include private member variables along with public member functions for manipulating these variables. Below is a C++ class definition that models a student's basic properties and illustrates how to use it through an example program.

class StudentType {
private:
string firstName;
string lastName;
char courseGrade;
int testScore;
int programmingScore;
double GPA;
public:
// Constructors, accessors, and mutators
void setStudentInfo(string fName, string lName, char grade, int test, int programming, double gpa) {
firstName = fName;
lastName = lName;
courseGrade = grade;
testScore = test;
programmingScore = programming;
GPA = gpa;
}
// Add other necessary member functions here
// Example member function
void printStudentInfo() const {
cout << "Name: " << firstName << " " << lastName << endl;
cout << "Grade: " << courseGrade << endl;
cout << "Test score: " << testScore << endl;
cout << "Programming score: " << programmingScore << endl;
cout << "GPA: " << GPA << endl;
}
};
To use this class, we would instantiate an object of StudentType and use its member functions to set and retrieve student data. For example:

int main() {
StudentType student;
student.setStudentInfo("Paul", "Johnson", 'B', 79, 84, 3.24);
student.printStudentInfo();
return 0;
}
This simple program will output the information about the student, Paul Johnson, with the grade, test score, programming score, and GPA as provided.

User Borntyping
by
7.9k points