0% found this document useful (0 votes)
9 views2 pages

Student Class for Details Input/Output

The document describes a C++ program that defines a 'Student' class with two methods: 'readDetails()' for inputting student information and 'displayDetails()' for printing that information. The program prompts the user for the student's name, year of admission, marks, and address, and then displays the collected details in a formatted manner. An example output is provided, showing how the program operates with sample input.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Student Class for Details Input/Output

The document describes a C++ program that defines a 'Student' class with two methods: 'readDetails()' for inputting student information and 'displayDetails()' for printing that information. The program prompts the user for the student's name, year of admission, marks, and address, and then displays the collected details in a formatted manner. An example output is provided, showing how the program operates with sample input.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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: ";
[Link](); // Clear input buffer
getline(cin, name);

cout << "Enter Year of Admission: ";


getline(cin, yearOfAdmission);

cout << "Enter Student's Marks: ";


cin >> marks;
[Link](); // 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

[Link](); // Read student details


[Link](); // 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

Common questions

Powered by AI

The output will be: Student Information: Name: Alice Smith Year of Admission: 2019 Marks: 90.7 Address: 456 Elm St. This reflects the use of 'cout' to display each data member in a formatted manner .

Using public access modifiers makes data and methods easily accessible, simplifying usage and testing. However, it risks exposing internal data, potential unintended modification, and reduced encapsulation, impacting data integrity and privacy .

An improvement could involve adding a loop and input validation to ensure only valid float inputs are accepted. This could be done by checking the stream's failure state using 'cin.fail()', clearing the error state with 'cin.clear()', and consuming the incorrect input before retrying .

The primary components include data members for storing student's name, year of admission, marks, and address, and methods for reading and displaying these details. The 'readDetails()' method is used to input the student information, and 'displayDetails()' is utilized to output this information in a structured format .

Omitting 'cin.ignore()' could lead to input issues, where 'getline()' immediately reads a leftover newline character from the input buffer, resulting in an empty string being captured instead of the intended input, such as the student's name or address .

Using 'getline()' for inputs like the student's name and address allows the program to read an entire line, including spaces, as a single string. This is crucial when input contains spaces, such as a full name or address. In contrast, 'cin' would only read up to the first space .

The example separates concerns by having distinct methods for reading ('readDetails()') and displaying ('displayDetails()') student information. This separation simplifies maintenance, allows reuse of methods, and enhances code clarity and organization .

Float is chosen for storing a student's marks to accommodate decimal values, reflecting more precise scores like 85.5. Potential limitations include limited precision and rounding errors, which could affect calculations and display of marks with many decimal places .

The program uses 'cin.ignore()' before reading the student's name and address to clear the input buffer. This is necessary because 'cin' does not consume the newline character left in the buffer after reading other types, which may interfere with subsequent 'getline()' calls .

Encapsulation is demonstrated by grouping related data (student's name, year, marks, address) and methods (readDetails, displayDetails) into a single class, 'Student'. This enables the controlled interaction through class methods, promoting data protection and integrity .

You might also like