/* *copyright (c) 2016, *all right reserved, school of computer and Control engineering, Yantai University. * File name: 77.cpp * Author: Dong Kai * Completion Date: May 6, 2016 * version number: v1.0 * * Problem Description: Please complete the definition of member function in class * Input Description: * Program output: Student's information */#include <iostream> using namespace Std; Class Stu {public:stu (int n,string nam); void display ();p rivate:int num; String name; }; Stu::stu (int n,string nam) {num=n; Name=nam; } void Stu::d isplay () {cout<< "Study No.:" <<num<<endl; cout<< "Name:" <<name<<endl; }class studetail:public stu{public://Student NAM, study number n, live ad, his monitor nam1, study number n1 studetail (int n,string nam,int a,string ad,int N1 , string nam1); void Show (); void Show_monitor ();p rivate:stu Monitor; int age; string addr;}; Studetail::studetail (int n,string nam,int n1,string nam1,int a,string AD): Stu (N,nam), monitor (N1,NAM1), age (a), addr (ad {}void studetail::show () {cout<< "Student information:" <<endl; Stu::d isplay (); cout<< "Age:" <<age<<endl; cout<< "Address:" <<addr<<endl; Cout<<endl;} void Studetail::show_monitor () {cout<< "Monitor info:" <<endl; Monitor.display ();} int main () {//Student Wang Li, No. 10,010th, 19 years old, lives in Shanghai's Beijing Road, his monitor is Jason, study number 10001 Studetail s (10010, "Wang-li", "1", "Beijing Road,shanghai" 0001, "Li-sun"); S.show (); Output Student's data s.show_monitor (); Output sub-object data return 0;}
Summary of Knowledge points:
If you change the base class data member to private, you can take the following two methods:
1. Add two read number and name functions to the base class member function: int getnum{return num;} and string GetName {return name;}
In a derived class
void Studetail::show ()
When the member function is implemented, change to:
- void studetail::show ()
- {
- cout<<" Student Information: " <<ENDL;&NBSP;&NBSP;
- cout<<": " <<getnum () <<endl; //output num and name &NBSP;&NBSP;
- cout<<" name: " <<getname () <<endl;
- cout<<" Age: " <<age<<endl; //output age &NBSP;&NBSP;
- cout<<" address: " <<addr<<endl<< endl; //output addr &NBSP;&NBSP;
- }
2. Directly call the base class member function directly in the member function.
Learning experience:
To learn how to master some methods, flexible.
11th Week Practice Item 2.2-Student class to store monitor information (if the base class data member is changed to private)