A program to declare a class student whose data members are student name,marks of 3 subject,total and average and two static data members pass and fail and members functions are-
1 get()to get student name and marks of three subject
2 calc() to Calculate total and avg and also calculate how many student are pass and fail (avg>=40,pass otherwise fail)
3 disp()to display all data members except pass and fail
4 result ()to display total no.of pass and fail student it is a static function
In main(),get details 5 students
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Student {
string name;
int marksEnglish;
int marksMath;
int marksScince;
int total;
double average;
static int pass;
static int fail;
public:
Student(string name);
void get();
void calc();
void disp();
static void result();
};
int Student::pass = 0;
int Student::fail = 0;
Student::Student(string name) {
this->name = name;
marksEnglish = marksMath = marksScince = total = 0;
average = 0.0;
}
void Student::get() {
cout << "Enter marks for " << name << endl;
cout << "English: ";
cin >> marksEnglish;
cout << "Math: ";
cin >> marksMath;
cout << "Science: ";
cin >> marksScince;
}
void Student::calc() {
total = marksEnglish + marksMath + marksScince;
average = total / 3.0;
if (average >= 40) {
pass++;
}
else {
fail++;
}
}
void Student::disp() {
cout << "Results of student " << name << endl;
cout << "English: " << marksEnglish << endl;
cout << "Math: " << marksMath << endl;
cout << "Science: " << marksScince << endl;
cout << "Totla: " << total << endl;
cout << "Average: " << fixed << setprecision(2) << average << endl;
}
void Student::result() {
int n = pass + fail;
double pr_pass = static_cast<double>(pass) / n * 100;
double pr_fail = static_cast<double>(fail) / n * 100;
cout << fixed << setprecision(2) << pr_pass << "% students pass; "
<< setprecision(2) << pr_fail << "% students fail" << endl;
}
int main() {
Student students[5] = { Student("John Snow"),
Student("Adam Smith"),
Student("Mary Brown"),
Student("Alice Bloom"),
Student("Bill Gates") };
for (int i=0; i<5; i++) {
students[i].get();
cout << endl;
}
for (int i=0; i<5; i++) {
cout << endl;
students[i].calc();
students[i].disp();
}
Student::result();
return 0;
}
Comments
Leave a comment