Similar to previous, make Student class with 4 data members (name, roll no, GPA, credit hours) and 2 constructors (one user input, one file input) and a destructor.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Student
{
public:
Student(string fileName); // file input
Student();
string GetName() { return name; }
string GetRoll() { return roll; }
int GetGPA() { return GPA; }
double GetCreditHours() { return creditHours; }
private:
string name;
string roll;
int GPA;
double creditHours;
};
Student::Student()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll: ";
cin >> roll;
cout << "Enter GPA: ";
cin >> GPA;
cout << "Enter credit hours: ";
cin >> creditHours;
}
Student::Student(string fileName)
{
fstream fin;
fin.open(fileName);
if (!fin)
{
cout << "File not found!" << endl;
return;
}
fin >> name >> roll >> GPA >> creditHours;
}
int main()
{
Student Mike;
cout << "Name is: " << Mike.GetName() << endl;
cout << "Roll is: " << Mike.GetRoll() << endl;
cout << "GPA is: " << Mike.GetGPA() << endl;
cout << "Credit hours is: " << Mike.GetCreditHours() << endl << endl;
return 0;
}
Comments
Leave a comment