CS Electrical And Electronics
@cselectricalandelectronics

Write a Cpp program to read and display student name, roll no, total marks and calculate percentage by creating objects and class?

All QuestionsCategory: Cpp LanguageWrite a Cpp program to read and display student name, roll no, total marks and calculate percentage by creating objects and class?
CS Electrical And Electronics Staff asked 4 years ago

I need short information.

1 Answers
CS Electrical And Electronics Staff answered 4 years ago

Code:

#include <iostream>
using namespace std;
#define MAX 10
class student
{
private:
char name[30];
int rollNo;
int total;
float perc;
public:
//member function to get student’s details
void getDetails(void);
//member function to print student’s details
void putDetails(void);
};
//member function definition, outside of the class
void student::getDetails(void){
cout << “Enter name: ” ;
cin >> name;
cout << “Enter roll number: “;
cin >> rollNo;
cout << “Enter total marks out of 500: “;
cin >> total;
perc=(float)total/500*100;
}
//member function definition, outside of the class
void student::putDetails(void){
cout << “Student details:\n”;
cout << “Name:”<< name << “,Roll Number:” << rollNo << “,Total:” << total << “,Percentage:” << perc;
}
int main()
{
student std[MAX]; //array of objects creation
int n,loop;
cout << “Enter total number of students: “;
cin >> n;
for(loop=0;loop< n; loop++){
cout << “Enter details of student ” << loop+1 << “:\n”;
std[loop].getDetails();
}
cout << endl;
for(loop=0;loop< n; loop++){
cout << “Details of student ” << (loop+1) << “:\n”;
std[loop].putDetails();
}
return 0;
}

Output:

Enter total number of students: 2
Enter details of student 1:
Enter name: Chetanshidling
Enter roll number: 135
Enter total marks out of 500: 400
Enter details of student 2:
Enter name: Megha
Enter roll number: 134
Enter total marks out of 500: 450
Details of student 1:
Student details:
Name:Chetanshidling ,Roll Number:135 ,Total:400 Percentage:80
Details of student 2:
Student details:
Name:Megha ,Roll Number:134 ,Total:450 Percentage:90