Answer to Question #174674 in C++ for zain ul abdeen

Question #174674

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.


1
Expert's answer
2021-03-28T05:39:33-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS