C++ Program to Read and Display Student Details using Class

Introduction

In this post, I am going to write a c++ program to read and display student details using class. Actually, I am writing the same program using a number of ways. I am going to use simple member functions, member functions outside the class and constructor and array of objects. Now, let’s see these programs one by one.

C++ Program to Read and Display Student Details using a Simple Member Functions using Class

#include <iostream>
using namespace std;
class student
{
private:
    int rollno;
    char fname[10],mname[10],lname[10];
    char branch[15];
    char city[15];
public:
    void get_data()
    {
        cout<<"Enter Roll Number"<<endl;
        cin>>rollno;
        cout<<"Enter First Name"<<endl;
        cin>>fname;
        cout<<"Enter Middle Name"<<endl;
        cin>>mname;
        cout<<"Enter Last Name"<<endl;
        cin>>lname;
        cout<<"Enter Branch"<<endl;
        cin>>branch;
        cout<<"Enter City"<<endl;
        cin>>city;
    }
    void display()
    {
        cout<<"Roll Number: "<<rollno<<endl;
        cout<<"First Name: "<<fname<<endl;
        cout<<"Middle Name: "<<mname<<endl;
        cout<<"Last Name: "<<lname<<endl;
        cout<<"Branch: "<<branch<<endl;
        cout<<"City: "<<city<<endl;
    }
};
int main()
{
    student s1;
    cout << "Enter student details" << endl;
    s1.get_data();
    cout << "Student Details " << endl;
    s1.display();
    return 0;
}

In the above program, I have declared 6 member variables, out of which five variables are of type character array and one variable is of type int. The access specifier for these variables is private.

Now, in the same, I have declared two member functions which are publicly accessible. One member function get_data() and other function is display(). In the function get_data(), I am reading the student details such as his roll number, his name, branch and city. Now, for displaying the same details, I am using a function i.e. display(). In the main function, I have created only object s1 of class student.

See the following output.

Enter student details
Enter Roll Number
55
Enter First Name
Vijay
Enter Middle Name
Satish
Enter Last Name
Londhe
Enter Branch
CSE
Enter City
Akola
Student Details
Roll Number: 55
First Name: Vijay
Middle Name: Satish
Last Name: Londhe
Branch: CSE
City: Akola

This is a very simple c++ program which is reading student details from the user and display the same on the console. I have defined two member functions inside the class. Now, I am going to write the same c++ program to read and display student details using class by defining these member functions outside the class. For this, we will have to use the scope resolution operator(::).

C++ Program to Read and Display Student Details by Defining Member Functions Outside the Class

#include <iostream>
using namespace std;
class student
{
private:
    int rollno;
    char fname[10],mname[10],lname[10];
    char branch[15];
    char city[15];
public:
    void get_data();
    void display();
};
void student::get_data()
    {
        cout<<"Enter Roll Number"<<endl;
        cin>>rollno;
        cout<<"Enter First Name"<<endl;
        cin>>fname;
        cout<<"Enter Middle Name"<<endl;
        cin>>mname;
        cout<<"Enter Last Name"<<endl;
        cin>>lname;
        cout<<"Enter Branch"<<endl;
        cin>>branch;
        cout<<"Enter City"<<endl;
        cin>>city;
    }
    void student::display()
    {
        cout<<"Roll Number: "<<rollno<<endl;
        cout<<"First Name: "<<fname<<endl;
        cout<<"Middle Name: "<<mname<<endl;
        cout<<"Last Name: "<<lname<<endl;
        cout<<"Branch: "<<branch<<endl;
        cout<<"City: "<<city<<endl;
    }
int main()
{
    student s1;
    cout << "Enter student details" << endl;
    s1.get_data();
    cout << "Student Details " << endl;
    s1.display();
    return 0;
}

In the above program, I have defined member functions get_data() and display() outside the class using scope resolution operator. The output of this program will remain same as the first one.

In the next section, I am going to write the same program using the constructor.

Read and Display Student Details using Default Constructor

#include <iostream>
using namespace std;
class student
{
private:
    int rollno;
    char fname[10],mname[10],lname[10];
    char branch[15];
    char city[15];
public:
    student();  // constructor
    void display();
};
    student::student()
    {
        cout<<"Enter Roll Number"<<endl;
        cin>>rollno;
        cout<<"Enter First Name"<<endl;
        cin>>fname;
        cout<<"Enter Middle Name"<<endl;
        cin>>mname;
        cout<<"Enter Last Name"<<endl;
        cin>>lname;
        cout<<"Enter Branch"<<endl;
        cin>>branch;
        cout<<"Enter City"<<endl;
        cin>>city;
    }
    void student::display()
    {
        cout<<"Roll Number: "<<rollno<<endl;
        cout<<"First Name: "<<fname<<endl;
        cout<<"Middle Name: "<<mname<<endl;
        cout<<"Last Name: "<<lname<<endl;
        cout<<"Branch: "<<branch<<endl;
        cout<<"City: "<<city<<endl;
    }
int main()
{
    cout << "Enter student details" << endl;
    student s1;
    cout << "Student Details " << endl;
    s1.display();
    return 0;
}

In the above program, I have used the constructor. We know, the constructor is a special member function which is having the same name as the class name and it is invoked when the object is created. So, instead of get_data(), I have used the constructor student. There is no need to explicitly call this constructor.

Now, we can write this same program, but there is a one twist and that twist is I am going to use the concept object.

In the next section, I am going to write a c++ program to read the details of 100 students and display the same. So, here I am not going to create 100 objects. I am simply creating array of 100 objects.

C++ Program to Read Details of 100 students and Display the same

#include <iostream>
using namespace std;
class student
{
private:
    int rollno;
    char fname[10],mname[10],lname[10];
    char branch[15];
    char city[15];
public:
    void get_data();
    void display();
};
void student::get_data()
    {
        cout<<"Enter Roll Number"<<endl;
        cin>>rollno;
        cout<<"Enter First Name"<<endl;
        cin>>fname;
        cout<<"Enter Middle Name"<<endl;
        cin>>mname;
        cout<<"Enter Last Name"<<endl;
        cin>>lname;
        cout<<"Enter Branch"<<endl;
        cin>>branch;
        cout<<"Enter City"<<endl;
        cin>>city;
    }
    void student::display()
    {
        cout<<"Roll Number: "<<rollno<<endl;
        cout<<"First Name: "<<fname<<endl;
        cout<<"Middle Name: "<<mname<<endl;
        cout<<"Last Name: "<<lname<<endl;
        cout<<"Branch: "<<branch<<endl;
        cout<<"City: "<<city<<endl;
    }
int main()
{
    student s[100];
    int i;
    cout << "Enter details of 100 Students" << endl;
    for(i=0;i<100;i++)
    {
        s[i].get_data();
    }
    cout << "Details of 100 Students" << endl;
    for(i=0;i<100;i++)
    {
        s[i].display();
    }
    return 0;
}

I hope you have understood all these programs. If you are new to c++ programming, then this will definitely help you. If you have any doubt or difficulties, then please feel free to contact me.

Thank you.

Important C Programs

If you are interested in the c programs, then here is the list.

  1. Switch Case in C Program to Calculate Area of Circle and Triangle
  2. C Language Program to Count the Number of Lowercase Letters in a Text File
  3. Program in C to Replace Capital C with Capital S in a File
  4. C Program to Remove White Spaces and Comments from a File
  5. Perfect Number in C Programming using All Loops
  6. Reverse a Number in C
  7. LCM of Two Numbers in C Programming
  8. GCD of Two Numbers in C Programming
  9. Strong Number in C Programming
  10. C Program to Find the Sum of Cubes of Elements in an Array

Leave a Comment