Templates

Write a main function that does the followings:
--- Create an empty vector of Student class (see code below).
--- Add 4 Student objects to the vector using the push_back() method.
--- Use a loop to show GPAs of 4 objects.
--- Insert a new Student object in the middle of the vector.
--- Use a loop to show GPAs of 5 objects
class Student
{
protected:
int ID;
float mathScore;
float chemScore;
public:
Student (int = 0, float = 0.0f, float = 0.0f);
void getGPA();
};

Student::Student (int id, float ms, float cs)
{
ID = id;
mathScore = ms;
chemScore = cs;
}

void Student::getGPA ()
{
float gpa = (mathScore + chemScore)/2.0;
cout << "ID = " << ID << " PGA = " << gpa << endl;
}

Please note, that this is not a homework site. We won't do your homework for you. The purpose of homework is that you learn by doing. However we are always willing to help solve problems you encountered, correct mistakes you made in your code and answer your questions.

We didn't see your attempts to solve this problem yourself and so we cannot correct mistakes you didn't made and answer questions you didn't ask. To get help you should do something yourself and get real problems with something. If your problem is "I don't understand a thing", then you should go back to basics and study again.


You have a rather clear list of things that have to be within main().
This is what I have so far... I'm not getting the result that is needed and I'm getting an error on line 16 or which I don't know why...

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

class Student
{
     private:
     float GPA;          
                 
     public:                  
    Student (float = 1);
    int computeGPA();              
};

Student::Student(float = 1)    
{
     gpa = GPA;
}

int Student::computeGPA()    
{
return (GPA);
}


int main()
{
vector<Student> h;                    
    h.push_back(Student(88));     
    h.push_back(Student(59)); 
    h.push_back(Student(90));
    h.push_back(Student(85));

 vector<Student>::iterator it;       

 cout << endl;

    for (it = h.begin(); it != h.end(); ++it)
    {
    cout << (*it).computeGPA() << endl;
    //cout << it->computeGPA()   << endl;    

    }

   cout << endl;
    it = h.begin();   
    *it = Student(88);      
    cout << endl;

   for (it = h.begin(); it != h.end(); ++it)
   {
   cout << (*it).computeGPA() << endl;
   //cout << it->computeGPA()   << endl;    
   }

   h.insert(h.begin(), Student(100));  
    cout << endl;

   for (it = h.begin(); it != h.end(); ++it)
   {

         cout << (*it).computeGPA() << endl;
         //cout << it->computeGPA()   << endl;  

    }


 return 0;

}
Please use code tags. But anyway,

1. You need to only declare a default arguement once. You already did it in your function declaration so don't do it again in the definition.

2. You never name the variable that your passing into your function definition:
Student::Student(float = 1)
should be float variable

3. Check the order of the assignment in that function. (What is it actually doing?)
Last edited on
Topic archived. No new replies allowed.