Tuesday, August 1, 2017

C++ program to create student class

This C++ program will create a student class. Program will read and print details of N students . 


#include <iostream>
using namespace std;

class student
{
  private:
        int   rollno;
        char  name[20];
        float percentage;
  public:
    //member function to read details
    void readDetails(){
    cout << "Enter Roll number:";
    cin >> rollno;
   cout << "Enter Name:";
   cin >> name;
   cout << "Enter Percentage:";
   cin >> percentage;
     }
    //member function to display details
    void printDetails(){
    cout << "Student details:\n";
    cout << "RollNo=" << rollno;
cout << ", Name=" << name;
cout << ", Percentage=" << percentage;    
    }
};

int main()
{
    student std[5];    //array of students object
    int n, i;
     
    cout << "Enter total number of students: ";
    cin >> n;
     
    for(i=0; i<n; i++){
        cout << "Enter details of student " << i+1 << ":\n";
        std[i].readDetails();
    }
    
    for(i=0; i<n; i++){
        cout << "\nDetails of student " << i+1 << ":\n";
        std[i].printDetails();
    }
    return 0;
}

Output of program

Enter total number of students: 2
Enter details of student 1:
Enter Roll number:101
Enter Name:AAA
Enter Percentage:85.5
Enter details of student 2:
Enter Roll number:102
Enter Name:BBB
Enter Percentage:90.55

Details of student 1:
Student details:
RollNo=101, Name=AAA, Percentage=85.5

Details of student 2:
Student details:
RollNo=102, Name=BBB, Percentage=90.55


No comments:

Post a Comment