Define a class in C++ that has the following properties: class student{ string n
ID: 3785433 • Letter: D
Question
Define a class in C++ that has the following properties:
class student{
string name, id;
float gpa; // 0 <= gpa <= 4
public:
student(); //cons_0 ==> set default values with empty string and 0.00 as
gpa
student(string, string, float); //cons_1
student(const student&); //cost_2
string get_name();
string get_id();
float get_gpa();
void set_name(string);
void set_id(string);
void set_gpa(float);
}
In your main function (should be declared in a file named
'program_01_yourUNTID.cpp')
you will create:
i) three objects of type: student (say, a, b, c).
ii) initialize 'a' with constructor cons_0,
initialize 'c' with constructor cons_1,
initialize 'b' with constructor cons_2, you can pass either a or
c
iii) print all the properties of each object
iv) create an array of 5 objects. You can ask the user either
a) to insert the properties while constructing the
new object
b) to use a predefined object (say, b) to construct
the new object
(we will prefer a menu to handle such scenario)
v) print the name and id of the student with highest gpa out of the 5
students;
Explanation / Answer
#include<iostream>
using namespace std;
class student{
string name, id;
float gpa; // 0 <= gpa <= 4
public:
student()//cons_0 ==> set default values with empty string and 0.00 as
{
name="";
id = "";
gpa = 0;
}
student(string n, string i, float gp)
{
name =n;
id=i;
gpa =gp;
} //cons_1
student(const student&)//cost_2
{
name = *student.name;
id = *student.id;
gpa = *student.gpa;
}
string get_name()
{
return name;
}
string get_id()
{
return id;
}
float get_gpa()
{
return gpa;
}
void set_name(string n)
{
name =n;
}
void set_id(string i)
{
id =i;
}
void set_gpa(float g)
{
gpa=g;
}
void display()
{
cout<<"name is:"<<name;
cout<<"id is:"<<id;
cout<<"gpa is:"<<gpa;
}
};
int main()
{
student a = new student();
student b = a;
student c = new student("ankit","1","9");
a.display();
b.display();
c.display();
student arr[5];
arr[0]= new student("a","1",1);
arr[1]= new student("b","2",2);
arr[2]= new student("c","3",3);
arr[3]= new student("d","4",4);
arr[4]= new student("e","5",5);
int max=-1;
int maxind=0;
for(int i=0;i<5;i++)
{
if(arr[i].get_gpa()>max)
{
max = arr[i].get_gpa();
maxind =i;
}
}
cout<<"highest gpa student details are as follows:"<<endl;
arr[maxind].display();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.