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;
}