please slove it Define a class named Student that has the following data members
ID: 3552061 • Letter: P
Question
please slove it
Define a class named Student that has the following data members and member functions: age - an int that holds the Student's age. gpa a double that holds the Student's current GPA ranged 0.0 - 4.0. Constructor it has one default argument (default value: 20) which should be assigned to the data member: age. The constructor also sets the gpa to 0.0. setGpa(double) - void function, set GPA value. If the gpa is greater than 4, set it to 0.0. print lnfo - it displaces the data members in one line. Then write the main program to perform the following tasks: (1) Create an object with some data. (2) Prompt user to input a GPA value, Call set GpaQ with the input value as the parameter. (3) Call print lnfo(). Add another data member called name which is a pointer to char and a destructor to delete name. Also add a void function called update(char *), which will update the name: first delete the old name and use new to reallocate memory (see constructor). In the constructor, first add one more default parameter (char * with default value "John") and then use new to allocate exact memory for name and copy the string to name (age and gpa are set to 0). Modify print lnfoQ to display name. In your main function, use new to declare an array of Student type with 20 elements. Use for or while loop to prompt user to input student name, age and gpa. Stop the for/while loop either the user enters exit for the student name or all 20 elements are filled. Then print all student information. You may assume that the user will input correct student name (just one word so that you can use cin to pick up the name). Use one decimal for gpa. Note: if you have no time to finish your for/while loop, you can perform as part 1 and will receive 15%.Explanation / Answer
#include<iostream>
using namespace std;
class Student
{
public:
int age;
double gpa;
Student();
void setgpa(double gpa);
void printinfo();
};
Student::Student(void)
{
age=20;
gpa=0.0;
}
void Student::setgpa(double gpa1)
{
if(gpa1>4.0)
gpa=0.0;
else
gpa=gpa1;
}
void Student::printinfo(void)
{
cout<<"age: "<<age<<" gpa : "<<gpa<<endl;
}
int main()
{
double gpa;
Student student1;
cout<<"enter your gpa : ";
cin>>gpa;
student1.setgpa(gpa);
student1.printinfo();
}
part 2
#include <iostream>
#include <cstring>
using namespace std;
class Student {
int age;
double gpa;
char *name;
public :
Student(char *n = "John") {
age = 0;
gpa = 0;
int len = strlen(n);
name = new char[len+1];
strcpy(name,n);
}
void setGpa(double g) {
if(g > 4)
gpa = 0.0;
else
gpa = g;
}
void setAge(int a) {
age = a;
}
void printInfo() {
cout<<" "<<name<<" "<<age<<" "<<gpa;
}
void update(char *n) {
deleteName();
name = new char[20];
strcpy(name,n);
}
void deleteName() {
delete(name);
}
};
int main() {
Student *object;
object = new Student[20];
int a, i = 0;
double g;
char *name;
do {
name = new char[20];
cout<<" Enter the name of student "<<i+1<<": ";
cin>>name;
if(!strcmp(name,"exit"))
break;
cout<<" Enter "<<name<<"'s age and GPA: ";
cin>>a>>g;
object[i].setAge(a);
object[i].setGpa(g);
object[i].update(name);
delete(name);
++i;
}while(strcmp(name,"exit"));
cout<<" Student Information: ";
cout<<" Name Age GPA";
for(int j = 0;j < i;++j)
object[j].printInfo();
cout<<" ";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.