Question #173487

Define a class Student with the following specification:

PRIVATE DATA MEMBERS:

regno integer sname 20 character s1, s2. s3 float total float

PUBLIC MEMBER FUNCTIONS:

Student() Default constructor to initialize data members, s1 = s2 = s3 = 0

Student(rn, sn, m, p, c) Parameterized constructor accept values for regno, sname, s1, s2 and s3

calculate() Function to calculate the student total marks

putdata() Function to display all the data members of student

~student() Destructor to destroy the data objects


Expert's answer

#include <iostream>
#include <string>
#include <iomanip>
 
using namespace std;
 
class Student
{
public:
               // Default constructor
  Student() {
      s1 = 0;
      s2 = 0;
      s3 = 0;
  } 
	// Parameterized constructor
  Student(int no, string name, float m, float p, float c) {
      regono = no;
      sname = name;
      s1 = m;
      s2 = p;
      s3 = c; 
  } 
	// Member functions
  float calculate(){
      total = s1 + s2 + s3;
       return total;
  }
  void putData();
	// destructor. 
  ~Student() {};
 
private:
               int regono;
               string sname;
               float s1;
               float s2;
               float s3;
               float total;
};
 
void Student::putData() {
  cout << "Student number: " << regono << endl;
  cout << "Student name:  " << sname << endl<<endl;
  cout << "s1 mathe:    " <<setw(6)<< fixed << setprecision(2) << s1 << endl;
  cout << "s2 physics:  " <<setw(6)<< fixed << setprecision(2) <<s2 << endl;
  cout << "s3 chemistry: " <<setw(6)<< fixed << setprecision(2) <<s3 << endl;
  cout << "total:       " <<setw(6)<< fixed << setprecision(2) <<calculate() << endl;
  cout << "==============================="<<endl;
}
 
int main() {
  Student st1(123, "Anna Kern", 80.25, 50, 90.45); //Parameterized constructor
  st1.putData();
  return 0;
}

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!

LATEST TUTORIALS
APPROVED BY CLIENTS