题目描述:
编写两个有意义的类,使一个类嵌套在另一个类中。
分析:
本题涉及两个类student和cdegree,前者为学生类,包含学生的学号(nubner),姓名(name)和成绩(degree),而成绩degree是类cdegree的对象。cdegree类有3个数据成员,分别为数学(math),英语(english)和物理(phy)分数。
程序代码:
#include#include using namespace std;class Student { public: void getdata(); void showdata(); private: string number; string name; class Cdegree { public: double math; double english; double phy; }degree; };void Student::getdata() { cout<<"Input number:"; cin>>number; cout<<"Input name:"; cin>>name; cout<<"Input degree of math:"; cin>>degree.math; cout<<"Input degree of english:"; cin>>degree.english; cout<<"Input degree of physics:"; cin>>degree.phy; }void Student::showdata(){ cout<<"=========分割线======="<
结果输出:
Input number:007Input name:qianshouInput degree of math:89Input degree of english:99Input degree of physics:100=========分割线=======Number:007Name:qianshouMath89English:99Physics:100