设计一个类student
private:
admno int
sname 20 char
eng,math,science float
total float
ctotal() 计算eng+math+science with float return type
public:
takedata() function to accept value for all the value above
showdata() function to display all the data members
/*
# 一道C++的类与对象问题 5Cbuy
设计一个类student
private:
admno int
sname 20 char
eng,math,science float
total float
ctotal() 计算eng+math+science with float return type
public:
takedata() function to accept value for all the value above
showdata() function to display all the data members
*/
#include <iostream>
using namespace std;
class student {
public:
void takedata();
void showdata();
private:
int admno;
char sname[21];//20 + '\0'
float eng,math,science;
float total;
float ctotal();
};
int main(int argc,char **argv) {
student stu;
stu.takedata();
stu.showdata();
return 0;
}
float student::ctotal() {
return this->eng + this->math + this->science;
}
void student::takedata() {
cin >> this->admno >> this->sname >> this->eng >> this->math >> this->science;
this->total = this->ctotal();
return;
}
void student::showdata() {
cout << this->admno << ' ' << this->sname << ' ' << this->eng << ' ' << this->math << ' ' << this->science << ' ' << this->total << endl;
return;
}
class student
{
private:
int admno;
char sname[21];
float eng;
float math;
float science;
float total;
float ctotal() { return eng+math+science; }
public:
void takedata()
{
cin >> admno >> sname >> eng >> math >> science;
total = ctotal();
}
void showdata()
{
cout << admno << " " << sname << " " << eng << " " << math << " " << science << " " << total << endl;
}
};