WEEK 1 [Project 1, 11th project 1
Problem description:
[Cpp]View plaincopyprint?
- Class Stu // declare the base class
- {
- Public:
- Stu (int n, string nam); // base class Constructor
- Void display (); // a member function that outputs base-class data members.
- Protected: // (*) data member with a protected access permission
- Int num; // student ID
- String name; // Student name
- };
- Class StuDetail: public Stu // declare the derived class StuDetail
- {
- Public:
- // Student nam, student ID n, a-year-old, who lives in ad, his shift leader is nam1, student ID n1
- StuDetail (int n, string nam, int a, string ad, int n1, string nam1); // constructor of the derived class
- Void show (); // member function, which outputs Student Information
- Void show_monitor (); // member function, which outputs the information of the monitor.
- Private:
- Stu monitor; // the class leader of the student's class. The class leader is a student and a member of the Stu class.
- Int age; // student age
- String addr; // student address
- };
- Int main ()
- {
- // Student Wang Li, No. 10010, 19 years old, lives in Beijing Road, Shanghai. His shift leader is Li Sun, student no. 10001
- StuDetail s (10010, "Wang-li", 19, "115 Beijing Road, Shanghai", 10001, "Li-sun ");
- S. show (); // outputs Student Information
- S. show_monitor (); // output the monitor information
- Return 0;
- }
(1) The declared classes and test functions are described above. Complete the definition of member functions in the class to make the running results.
Code:
# Include <iostream> # include <cstring> using namespace std; class Stu // acoustic base class {public: Stu (int n, string nam ); // base class constructor void display (); // member function. The output base class data member protected: // (*) The access permission is the int num of the protected data member; // student ID string name; // Student name}; Stu: Stu (int n, string nam) {num = n; name = nam;} void Stu :: display () {cout <"Student Information \ n"; cout <"name:" <name <'\ 12'; cout <"student ID: "<num <'\ 12';} class StuDetail: public Stu // declare the derived class StuDetail {public: // student nam, student ID n, a years old, lives in ad, his class leader is nam1, student ID n1 StuDetail (int n, string nam, int a, string ad, int n1, string nam1); // The class constructor void show (); // member function, which outputs the student information void show_monitor (); // member function, which outputs the banner information private: Stu monitor; // the banner of the student's class. The banner is a student, is a member of the Stu class int age; // student age string addr; // student address}; StuDetail: StuDetail (int n, string nam, int a, string ad, int n1, string nam1): Stu (n, nam), monitor (n1, nam1), age (a), addr (ad) {} void StuDetail: show_monitor () {cout <" \ n"; cout <"name:" <name <'\ 12'; cout <"student ID: "<num <'\ 12';} void StuDetail: show () {display (); cout <" age: "<age <'\ 12'; cout <" Address: "<addr <' \ 12' <'\ 12';} int main () {// student Wang li, No. 10010, 19 years old, lives in Beijing Road, Shanghai. His class leader is li Sun, student ID: 10001 StuDetail s (10010, "Wang-li", 19, "115 Beijing Road, Shanghai", 10001, "Li-sun"); s. show (); // outputs student information s. show_monitor (); // return 0 ;}Running result:
(2) Change the access permission of data members of the Stu class to private. Can your program complete the required functions? If not, modify the program. Do not modify the given code. You can only modify your own code.
Code:
# Include <iostream> # include <cstring> using namespace std; class Stu // acoustic base class {public: Stu (int n, string nam ); // base class constructor void display (); // member function, output base class data member private: // (*) The access permission is the int num of the protected data member; // student ID string name; // Student name}; Stu: Stu (int n, string nam) {num = n; name = nam;} void Stu :: display () {cout <"Student Information \ n"; cout <"name:" <name <'\ 12'; cout <"student ID: "<num <'\ 12';} class StuDetail: public Stu // declare the derived class StuDetail {public: // student nam, student ID n, a years old, lives in ad, his class leader is nam1, student ID n1 StuDetail (int n, string nam, int a, string ad, int n1, string nam1); // The class constructor void show (); // member function, which outputs the student information void show_monitor (); // member function, which outputs the banner information private: Stu monitor; // the banner of the student's class. The banner is a student, is a member of the Stu class int age; // student age string addr; // student address}; StuDetail: StuDetail (int n, string nam, int a, string ad, int n1, string nam1): Stu (n, nam), monitor (n1, nam1), age (a), addr (ad) {} void StuDetail: show_monitor () {cout <" \ n"; display () ;}void StuDetail: show () {display (); cout <"age: "<age <'\ 12'; cout <" Address: "<addr <' \ 12' <'\ 12';} int main () {// student Wang li, No. 10010, 19 years old, lives in Beijing Road, Shanghai. His class leader is li Sun, student ID: 10001 StuDetail s (10010, "Wang-li", 19, "115 Beijing Road, Shanghai", 10001, "Li-sun"); s. show (); // outputs student information s. show_monitor (); // return 0 ;}Running result: