Create a class Student which stores the details about roll no, name, marks of 5 subjects i.e. Science, Mathematics, English, C and C++.
The class must have the following:
- Get function to accept the value of data members.
- Display function to display the value of data members.
- Total function to add marks of all 5 subjects and store it in the data member named total.
First, we need to define a Student class which will have 5 data members for subjects and two additional data members to store the roll number and the total. Apart from that, we will also declare a character type array to store the name of the student.
Now as per the program definition, we need to define a member function called Get() which will input the data from the user such as roll number, name and subject marks.
Next, we will define another member function called Display() which will display the results or you can say the marksheet based on the accepted data and calculated total or grade.
Here’s one more thing that we need to do, and that is defining another member function called Total() which will calculate the total marks by adding up the values stored in data members declared for 5 subjects.
Finally, we will create an object of the class and will call the methods in following order: Get(), Total() and Display().
Tutorial 📺
Code 💻
#include<iostream.h>
#include<conio.h>
class Student
{
int rollno, science, maths, english, c, cpp, total;
char name[20];
public:
void Get()
{
cout <<"\n\nPlease enter your rollno.: ";
cin >> rollno;
cout << "\n\nPlease enter your name: ";
cin >> name;
cout << "\n\nPlease enter marks for Science: ";
cin>> science;
cout << "\n\nPlease enter marks for Maths: ";
cin >> maths;
cout << "\n\nPlease enter marks for English: ";
cin >> english;
cout << "\n\nPlease enter marks for C: ";
cin >> c;
cout << "\n\nPlease enter marks for C++: ";
cin >> cpp;
}
void Display()
{
clrscr();
cout << "\t\tMarks" << endl;
cout << "\nRoll no.: "<<rollno;
cout << "\nName: "<<name;
cout << "\n\nMarks in Science: "<<science;
cout << "\nMarks in Mathematics: "<<maths;
cout << "\nMarks in English: "<<english;
cout << "\nMarks in C: "<<c;
cout << "\nMarks in C++: "<<cpp;
cout << "\n\nTotal marks: "<<total;
}
void Total()
{
total = science + maths + english + c + cpp;
}
};//class ends here
void main()
{
clrscr();
Student s;
s.Get();
s.Total();
s.Display();
getch();
}


Comments