Define a class student with the following specification. Note for user understanding purposes you
should write comment with each line of code.
Private members of class student
admno integer
sname 20 character
eng. math, science float
total float
ctotal() a function to calculate eng + math + science with float return type.
Public member function of class student
Takedata() Function to accept values for admno, sname, eng, science and invoke
ctotal() to calculate total.
Showdata() Function to display all the data members on the screen.
#include <iostream>
using namespace std;
class Student {
private:
int admno;
char sname[20];
float eng;
float math;
float science;
float total;
float ctotal() {
return eng + math + science;
}
public:
void Takedata(int admno, char sname[20], float eng, float math, float science) {
this->admno = admno;
strcpy(this->sname, sname);
this->eng = eng;
this->math = math;
this->science = science;
this->total = ctotal();
}
void Showdata() {
cout<<"\nAdmno: " <<admno<<"\n";
cout<<"Name: " <<sname<<"\n";
cout<<"Eng: " <<eng <<"\n";
cout<<"Math: " <<math <<"\n";
cout<<"Science: " <<science <<"\n";
cout<<"Total: " <<total <<"\n";
}
};
//The start point of the program
int main(){
Student st;
int admno;
char sname[20];
float eng;
float math;
float science;
cout<<"Enter admno: ";
cin>>admno;
cin.ignore();
cout<<"Enter sname: ";
gets(sname);
cout<<"Enter eng: ";
cin>>eng;
cout<<"Enter math: ";
cin>>math;
cout<<"Enter science: ";
cin>>science;
st.Takedata(admno, sname, eng, math, science);
st.Showdata();
cout<<"\n\n";
system("pause");
return 0;
}
Comments
Hi assignmentexpert.com administrator, Your posts are always well-received by the community.
Leave a comment