我把整个代码写下来了,测试过了。
/*
# 一道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;
}
解决后请点下采纳!