This blog is about providing theory as well as simple executable codes of different programming languages such as java, C, C++, and web programming, etc. This blog will be helpful to the IT students to learn about programming.

Tuesday, March 14, 2023

Create a class named Student with six data members (name, roll, and marks in Math, English and Programming, total_marks). Write a program with member functions: one that initializes necessary data members, another one that calculate total marks and another one that displays student's detail. In main(), create two objects of class Student, for each object, invoke the functions defined above in the class.

 Here's the program that creates a class named Student and its member functions to initialize data members, calculate total marks and display student's details:

#include <iostream>
#include <string>
using namespace std;
class Student {
    private:
        string name;
        int roll, math_marks, english_marks, programming_marks, total_marks;
    
    public:
        void set_data() {
            cout << "Enter student's name: ";
            cin >> name;
            cout << "Enter student's roll number: ";
            cin >> roll;
            cout << "Enter marks in Math: ";
            cin >> math_marks;
            cout << "Enter marks in English: ";
            cin >> english_marks;
            cout << "Enter marks in Programming: ";
            cin >> programming_marks;
        }
    
        void calculate_total_marks() {
            total_marks = math_marks + english_marks + programming_marks;
        }
    
        void display_details() {
            cout << "Name: " << name << endl;
            cout << "Roll Number: " << roll << endl;
            cout << "Marks in Math: " << math_marks << endl;
            cout << "Marks in English: " << english_marks << endl;
            cout << "Marks in Programming: " << programming_marks << endl;
            cout << "Total Marks: " << total_marks << endl;
        }
};
int main() {
    Student s1, s2;
    
    cout << "Enter details of Student 1:" << endl;
    s1.set_data();
    s1.calculate_total_marks();
    
    cout << endl << "Enter details of Student 2:" << endl;
    s2.set_data();
    s2.calculate_total_marks();
    
    cout << endl << "Details of Student 1:" << endl;
    s1.display_details();
    
    cout << endl << "Details of Student 2:" << endl;
    s2.display_details();
    
    return 0;
}

Explanation:

  • The class Student is defined with data members: name, roll, math_marks, english_marks, programming_marks, total_marks.
  • The member function set_data() is used to initialize the necessary data members (name, roll, and marks in Math, English and Programming) for each object.
  • The member function calculate_total_marks() is used to calculate the total marks of each student object.
  • The member function display_details() is used to display the details of each student object (name, roll, and marks in Math, English and Programming, and total marks).
  • In the main() function, two objects s1 and s2 of class Student are created and their data members are initialized by invoking the set_data() function. Then, the calculate_total_marks() function is called to calculate the total marks of each student. 
  • Finally, the display_details() function is used to display the details of each student object.

No comments:

Post a Comment

If you have any doubts, please let me know

Slider Widget