Monday, February 4, 2019

A Class Example (C++)

Q. Define a class student with the following specification:

admno                 interger
sname                 20 characters
eng, math, science    float (marks in 3 subjects)
total                 float
ctotal()              A function to calculate
                      eng + math + science marks

Public member fucntions of the class student:

takedata()   function to accept values for admno,
             sname, marks in eng, math, science and
             invoke ctotal() to calculate total.

showdata()   function to display

Give detailed function definitions also.

Solution:

#include <iostream>

using namespace std;

class student{
    int admno;
    char sname[20];
    float eng, math, science;
    float total;
    void ctotal();
  public:
    void takedata();
    void showdata();
};

void student::ctotal(){
    total = eng + math + science;
}

void student::takedata(){
    cout << "Enter Admission Number: ";
    cin >> admno;
    cout << "Enter Name of Student: ";
    cin >> sname;
    cout << "Enter marks in English: ";
    cin >> eng;
    cout << "Enter marks in Mathematics: ";
    cin >> math;
    cout << "Enter marks in Science: ";
    cin >> science;
    ctotal();
}

void student::showdata(){
    cout << "\nAdmission Number: " << admno;
    cout << "\nName of Student: " << sname;
    cout << "\nMarks in English: " << eng;
    cout << "\nMarks in Mathematics: " << math;
    cout << "\nMarks in Science: " << science;
    cout << "\nTotal Marks: " << total;
}


.

No comments: