samedi 25 avril 2020

How do you use, and pass arguments into function that use variables and lists declared in a class in c++?

Ok, so, I know how to use java classes, but there is something about c++ classes that I don't get. My professor wants us to be able to add the name of a classes a student is taking and its grade to a stl list of Records that is in a Class of Student and then in other functions traverse and print out the information. I know how to use structs and functions and even classes from other languages, but how do I declare and then use them in c++? I have currently created what I have below from the basic model he gave us, but for example, I added the list & head to the printRecords() function deceleration in the class student, but I don't know if I do that. and how do I truly pass the list into a function, or in my function addClass(), actually add the information to the list? I didn't add the list & head to the function definition of addClass() and I am just trying to use it by its list name of st_records and it isn't giving me any errors when I build it, but, that seems odd to me since it is listed under private in the class deceleration... is it because it is still within the header file?

//////// This is my header file copied
#ifndef _BIKES_H_ 
#define _BIKES_H_
#include <fstream>
#include <string>
#include <iostream>
#include <list>

using namespace std;

struct Record 
{ 
    string class_name;
    char grade; 
};

class Student 
{ 
    public:
        Student(string & name); 
        void printRecords(list<Record> & head);
        char gradeForClass(string & cl_name, list<Record>& head);
        string standing(int); 
        void addClass(string &, char); 
    private: 
        int numClassesTaken; 
        string st_name; 
        std::list<Record> st_records; 
};

/*********************** * Student::printRecords() 
* When called from the main(), this function prints the entire Student's record: 
* classes taken along with their grades. 
*/ 
void Student::printRecords(list<Record>& head) 
{ 
    for (auto it = head.begin(); it != head.end(); it++)
    {
        cout << "Class: " << it->class_name << " Grade: " << it->grade;
    }
}
/*********************** 
* Student::standing() 
* When called from the main(), this function should output: 
* "Freshman", "Sophomore", "Junior", "Senior", 
* depending on the number of classes successfully(!) taken. 
* The exact implementation (in the part of 
* how many classes needed to advance to the next level) is up to you. 
* But this function should not iterate over the list of Records, or call std::list's 
* size() to determine the number of classes. You have a variable for that in
* the Student's class. 
* Note that this function returns string by value. 
*/ 
string Student::standing(int num) 
{ 
    int classes = num / 4;
    string ret = "";

    switch (classes)
    {
    case 0: ret = "Freshman"; break;
    case 1: ret = "Sophmore"; break;
    case 2: ret = "Junior"; break;
    case 3: ret = "Senior"; break;
    default: ret = "Not a normal path"; break;
    }
    return ret;
}

/*********************** 
* Student::gradeForClass() 
* Parameter: A class name. 
* Return type: a single character of grade. 
* Behavior: 
* When called from the main(), this function shall find a class by its name 
* in the Student's Records and output the grade for that class. 
* In case there is no such class in the Records, you shall output the value, 
* somehow reflecting that. 
*/ 
char Student::gradeForClass(string & cl_name, list<Record>& head)
{ 
    for (auto it = head.begin(); it != head.end(); it++)
    {
        if (it->class_name.compare(cl_name) == 0)
        {
            return it->grade;
        }
    }
}

/*********************** 
* Student::addClass() 
* When called from the main(), this function creates a new Record and adds that to 
* the list of Student's Records. It also increments the numClassesTaken, 
* which will be used to determine a Student's standing. 
*/ 
void Student::addClass(string & cl_name, char  grade_)
{ 
    Record* r = new Record();
    r->class_name = cl_name;
    r->grade = grade_;

    st_records.push_back(*r);
}
#endif

Aucun commentaire:

Enregistrer un commentaire