一道C++的类与对象问题

设计一个类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++

2个回答

我把整个代码写下来了,测试过了。

/*
# 一道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;
}
};
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐