使用类内的成员函数给类内的私有成员赋值,并且成员函数在类内声明,类外定义
#include <iostream> #include <string.h> using namespace std; class Student{ private: string name; float math; float chinese; public: float average; void setname(); void setmath(); void setchinese(); float show_average(); float show_sum() ; }; void Student::setname () {cin>>name ;} void Student::setmath () {cin>>math;} void Student::setchinese () {cin>>chinese;} float Student::show_average () {cout<< (math+chinese)/2<<endl;} float Student::show_sum() {cout<<math + chinese<<endl;} main(){ Student a; a.setname(); a.setmath(); a.setchinese(); a.show_average(); a.show_sum(); return 0; }
原文:https://www.cnblogs.com/Atsuhiro/p/14828476.html