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.
#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();
}
Comments
Leave a comment