Introduction:
In this C++ tutorial, we will learn how to read the details of students and how to print them using a Class. We will use one class to hold the name and marks of each student and two member functions to read and print these details.
I am explaining the steps at the end of this article. You can go through the steps to learn how the program works.
C++ program to record and print student details:
The following program stores the name and marks of any number of students and prints them. It reads the values as inputs from the user.
#include <iostream>
#include <string>
using namespace std;
// 1
class Student
{
private:
string name;
int marks;
public:
void getDetails();
void setDetails();
};
// 2
void Student::setDetails()
{
cout << "Enter the name:" << endl;
cin >> name;
cout << "Enter total marks:" << endl;
cin >> marks;
}
// 3
void Student::getDetails()
{
cout << "Name: " << name << " ,marks: " << marks << endl;
}
int main(int argc, char const *argv[])
{
// 4
int count;
cout << "Enter the total number of students: ";
cin >> count;
// 5
if (count > 0)
{
// 6
Student studentArray[count];
for (int i = 0; i < count; i++)
{
cout << "For student " << i + 1 << ":" << endl;
studentArray[i].setDetails();
}
// 7
cout << "\nYou have entered:" << endl;
for (int i = 0; i < count; i++)
{
studentArray[i].getDetails();
}
}
else
{
cout << "Please enter a valid number." << endl;
}
return 0;
}Download it on GitHub
Explanation :
The commented numbers in the above program denote the step numbers below:
-
The above example is using a class,
Student, with two private variables and two public methods. The variablenameandmarksare used to store the name and marks of a student and the methodsgetDetailsandsetDetailsare used to get the details and assign values to the private variables. -
setDetailsis a member function to assign values to the variablenameandmarks. If you call this function, it will ask the user to enter the name and marks. Withcin, we are assigning these values to the variablenameandmarks. -
getDetailsmember function is used to print the details. It prints thenameandmarksfor that class object. -
The
mainfunction is the starting point of the program. It asks the user to enter the total number of students. This value is assigned to thecountvariable. -
Check if the entered value is greater than 0 or not. If not, print one message to enter a valid number.
-
If the entered value is greater than 0, create one array of
Studentto hold the names and marks of the students. By using oneforloop, we are assigning the values to the variables of the class objects. It calls thesetDetailsfunction of aStudentobject to read these values. -
It uses one more
forloop to print the user-entered values. We are using thegetDetailsmethod to print the name and marks.
Sample output:
If you run the above program, it will print outputs as below:
Example 1:
Enter the count of students : 4
For student 1 :
Enter the name :
Alex
Enter total marks :
88
For student 2 :
Enter the name :
Bob
Enter total marks :
77
For student 3 :
Enter the name :
Charlie
Enter total marks :
66
For student 4 :
Enter the name :
Dan
Enter total marks :
59
You have entered :
Name : Alex ,marks : 88
Name : Bob ,marks : 77
Name : Charlie ,marks : 66
Name : Dan ,marks : 59Example 2:
Enter the count of students : -3
Please enter a valid number.Conclusion:
We are using the Student class to hold only the name and marks of a student. You can extend this program to hold any other information as well. Similarly, you can add more member functions to the class.
You might also like:
- C++ program to find the total number of characters in a string
- C++ isblank function explanation with example
- C++ program to check if a character is a punctuation using ispunct
- C++ program to check if a character is a hexadecimal using isxdigit
- C++ check if a character is alphabetic using isalpha
- C++ program to pass a structure to a function
