First hello to everyone.
I'm new to this forum and also to C++ programming language.
I need help with a task that may look very easy to you, but I've just started learning C++ and I haven't worked with classes yet.
So the task is:
---Create a class named Student with name, surname, index number, average of 5 marks, and a function that will print them in order (name,surname,index no.,average).
In the main function I need to create 5 students with the above stated data (entered using keyboard *cin>>*) and after closing the application those 5 students and their information need to be written in .txt file.
Yes, but creating the 5 students and adding the data is problem . Maybe I should use for(); to create them but I don't know how. Really need this solved. It should look like this
1 2 3 4 5 6 7 8 9 10 11 12
class Student{
public: string ime;//name
public: string prezime;//surname
public: int index;//number of index
public: float prosek;//average
public: int oceni[5];//marks
public: void pecati();//function for printing in correct order
};
void Student::pecati(){
cout<<ime<<":"<<prezime<<":"<<index<<prosek<<endl;
}
Creating the 5 students will be as simple as creating an array or vector of that class in your main function.
Assuming that you probably won't have encountered STL much (generally I find it's taught after classes at some point), I'll give you an array example.
1 2 3 4
// Assuming I've made a class named 'foo'
constint MAX_ITEMS = 5;
foo[MAX_ITEMS];
Also, you don't have to prefix each of those members with public. This will do:
1 2 3 4 5 6
class foo
{
public:
string str;
int num;
};
Finally, it's generally good practice of encapsulation to make member variables of a class private and use accessor methods to retrieve or alter values. What you've essentially done in the above example is create a struct under the guise of a class.