I'm going to learn classes and objects. Could you please translate the code below into the one which uses concept of classes and objects? It would be kind of you. Thank you.
// student_data_read_practice.cpp
// program which reads data of some students
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
/////////////////////////////////////////////////////
struct Date {int d; string m; int y;};
////////////////////////////////////////////////////
/////////////////////////////////////////////////////
struct Student {int rollno; string sex; string name; float gpa; Date DoB;};
/////////////////////////////////////////////////////
Student read();
void prnt(Student dummystud);
char grade(float dummyGpa);
int main()
{
Student stud[5];
for(int i=0; i<5; i++)
{
cout << "enter student #" << (i+1) << "'s details below:-" << endl;
stud[i] = read();
prnt(stud[i]);
cout << endl << endl;
}
system("pause");
return 0;
}
//-----------------------------------------------
// Student read() definition
Student read()
{
Student stud;
cout << "enter name: "; getline(cin, stud.name);
cout << "enter roll number: "; cin >> stud.rollno;
cout << "enter sex: "; cin >> stud.sex;
cout << "enter date of birth (e.g. 01 Jan 2000) below:-" << endl;
cout << "enter day: "; cin >> stud.DoB.d;
cout << "enter month: "; cin >> stud.DoB.m;
cout << "enter year: "; cin >> stud.DoB.y;
cout << "enter GPA: "; cin >> stud.gpa;
cin.ignore();
return stud;
}
//-------------------------------------------------
// void prnt() definition
void prnt (Student dummystud)
{
cout << "\n\n\n************************************\n\n\n";
cout << "roll no.: " << dummystud.rollno << endl;
cout << "name: " << dummystud.name << endl;
cout << "sex: " << dummystud.sex << endl;
cout << "date of birth: " << dummystud.DoB.d << "-" << dummystud.DoB.m << "-" << dummystud.DoB.y << endl;
cout << "grade: " << grade(dummystud.gpa) << endl;
cout << "\n\n\n************************************";
}
//-------------------------------------------------------------
// grade() definition
char grade(float dummyGpa)
{
if (dummyGpa >= 3.5)
return 'A';
else if (dummyGpa >= 3.0)
return 'B';
else if (dummyGpa >= 2.0)
return 'C';
else
return 'D';
}
//----------------------------------------------------


Sign In
Create Account


Back to top









