4. . Write a program by creating a class named 'Student' having two methods.
Perform the following:
a. First method named as 'readDetails()‟ This method should accept the following details from the
user:
Student's name (string)
Year of admission (string)
Student's marks (float)
Address (string)
b. Second method „displayDetails()‟ that would print the information (name, year of admission,
marks, address) of the students as follows:
Student Information:
Name: [Student's Name]
Year of Admission: [Year of Admission]
Marks: [Student's Marks]
Address: [Student's Address]
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
string yearOfAdmission;
float marks;
string address;
// Method to read student details
void readDetails() {
cout << "Enter Student's Name: ";
cin.ignore(); // Clear input buffer
getline(cin, name);
cout << "Enter Year of Admission: ";
getline(cin, yearOfAdmission);
cout << "Enter Student's Marks: ";
cin >> marks;
cin.ignore(); // Clear input buffer
cout << "Enter Student's Address: ";
getline(cin, address);
}
// Method to display student details
void displayDetails() {
cout << "\nStudent Information:\n";
cout << "Name: " << name << endl;
cout << "Year of Admission: " << yearOfAdmission << endl;
cout << "Marks: " << marks << endl;
cout << "Address: " << address << endl;
}
};
int main() {
Student student; // Create a Student object
student.readDetails(); // Read student details
student.displayDetails(); // Display student details
return 0;
}
OUTPUT:
Enter Student's Name: John Doe
Enter Year of Admission: 2020
Enter Student's Marks: 85.5
Enter Student's Address: 123 Main St
Student Information:
Name: John Doe
Year of Admission: 2020
Marks: 85.5
Address: 123 Main St