Tuesday, February 10, 2015

Student Deatils Using Object

Here is the code for calculating the total score of a student. It focuses on the object orientated approach.
Turbo C++ users should read this (click here) first.

Code

#include <iostream>
using namespace std;

class student
{
    private:
        char name[50];
        int rollno;
        float score[6], total_score;
       
    public:
        void input();
        void calculate();
        void output();
};

void student :: input()
{
    cout << "Enter the name of the student: ";
    cin >> name;
    cout << "Enter the roll no. of the student: ";
    cin >> rollno;
    cout << "Enter the scores for six subjects one by one:\n";
    for(int n = 0; n < 6; n++)
    {
        cin >> score[n];
    }
}

void student :: calculate()
{
    total_score = 0;
    for(int n = 0; n < 6; n++)
        total_score += score[n];
}

void student :: output()
{
    cout << "\n\nStudent Details:\n"
         << "\nName    : " << name
         << "\nRoll No.: " << rollno
         << "\nTotal Score   : " << total_score
         << endl;
}
int main()
{
    student stud;
    stud.input();
    stud.calculate();
    stud.output();
   
    return 0;
}


Output

Enter the name of the student: Example
Enter the roll no. of the student: 30
Enter the scores for six subjects one by one:
90 92 89 94 86 90


Student Details:

Name    : Example
Roll No.: 30
Total Score   : 541

No comments:

Post a Comment