Answer to Question #197535 in C++ for Asif

Question #197535

Write a program to define a class to input the name of a student and marks of three subjects

(s1, s2, s3), calculate the total marks and average marks in a member function of a class and also

display the output in another member function of a class. Each subject has a maximum of 100

marks.


1
Expert's answer
2021-05-23T16:22:01-0400
#include<iostream>
#include<string>
using namespace std;
class student
{
    string name;
    float s1,s2,s3;
public:
    student()
    {
        cout<<"Enter the name of student : ";
        getline(cin,name);
        s1m:
            cout<<"Enter marks in first subject : ";
            cin>>s1;
            if(s1>100)
                {
                    cout<<"Invalid input marks should be less than 100 ";
                    goto s1m;
                }
        s2m:
            cout<<"Enter marks in second subject : ";
            cin>>s2;
            if(s2>100)
                {
                    cout<<"Invalid input marks should be less than 100 ";
                    goto s2m;
                }
        s3m:
            cout<<"Enter marks in third subject : ";
            cin>>s3;
            if(s3>100)
                {
                    cout<<"Invalid input marks should be less than 100 ";
                    goto s3m;
                }
    }
    float total_marks()
    {
        float total;
        total=s1+s2+s3;
        return total;
    }
    float average_marks()
    {
        float average;
        average=(total_marks())/3;
        return average;
    }
    void display()
    {
        cout<<"\nName of student : "<<name;
        cout<<"\nMarks in first subject : "<<s1;
        cout<<"\nMarks in second subject : "<<s2;
        cout<<"\nMarks in third subject : "<<s3;
        cout<<"\nTotal marks = "<<total_marks()<<"/300";
        cout<<"\nAverage marks = "<<average_marks();
    }


};
int main()
{
    student s1;
    s1.display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Assignment Expert
24.05.21, 14:47

Dear Asif , please post a new question



Asif
24.05.21, 08:38

Write a program to initialize the data members through passing two parameters using a constructor. Calculate their sum and print the output on the screen using a separate member function of a class.

Leave a comment

LATEST TUTORIALS
New on Blog