Read and Print Student Information Class Example Program In C++


Class and Object Definition

  • A class is a blueprint, or prototype which defines and describes the member attributes and member functions.
  • Objects are the instances of a class. Simply speaking, it may be defined as a variable of user-defined datatype classes. Through an object or instance, all member variables and member functions of classes can be accessed.

For a better understanding,

Class Syntax

class Classname {
    access_specifier:
    member_varibales;
    member_functions;
}object1, object2;

Read and Print Student Information Class Example Program

/* Read and Print Student Information Class Example Program In C++ */
/* Class C++ Programs, Understanding Class,C++ Examples */

// Header Files
#include <iostream>
#include<conio.h>

using namespace std;

// Student Class Declaration
class StudentClass {
private://Access - Specifier
   //Member Variable Declaration
   char name[20];
   int regNo, sub1, sub2, sub3;
   float total, avg;

public://Access - Specifier
   //Member Functions read() and print() Declaration

   void read() {
      //Get Input Values For Object Variables
      cout << "Enter Name :";
      cin >> name;

      cout << "Enter Registration Number :";
      cin >> regNo;

      cout << "Enter Marks for Subject 1,2 and 3 :";
      cin >> sub1 >> sub2>> sub3;
   }

   void sum() {
      total = sub1 + sub2 + sub3;
      avg = total / 3;
   }

   void print() {
      //Show the Output
      cout << "Name :" << name << endl;
      cout << "Registration Number :" << regNo << endl;
      cout << "Marks :" << sub1 << " , " << sub2 << " , " << sub3 << endl;
      cout << "Total :" << total << endl;
      cout << "Average :" << avg << endl;
   }
};

int main() {
   // Object Creation For Class
   StudentClass stu1, stu2;

   cout << "Read and Print Student Information Class Example Program In C++\n";

   cout << "\nStudentClass : Student 1" << endl;
   stu1.read();
   stu1.sum();
   stu1.print();

   cout << "\nStudentClass : Student 2" << endl;
   stu2.read();
   stu2.sum();
   stu2.print();

   getch();
   return 0;
}

Sample Output

Read and Print Student Information Class Example Program In C++

StudentClass : Student 1
Enter Name :Mask
Enter Registration Number :10001
Enter Marks for Subject 1,2 and 3 :90
80
65
Name :Mask
Registration Number :10001
Marks :90 , 80 , 65
Total :235
Average :78.3333

StudentClass : Student 2
Enter Name :Operas
Enter Registration Number :10002
Enter Marks for Subject 1,2 and 3 :95
85
70
Name :Operas
Registration Number :10002
Marks :95 , 85 , 70
Total :250
Average :83.3333