WEEK 1 [Project 1, 11th project 1

Source: Internet
Author: User

WEEK 1 [Project 1, 11th project 1

Problem description:

[Cpp]View plaincopyprint?
  1. Class Stu // declare the base class
  2. {
  3. Public:
  4. Stu (int n, string nam); // base class Constructor
  5. Void display (); // a member function that outputs base-class data members.
  6. Protected: // (*) data member with a protected access permission
  7. Int num; // student ID
  8. String name; // Student name
  9. };
  10. Class StuDetail: public Stu // declare the derived class StuDetail
  11. {
  12. Public:
  13. // Student nam, student ID n, a-year-old, who lives in ad, his shift leader is nam1, student ID n1
  14. StuDetail (int n, string nam, int a, string ad, int n1, string nam1); // constructor of the derived class
  15. Void show (); // member function, which outputs Student Information
  16. Void show_monitor (); // member function, which outputs the information of the monitor.
  17. Private:
  18. Stu monitor; // the class leader of the student's class. The class leader is a student and a member of the Stu class.
  19. Int age; // student age
  20. String addr; // student address
  21. };
  22. Int main ()
  23. {
  24. // Student Wang Li, No. 10010, 19 years old, lives in Beijing Road, Shanghai. His shift leader is Li Sun, student no. 10001
  25. StuDetail s (10010, "Wang-li", 19, "115 Beijing Road, Shanghai", 10001, "Li-sun ");
  26. S. show (); // outputs Student Information
  27. S. show_monitor (); // output the monitor information
  28. Return 0;
  29. }
(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:



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.