-1
\$\begingroup\$

Im making this project from few days , and now i need little help because i am struggling. The task says

II. Define a student booklet class CStudBook that inherits CStudent with a member data container of the student's grades. To implement a constructor with a filename parameter from which to initialize container and the following methods:

Create an object using another object (copy constructor)
Creating an object using explicit parameters
Creating an object by default constructor
Calculate and return the student's grade point average
operator>>
operator<<
Print

I created almost everything , but i cannot find a way to output the student grade from my 2nd class into the console. I will drop the code.

#include <iostream> 
#include <string> 
#include <vector> 
#include <fstream>
#include <algorithm>

using namespace std;

class CStudent {
public:
    // Default constructor
    CStudent() = default;

    // Explicit constructor
    CStudent(string name, int facnum, string specialty) {
        this->name = name;
        this->facnum = facnum;
        this->specialty = specialty;
    }

    // Copy constructor
    CStudent(const CStudent& obj) {
        this->name = obj.name;
        this->facnum = obj.facnum;
        this->specialty = obj.specialty;
    }

    // Method, that prints the contents of the object
    void PrintStudent() const {
        cout << "Name: " << name << endl;
        cout << "Fac. num: " << facnum << endl;
        cout << "Specialty: " << specialty << endl;
    }
   
    friend std::ostream& operator <<(std::ostream& out, const CStudent& obj) // output operator
    {
        out << "Name:" << obj.name << "-> FacNumber: " << obj.facnum << std::endl;

    }

  

    bool operator <(const CStudent& obj) // operator for comparison of two objects
    {
        return name < obj.name;
        return facnum < obj.facnum;
        
    }

    friend std::istream& operator>>(std::istream& in, CStudent& obj) // operator za vhod
    {
        in >> obj.name;
        in >> obj.facnum;
        in >> obj.specialty;

     }

    private:
    string name;
    int facnum;
    string specialty;
    
};

class CStudBook : public CStudent {
private:
    int grade;
    
public:
    CStudBook()
    {
    }
 
    CStudBook(int grade)
    {
        this->grade = grade;
    }

    CStudBook(const CStudBook& obj) {

        this->grade = obj.grade;
    }
    void PrintCStudBook() const {
        cout << "Grade of student: " << grade << endl;
         }
 



};
 
  















// Function, that gets student data from the console
// and creates an instance of the CStudent class using this data
CStudent CreateFromConsole() {
    cout << "Enter name: ";
    string name;
    cin >> name;

    cout << "Enter fac. num: ";
    int facnum;
    cin >> facnum;

    cout << "Enter specialty: ";
    string specialty;
    cin >> specialty;

    cout << endl;
    return CStudent(name, facnum, specialty); // returns an instance of the class
    

}





int main() {
    vector<CStudent> students; // stack-like container

    cout << "Enter total number of students: ";
    int count;
    cin >> count;

    cout << "Enter details of students" << endl << endl;
    for (int i = 0; i < count; i++) {
        // Create an instance of the class
        CStudent student = CreateFromConsole();
       
        // Push a copy of the object into the container
        students.push_back(student);




    }

    cout << endl;

    cout << "Details" << endl << endl;
    for (int i = 0; i < students.size(); i++) {
        students[i].PrintStudent();
        cout << endl;
    }

    fflush(stdin); // Reset input console 
    getchar(); // Pause
    return 0;
}


\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
  1. The task is flawed. Inheritance should be "is a" relation, but "Student booklet is a student" is false. LSP will not work here. Of course, it's up to your tutor to teach you, but pay attention to this.
  2. You have PrintStudent() and PrintCStudBook() methods together with operator<<. std::cout<<student; should probably do the same as student.PrintStudent(); so those methods are excessive.
  3. Use class members initialization (via :).
  4. Use const references for class arguments like std::string.
  5. Class members usually have some mark in their name to distinguish them from other variables - like name_ or m_name instead of name.
  6. Task says "member data container of the student's grades" - and you have only single int, not a container.
  7. "To implement a constructor with a filename parameter" - can't find it. Btw this is against SRP.
  8. Now to your question. You should have an object of your second class (CStudBook) to output it; then you should call PrintCStudBook on that object. Probably, you should also add a way to initialize the inherited data members of CStudBook with students data (like the constructor with Student argument).
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Review! Please refrain from answering low-quality questions that are likely to get closed. Once you've answered, that limits what can be done to improve the question, making it more likely that your efforts are wasted. It's better to wait until the question is properly ready before you answer! \$\endgroup\$ Commented Jun 13, 2021 at 12:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.