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

Question #174852

make a 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-29T18:46:32-0400
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


class Student
{
public:
    Student() 
	{
		cout << "Enter student name: " << endl;
		cin >> name;
		cout << "Enter student roll no: " << endl;
		cin >> roll_no;
		cout << "Enter student GPA: " << endl;
		cin >> gpa;
		cout << "Enter student credit hours: " << endl;
		cin >> credit_hours;
	}
	Student(string file_st) 
	{
		fstream f;
		f.open(file_st, ios::in);
		if (f) {
			f >> name >> roll_no >> gpa >> credit_hours;
        }
		else
			cout << "Error opening the file" << endl;


	}
	~Student () {}
	void save_st(string file_path)
	{ 
		fstream f;
		f.open(file_path,ios::out);
		if (f) {
			f << name<<endl;
			f << roll_no<<endl;
			f << gpa<<endl;
			f << credit_hours<<endl;
		}
		f.close();
	}
	void display() 
	{
		cout << "Name :" << name << endl;
		cout << "Roll no:" << roll_no << endl;
		cout << "GPA:" << gpa << endl;
		cout << "Credit hours:" << credit_hours << endl;
	}
private:
	string name{ "No name" };
	int roll_no{ 0 };
	int gpa{ 0 };
	double credit_hours{ 0 };
};
int main()
//example work
{
	Student s1;
	s1.save_st("test.txt");
	s1.display();
	Student s2("test.txt");
	s2.display();
}

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